mirror of
https://github.com/YspCoder/clawgo.git
synced 2026-05-16 13:43:34 +08:00
refactor: stabilize runtime and unify config
This commit is contained in:
61
pkg/bus/bus_test.go
Normal file
61
pkg/bus/bus_test.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package bus
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestMessageBusPublishAfterCloseDoesNotPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mb := NewMessageBus()
|
||||
mb.Close()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
mb.PublishInbound(InboundMessage{Channel: "test"})
|
||||
mb.PublishOutbound(OutboundMessage{Channel: "test"})
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(2 * time.Second):
|
||||
t.Fatal("publish after close blocked")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageBusCloseWhilePublishingDoesNotPanic(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mb := NewMessageBus()
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
if _, ok := mb.ConsumeInbound(ctx); !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 8; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for j := 0; j < 50; j++ {
|
||||
mb.PublishInbound(InboundMessage{Channel: "test"})
|
||||
mb.PublishOutbound(OutboundMessage{Channel: "test"})
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
mb.Close()
|
||||
cancel()
|
||||
wg.Wait()
|
||||
}
|
||||
Reference in New Issue
Block a user