Files
upstream-corteza/server/pkg/eventbus/eventbus_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

110 lines
2.2 KiB
Go

package eventbus
import (
"context"
"fmt"
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/atomic"
)
func TestEventbusRegUnreg(t *testing.T) {
var (
a = assert.New(t)
bus = New()
)
a.Empty(bus.handlers)
h1 := bus.Register(nil)
a.NotZero(h1)
h2 := bus.Register(nil)
a.NotZero(h2)
h3 := bus.Register(nil)
a.NotZero(h3)
a.Len(bus.handlers, 3)
bus.Unregister(h1)
a.Len(bus.handlers, 2)
bus.Unregister(h2)
a.Len(bus.handlers, 1)
bus.Unregister(h3)
a.Empty(bus.handlers)
}
func BenchmarkEventbusHandlerLookup(b *testing.B) {
var (
bus = New()
ptrs []uintptr
)
// Register handlers
for n := 0; n < b.N; n++ {
ptrs = append(ptrs, bus.Register(nil))
}
// Register find & fire handlers
for n := 0; n < b.N; n++ {
bus.find(nil)
}
}
func BenchmarkEventbusRegUnreg(b *testing.B) {
var (
bus = New()
ptrs []uintptr
)
// Register handlers
for n := 0; n < b.N; n++ {
ptrs = append(ptrs, bus.Register(nil))
}
// Shuffle stored pinters
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(ptrs), func(i, j int) { ptrs[i], ptrs[j] = ptrs[j], ptrs[i] })
// Unregister all pointers
for _, ptr := range ptrs {
bus.Unregister(ptr)
}
}
func TestEventFiring(t *testing.T) {
var (
a = assert.New(t)
// Let's make sure our simple tests don't
// cause any extra stress with dat race conditions
i = &atomic.Int32{}
ev = func(ev string) Event {
return &mockEvent{rType: "resource", eType: ev}
}
ctx = context.Background()
bus = New()
)
bus.Register(func(ctx context.Context, ev Event) error { i.Inc(); return nil }, On("inc"), For("resource"))
bus.Register(func(ctx context.Context, ev Event) error { i.Dec(); return nil }, On("dec"), For("resource"))
bus.Register(func(ctx context.Context, ev Event) error { return fmt.Errorf("handl-err") }, On("err"), For("resource"))
a.Equal(int32(0), i.Load())
a.NoError(bus.WaitFor(ctx, ev("inc")))
a.Equal(int32(1), i.Load())
a.NoError(bus.WaitFor(ctx, ev("inc")))
a.Equal(int32(2), i.Load())
a.NoError(bus.WaitFor(ctx, ev("dec")))
a.Equal(int32(1), i.Load())
a.EqualError(bus.WaitFor(ctx, ev("err")), "handl-err")
bus.Dispatch(ctx, ev("dec"))
bus.Dispatch(ctx, ev("inc"))
bus.Dispatch(ctx, ev("inc"))
bus.wait()
a.Equal(int32(2), i.Load())
}