Skip to content
This repository was archived by the owner on Jun 19, 2022. It is now read-only.

Commit

Permalink
Stop and start pool between tests to prevent data races
Browse files Browse the repository at this point in the history
This is necessary since the test introspects internal state.
  • Loading branch information
ian-mi committed Apr 13, 2020
1 parent 682dcab commit fd2f6b7
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion pkg/broker/handler/pool/fanout/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ func TestWatchAndSync(t *testing.T) {
targets := make(chan *config.TargetsConfig)
defer close(targets)
emptyTargetConfig := new(config.TargetsConfig)
p, err := StartSyncPool(ctx, config.NewTargetsWatcher(targets),
watcher := config.NewTargetsWatcher(targets)
p, err := StartSyncPool(ctx, watcher,
pool.WithPubsubClient(ps),
pool.WithProjectID(testProject),
)
Expand All @@ -69,9 +70,20 @@ func TestWatchAndSync(t *testing.T) {
targets <- targetConfig
// Wait a short period for the handlers to be updated.
<-time.After(time.Second)
cancel()
assertHandlers(t, p, targetConfig)
})

ctx, cancel = context.WithCancel(context.Background())
defer cancel()
p, err = StartSyncPool(ctx, watcher,
pool.WithPubsubClient(ps),
pool.WithProjectID(testProject),
)
if err != nil {
t.Errorf("unexpected error from starting sync pool: %v", err)
}

t.Run("adding and deleting brokers changes handlers", func(t *testing.T) {
newTargetConfig := &config.TargetsConfig{Brokers: make(map[string]*config.Broker)}
// Populate with new brokers.
Expand All @@ -83,9 +95,20 @@ func TestWatchAndSync(t *testing.T) {
targets <- newTargetConfig
// Wait a short period for the handlers to be updated.
<-time.After(time.Second)
cancel()
assertHandlers(t, p, newTargetConfig)
})

ctx, cancel = context.WithCancel(context.Background())
defer cancel()
p, err = StartSyncPool(ctx, watcher,
pool.WithPubsubClient(ps),
pool.WithProjectID(testProject),
)
if err != nil {
t.Errorf("unexpected error from starting sync pool: %v", err)
}

t.Run("deleting all brokers deletes all handlers", func(t *testing.T) {
// clean up all brokers
targets <- emptyTargetConfig
Expand Down

0 comments on commit fd2f6b7

Please sign in to comment.