grafana/pkg/bus/bus_test.go

93 lines
2.0 KiB
Go
Raw Normal View History

2014-11-28 15:16:49 -06:00
package bus
import (
"context"
2014-11-28 15:16:49 -06:00
"testing"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/stretchr/testify/require"
2014-11-28 15:16:49 -06:00
)
type testQuery struct {
ID int64
2014-11-28 15:16:49 -06:00
Resp string
}
func TestEventPublish(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
var invoked bool
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
invoked = true
2015-01-09 09:36:23 -06:00
return nil
})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
require.True(t, invoked)
}
func TestEventPublish_NoRegisteredListener(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
2015-01-09 09:36:23 -06:00
}
func TestEventCtxPublishCtx(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
var invoked bool
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
invoked = true
return nil
})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
require.True(t, invoked)
}
func TestEventPublishCtx_NoRegisteredListener(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
}
func TestEventPublishCtx(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
var invoked bool
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
invoked = true
return nil
})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
require.True(t, invoked)
}
func TestEventCtxPublish(t *testing.T) {
bus := ProvideBus(tracing.InitializeTracerForTest())
var invoked bool
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
invoked = true
return nil
})
err := bus.Publish(context.Background(), &testQuery{})
require.NoError(t, err, "unable to publish event")
require.True(t, invoked)
}