Browse Source

statesync: clarify test cleanup (#7565)

pull/7568/merge
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
fb10d1c705
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 23 deletions
  1. +5
    -4
      internal/statesync/reactor_test.go
  2. +30
    -19
      internal/statesync/syncer_test.go

+ 5
- 4
internal/statesync/reactor_test.go View File

@ -193,13 +193,14 @@ func setup(
rts.reactor.metrics,
)
ctx, cancel := context.WithCancel(ctx)
require.NoError(t, rts.reactor.Start(ctx))
require.True(t, rts.reactor.IsRunning())
t.Cleanup(func() {
rts.reactor.Wait()
require.False(t, rts.reactor.IsRunning())
})
t.Cleanup(cancel)
t.Cleanup(rts.reactor.Wait)
t.Cleanup(leaktest.Check(t))
return rts
}


+ 30
- 19
internal/statesync/syncer_test.go View File

@ -133,27 +133,38 @@ func TestSyncer_SyncAny(t *testing.T) {
chunkRequests := make(map[uint32]int)
chunkRequestsMtx := sync.Mutex{}
var wg sync.WaitGroup
wg.Add(4)
chunkProcessDone := make(chan struct{})
go func() {
for e := range rts.chunkOutCh {
msg, ok := e.Message.(*ssproto.ChunkRequest)
assert.True(t, ok)
assert.EqualValues(t, 1, msg.Height)
assert.EqualValues(t, 1, msg.Format)
assert.LessOrEqual(t, msg.Index, uint32(len(chunks)))
added, err := rts.syncer.AddChunk(chunks[msg.Index])
assert.NoError(t, err)
assert.True(t, added)
chunkRequestsMtx.Lock()
chunkRequests[msg.Index]++
chunkRequestsMtx.Unlock()
defer close(chunkProcessDone)
var seen int
for {
if seen >= 4 {
return
}
wg.Done()
select {
case <-ctx.Done():
t.Logf("sent %d chunks", seen)
return
case e := <-rts.chunkOutCh:
msg, ok := e.Message.(*ssproto.ChunkRequest)
assert.True(t, ok)
assert.EqualValues(t, 1, msg.Height)
assert.EqualValues(t, 1, msg.Format)
assert.LessOrEqual(t, msg.Index, uint32(len(chunks)))
added, err := rts.syncer.AddChunk(chunks[msg.Index])
assert.NoError(t, err)
assert.True(t, added)
chunkRequestsMtx.Lock()
chunkRequests[msg.Index]++
chunkRequestsMtx.Unlock()
seen++
t.Logf("added chunk (%d of 4): %d", seen, msg.Index)
}
}
}()
@ -186,7 +197,7 @@ func TestSyncer_SyncAny(t *testing.T) {
newState, lastCommit, err := rts.syncer.SyncAny(ctx, 0, func() error { return nil })
require.NoError(t, err)
wg.Wait()
<-chunkProcessDone
chunkRequestsMtx.Lock()
require.Equal(t, map[uint32]int{0: 1, 1: 2, 2: 1}, chunkRequests)


Loading…
Cancel
Save