Skip to content

Commit

Permalink
silence_test: better unit test to detect deadlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Corentin Chary committed Sep 27, 2017
1 parent b00a48c commit 22f7a74
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions silence/silence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,32 @@ func TestSilencesSetSilence(t *testing.T) {
s.gossip = &mockGossip{
broadcast: func(d mesh.GossipData) {
data, ok := d.(*gossipData)

// Double check that we can take a lock on s.mtx here.
s.mtx.Lock()
defer s.mtx.Unlock()

require.True(t, ok, "gossip data of unknown type")
require.Equal(t, want, data, "unexpected gossip broadcast data")
require.Equal(t, want.data, data.data, "unexpected gossip broadcast data")
close(done)
},
}
require.NoError(t, s.setSilence(sil))
require.Equal(t, want.data, s.st.data, "Unexpected silence state")

// setSilence() is always called with s.mtx locked()
go func() {
s.mtx.Lock()
require.NoError(t, s.setSilence(sil))
s.mtx.Unlock()
}()

// GossipBroadcast is called in a goroutine.
select {
case <-done:
case <-time.After(1 * time.Second):
t.Fatal("GossipBroadcast was not called")
}

require.Equal(t, want.data, s.st.data, "Unexpected silence state")
}

func TestSilenceSet(t *testing.T) {
Expand Down

0 comments on commit 22f7a74

Please sign in to comment.