diff --git a/bits.go b/bits.go index 15bafd87..c91cbb61 100644 --- a/bits.go +++ b/bits.go @@ -79,10 +79,7 @@ func (b *Bits) clearRange(startPos, count uint64) uint64 { // handle the potential partial word before pos becomes u64 aligned word := pos >> 6 bit := pos & 63 - take := uint64(64) - bit - if take > remaining { - take = remaining - } + take := min(uint64(64)-bit, remaining) if take > b.length-pos { take = b.length - pos } @@ -189,10 +186,7 @@ func (b *Bits) Update(l *slog.Logger, i uint64) bool { func (b *Bits) updateSlow(l *slog.Logger, i uint64) bool { // If i is a jump, adjust the window, record lost, update current, and return true if i > b.current { - end := i - if end > b.current+b.length { - end = b.current + b.length - } + end := min(i, b.current+b.length) count := end - b.current startPos := (b.current + 1) & b.lengthMask diff --git a/connection_state_test.go b/connection_state_test.go index d69c3817..310a26a4 100644 --- a/connection_state_test.go +++ b/connection_state_test.go @@ -97,7 +97,7 @@ func TestConnectionState_NextMessageCounter(t *testing.T) { assert.Equal(t, RejectAfterMessages, cs.messageCounter.Load()) // Continued send attempts stay refused and the counter never wraps - for i := 0; i < 10; i++ { + for range 10 { _, ok = cs.NextMessageCounter() assert.False(t, ok) } diff --git a/control_lifecycle_test.go b/control_lifecycle_test.go index a9f5323f..4bc75273 100644 --- a/control_lifecycle_test.go +++ b/control_lifecycle_test.go @@ -247,7 +247,7 @@ func TestControl_ConcurrentStopAndStart(t *testing.T) { c, _, _ := newReadyControl(t) var wg sync.WaitGroup - for i := 0; i < 2; i++ { + for range 2 { wg.Go(func() { c.Stop() }) } wg.Go(func() { _ = c.Start() }) diff --git a/scheduler_test.go b/scheduler_test.go index 085d523c..04a52109 100644 --- a/scheduler_test.go +++ b/scheduler_test.go @@ -1,21 +1,19 @@ package nebula import ( - "context" "testing" "time" ) func TestScheduler_PooledReuse(t *testing.T) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := t.Context() s := NewScheduler[int](16) delivered := make(chan int, 256) go s.Run(ctx, func(item int) { delivered <- item }) const N = 100 - for i := 0; i < N; i++ { + for i := range N { s.Schedule(ctx, i, time.Millisecond) } @@ -34,8 +32,7 @@ func TestScheduler_PooledReuse(t *testing.T) { // BenchmarkScheduler_Schedule reports allocations per Schedule call. // In steady state the Scheduler's sync.Pool means we should see zero allocs per op once the pool warms up. func BenchmarkScheduler_Schedule(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := b.Context() s := NewScheduler[int](b.N) go s.Run(ctx, func(int) {}) @@ -51,8 +48,7 @@ func BenchmarkScheduler_Schedule(b *testing.B) { // What we'd pay per Schedule if Punchy called time.AfterFunc directly without the pooled Scheduler. // Allocates a *time.Timer plus a closure each call. func BenchmarkBareAfterFunc(b *testing.B) { - ctx, cancel := context.WithCancel(context.Background()) - defer cancel() + ctx := b.Context() queue := make(chan int, b.N) go func() {