Improved perfs
This commit is contained in:
@@ -122,6 +122,68 @@ func TestUnknownSignal(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxUpdateRate(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
const maxHz = 5.0 // 5 Hz → 200 ms interval
|
||||
|
||||
ds := stub.New() // counter_fast fires at 1 kHz
|
||||
if err := ds.Connect(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
b := broker.New(ctx, slog.Default(), broker.WithMaxUpdateRate(maxHz))
|
||||
b.Register(ds)
|
||||
|
||||
ref := broker.SignalRef{DS: "stub", Name: "counter_fast"}
|
||||
ch := make(chan broker.Update, 512)
|
||||
unsub, err := b.Subscribe(ref, ch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer unsub()
|
||||
|
||||
// Collect updates for 2 seconds.
|
||||
var count int
|
||||
deadline := time.After(2 * time.Second)
|
||||
loop:
|
||||
for {
|
||||
select {
|
||||
case <-ch:
|
||||
count++
|
||||
case <-deadline:
|
||||
break loop
|
||||
}
|
||||
}
|
||||
|
||||
// At 5 Hz for 2 s we expect ~10 updates (±3 for timer jitter + coalescing).
|
||||
t.Logf("MaxUpdateRate: received %d updates at %.0f Hz limit over 2s (expect ~10)", count, maxHz)
|
||||
if count < 5 || count > 20 {
|
||||
t.Errorf("expected ~10 updates at 5 Hz, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaxUpdateRate_UnlimitedByDefault(t *testing.T) {
|
||||
b, cancel := newBroker(t) // no WithMaxUpdateRate → unlimited
|
||||
defer cancel()
|
||||
|
||||
ref := broker.SignalRef{DS: "stub", Name: "counter_fast"}
|
||||
ch := make(chan broker.Update, 4096)
|
||||
unsub, err := b.Subscribe(ref, ch)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer unsub()
|
||||
|
||||
// counter_fast fires at 1 kHz; over 500 ms we should see ≥ 400 updates.
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
got := len(ch)
|
||||
t.Logf("Unlimited: received %d updates in 500ms from 1kHz signal", got)
|
||||
if got < 400 {
|
||||
t.Errorf("expected >= 400 updates with no rate limit, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleSignals(t *testing.T) {
|
||||
b, cancel := newBroker(t)
|
||||
defer cancel()
|
||||
|
||||
Reference in New Issue
Block a user