Hackaton: Add more unit tests, take 3 (#101525)
* serviceaccounts/secretscan: test Service more thoroughly * middleware/cookies: add tests for CookieOptions * anonymous/anonimpl: cover a couple more methods * components/imguploader: Implement WebDAV integration tests * components/apikeygen: also check IsValid method * bus: cover invalid callback signature cases * cloudmigration/objectstorage: add basic unit tests * login/social/connectors: add test case for GitHub OAuth fetch emails+orgs * expr/classic: cover more evaluator types in tests
This commit is contained in:
@@ -2,6 +2,7 @@ package bus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -91,3 +92,61 @@ func TestEventCtxPublish(t *testing.T) {
|
||||
|
||||
require.True(t, invoked)
|
||||
}
|
||||
|
||||
func TestEventListenerError(t *testing.T) {
|
||||
bus := ProvideBus(tracing.InitializeTracerForTest())
|
||||
|
||||
mockErr := errors.New("error")
|
||||
|
||||
invocations := 0
|
||||
|
||||
// Will be called in order of declaration.
|
||||
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
||||
invocations++
|
||||
return nil
|
||||
})
|
||||
|
||||
bus.AddEventListener(func(ctx context.Context, query *testQuery) error {
|
||||
invocations++
|
||||
return mockErr
|
||||
})
|
||||
|
||||
bus.AddEventListener(func(ctx context.Context, query *testQuery) {
|
||||
invocations++
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
require.ErrorIs(t, err, mockErr)
|
||||
require.Equal(t, 2, invocations)
|
||||
}
|
||||
|
||||
func TestEventListenerInvalidCallbackType(t *testing.T) {
|
||||
bus := ProvideBus(tracing.InitializeTracerForTest())
|
||||
|
||||
invoked := false
|
||||
|
||||
bus.AddEventListener(func(ctx context.Context, query *testQuery) bool {
|
||||
invoked = true
|
||||
return invoked
|
||||
})
|
||||
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
require.Error(t, err)
|
||||
require.True(t, invoked)
|
||||
}
|
||||
|
||||
func TestEventListenerInvalidCallback(t *testing.T) {
|
||||
bus := ProvideBus(tracing.InitializeTracerForTest())
|
||||
|
||||
invoked := false
|
||||
|
||||
bus.AddEventListener(func(ctx context.Context, query *testQuery) {
|
||||
invoked = true
|
||||
})
|
||||
|
||||
require.Panics(t, func() {
|
||||
err := bus.Publish(context.Background(), &testQuery{})
|
||||
require.NoError(t, err) // unreachable
|
||||
})
|
||||
require.True(t, invoked)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user