From 4b947530c4e5d280eb3870c72dd446368b70fa9a Mon Sep 17 00:00:00 2001 From: Alain Jobart Date: Wed, 20 Sep 2017 08:12:12 -0700 Subject: [PATCH] Fixing race condition in discovery test. BUG=66067941 --- go/vt/discovery/topology_watcher_test.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/go/vt/discovery/topology_watcher_test.go b/go/vt/discovery/topology_watcher_test.go index 90dc91513ec..a9d1ec02481 100644 --- a/go/vt/discovery/topology_watcher_test.go +++ b/go/vt/discovery/topology_watcher_test.go @@ -50,6 +50,13 @@ func checkWatcher(t *testing.T, cellTablets bool) { t.Logf(`tw = ShardReplicationWatcher(topo.Server{ft}, fhc, "aa", "keyspace", "shard", 10ms, 5)`) } + // Wait for the initial topology load to finish. Otherwise we + // have a background loadTablets() that's running, and it can + // interact with our tests in weird ways. + if err := tw.WaitForInitialTopology(); err != nil { + t.Fatalf("initial WaitForInitialTopology failed") + } + // add a tablet to the topology ft.AddTablet("aa", 0, "host1", map[string]int32{"vt": 123}) tw.loadTablets() @@ -198,7 +205,16 @@ func (ft *fakeTopo) GetShardReplication(ctx context.Context, cell, keyspace, sha func (ft *fakeTopo) GetTablet(ctx context.Context, alias *topodatapb.TabletAlias) (*topodatapb.Tablet, int64, error) { ft.mu.RLock() defer ft.mu.RUnlock() - return ft.tablets[topoproto.TabletAliasString(alias)], 0, nil + // Note we want to be correct here. The way we call this, we never + // change the tablet list in between a call to list them, + // and a call to get the record, so we could just blindly return it. + // (It wasn't the case before we added the WaitForInitialTopology() + // call in the test though!). + tablet, ok := ft.tablets[topoproto.TabletAliasString(alias)] + if !ok { + return nil, 0, topo.ErrNoNode + } + return tablet, 0, nil } func TestFilterByShard(t *testing.T) {