Skip to content

Commit

Permalink
prevent nil panic due to throttler closing early
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <tim@timvaillancourt.com>
  • Loading branch information
timvaillancourt committed Jun 7, 2024
1 parent b39a72a commit ea06c1b
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions go/vt/throttler/throttler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package throttler
import (
"context"
"runtime"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -409,17 +410,14 @@ func TestThrottlerMaxLag(t *testing.T) {
throttler, _ := newThrottlerWithClock("test", "queries", 1, 1, ReplicationLagModuleDisabled, fc.now)
defer throttler.Close()

var err error
ratesHistory := newFakeRatesHistory()
fc.setNow(1 * time.Second)
config := NewMaxReplicationLagModuleConfig(10)
throttler.maxReplicationLagModule, err = NewMaxReplicationLagModule(config, ratesHistory.aggregatedIntervalHistory, fc.now)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

go func(ctx context.Context, throttler *Throttler) {
var wg sync.WaitGroup

go func(wg *sync.WaitGroup, ctx context.Context, throttler *Throttler) {
wg.Add(1)

Check failure on line 419 in go/vt/throttler/throttler_test.go

View workflow job for this annotation

GitHub Actions / Static Code Checks Etc

SA2000: should call wg.Add(1) before starting the goroutine to avoid a race (staticcheck)
defer wg.Done()
for {
select {
case <-ctx.Done():
Expand All @@ -433,9 +431,11 @@ func TestThrottlerMaxLag(t *testing.T) {
}
}
}
}(ctx, throttler)
}(&wg, ctx, throttler)

go func(ctx context.Context, throttler *Throttler) {
go func(wg *sync.WaitGroup, ctx context.Context, throttler *Throttler) {
wg.Add(1)

Check failure on line 437 in go/vt/throttler/throttler_test.go

View workflow job for this annotation

GitHub Actions / Static Code Checks Etc

SA2000: should call wg.Add(1) before starting the goroutine to avoid a race (staticcheck)
defer wg.Done()
for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -464,7 +464,9 @@ func TestThrottlerMaxLag(t *testing.T) {
}
}
}
}(ctx, throttler)
}(&wg, ctx, throttler)

time.Sleep(time.Second)
cancel()
wg.Wait()
}

0 comments on commit ea06c1b

Please sign in to comment.