2014-11-28 15:16:49 -06:00
|
|
|
package bus
|
|
|
|
|
|
|
|
import (
|
2018-06-08 03:51:27 -05:00
|
|
|
"context"
|
2014-11-28 15:16:49 -06:00
|
|
|
"testing"
|
2019-10-14 10:23:31 -05:00
|
|
|
|
2022-01-20 04:10:12 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2019-10-14 10:23:31 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2014-11-28 15:16:49 -06:00
|
|
|
)
|
|
|
|
|
2018-06-08 03:51:27 -05:00
|
|
|
type testQuery struct {
|
2019-06-04 15:00:05 -05:00
|
|
|
ID int64
|
2014-11-28 15:16:49 -06:00
|
|
|
Resp string
|
|
|
|
}
|
|
|
|
|
2021-11-19 06:56:42 -06:00
|
|
|
func TestEventPublish(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2019-11-13 06:02:44 -06:00
|
|
|
|
|
|
|
var invoked bool
|
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
2019-11-13 06:02:44 -06:00
|
|
|
invoked = true
|
2015-01-09 09:36:23 -06:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2019-11-13 06:02:44 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
|
|
|
|
require.True(t, invoked)
|
|
|
|
}
|
|
|
|
|
2021-11-19 06:56:42 -06:00
|
|
|
func TestEventPublish_NoRegisteredListener(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2019-11-13 06:02:44 -06:00
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2019-11-13 06:02:44 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
2015-01-09 09:36:23 -06:00
|
|
|
}
|
2021-11-19 06:56:42 -06:00
|
|
|
|
|
|
|
func TestEventCtxPublishCtx(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2021-11-19 06:56:42 -06:00
|
|
|
|
|
|
|
var invoked bool
|
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
2021-11-19 06:56:42 -06:00
|
|
|
invoked = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2021-11-19 06:56:42 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
|
|
|
|
require.True(t, invoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventPublishCtx_NoRegisteredListener(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2021-11-19 06:56:42 -06:00
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2021-11-19 06:56:42 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventPublishCtx(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2021-11-19 06:56:42 -06:00
|
|
|
|
|
|
|
var invoked bool
|
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
2021-11-19 06:56:42 -06:00
|
|
|
invoked = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2021-11-19 06:56:42 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
|
|
|
|
require.True(t, invoked)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEventCtxPublish(t *testing.T) {
|
2022-06-15 05:40:41 -05:00
|
|
|
bus := ProvideBus(tracing.InitializeTracerForTest())
|
2021-11-19 06:56:42 -06:00
|
|
|
|
|
|
|
var invoked bool
|
|
|
|
|
2022-01-04 02:36:01 -06:00
|
|
|
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
2021-11-19 06:56:42 -06:00
|
|
|
invoked = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-06-15 05:40:41 -05:00
|
|
|
err := bus.Publish(context.Background(), &testQuery{})
|
2021-11-19 06:56:42 -06:00
|
|
|
require.NoError(t, err, "unable to publish event")
|
|
|
|
|
|
|
|
require.True(t, invoked)
|
|
|
|
}
|