Skip to content

Commit

Permalink
Merge pull request #10752 from hashicorp/b-fix-test-datarace-volumewa…
Browse files Browse the repository at this point in the history
…tcher

volumewatcher: fix test data race.
  • Loading branch information
jrasell committed Jun 14, 2021
2 parents 2b63a09 + b6505c2 commit 7019bc2
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions nomad/volumewatcher/volumes_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@ func TestVolumeWatch_EnableDisable(t *testing.T) {
err = srv.State().CSIVolumeClaim(index, vol.Namespace, vol.ID, claim)
require.NoError(err)
require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return 1 == len(watcher.watchers)
}, time.Second, 10*time.Millisecond)

watcher.SetEnabled(false, srv.State())
watcher.SetEnabled(false, nil)
require.Equal(0, len(watcher.watchers))
}

Expand Down Expand Up @@ -77,16 +79,20 @@ func TestVolumeWatch_Checkpoint(t *testing.T) {
// we should get or start up a watcher when we get an update for
// the volume from the state store
require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return 1 == len(watcher.watchers)
}, time.Second, 10*time.Millisecond)

// step-down (this is sync, but step-up is async)
watcher.SetEnabled(false, srv.State())
watcher.SetEnabled(false, nil)
require.Equal(0, len(watcher.watchers))

// step-up again
watcher.SetEnabled(true, srv.State())
require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return 1 == len(watcher.watchers) &&
!watcher.watchers[vol.ID+vol.Namespace].isRunning()
}, time.Second, 10*time.Millisecond)
Expand Down Expand Up @@ -128,6 +134,8 @@ func TestVolumeWatch_StartStop(t *testing.T) {

// assert we get a watcher; there are no claims so it should immediately stop
require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return 1 == len(watcher.watchers) &&
!watcher.watchers[vol.ID+vol.Namespace].isRunning()
}, time.Second*2, 10*time.Millisecond)
Expand Down Expand Up @@ -177,6 +185,8 @@ func TestVolumeWatch_StartStop(t *testing.T) {
}, time.Second*2, 10*time.Millisecond)

require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return !watcher.watchers[vol.ID+vol.Namespace].isRunning()
}, time.Second*5, 10*time.Millisecond)
}
Expand Down Expand Up @@ -209,6 +219,8 @@ func TestVolumeWatch_RegisterDeregister(t *testing.T) {

// watcher should be started but immediately stopped
require.Eventually(func() bool {
watcher.wlock.RLock()
defer watcher.wlock.RUnlock()
return 1 == len(watcher.watchers)
}, time.Second, 10*time.Millisecond)

Expand Down

0 comments on commit 7019bc2

Please sign in to comment.