[P2P] Fix rarely occurred test failure
There was a timing bug in unit test.
@@ -295,9 +295,9 @@ func TestMultiLoads(t *testing.T) {
go consumeForLongterm(c, tt.testTime+time.Minute, doneC, finish)
wg := sync.WaitGroup{}
expire := time.Now().Add(tt.testTime)
+ wg.Add(threadsize)
for j := 0; j < threadsize; j++ {
go func(tid int) {
- wg.Add(1)
i := 0
for time.Now().Before(expire) {
c.In() <- mos[i%arrSize]
@@ -316,8 +316,8 @@ func TestMultiLoads(t *testing.T) {
lock.Unlock()
fmt.Printf("In %d , out %d , drop %d, consecutive drop %d\n", actStat.incnt, actStat.outcnt, actStat.dropcnt, actStat.consecdrop)
- // last one is in channel and not consumed
- assert.Equal(t, uint64(consumeCount+1), actStat.outcnt)
+ // There are two cases, one is last one is in channel and not consumed, and another is consumed all items.
+ assert.True(t, actStat.outcnt-uint64(consumeCount) <= 1 )
// in should equal to sum of out, drop, and remained in queue
assert.Equal(t, actStat.incnt, actStat.outcnt+actStat.dropcnt+uint64(rqueue.Size()))
