Commit 47052b24 authored by acud's avatar acud Committed by GitHub

fix test flake (#788)

parent fb7dcddc
...@@ -8,7 +8,6 @@ import ( ...@@ -8,7 +8,6 @@ import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"math" "math"
"runtime"
"testing" "testing"
"time" "time"
...@@ -58,8 +57,7 @@ func TestOneSync(t *testing.T) { ...@@ -58,8 +57,7 @@ func TestOneSync(t *testing.T) {
}) })
defer puller.Close() defer puller.Close()
defer pullsync.Close() defer pullsync.Close()
runtime.Gosched() time.Sleep(100 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
kad.Trigger() kad.Trigger()
...@@ -108,15 +106,14 @@ func TestSyncFlow_PeerOutsideDepth_Live(t *testing.T) { ...@@ -108,15 +106,14 @@ func TestSyncFlow_PeerOutsideDepth_Live(t *testing.T) {
pullsync.Close() pullsync.Close()
puller.Close() puller.Close()
}) })
runtime.Gosched() time.Sleep(100 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
kad.Trigger() kad.Trigger()
waitCursorsCalled(t, pullsync, addr, false) waitCursorsCalled(t, pullsync, addr, false)
waitLiveSyncCalled(t, pullsync, addr, false) waitLiveSyncCalled(t, pullsync, addr, false)
checkCalls(t, tc.expCalls, pullsync.SyncCalls(addr)) // hist always empty waitCheckCalls(t, tc.expCalls, pullsync.SyncCalls, addr) // hist always empty
checkCalls(t, tc.expLiveCalls, pullsync.LiveSyncCalls(addr)) waitCheckCalls(t, tc.expLiveCalls, pullsync.LiveSyncCalls, addr)
// check the intervals // check the intervals
checkIntervals(t, st, addr, tc.intervals, 1) checkIntervals(t, st, addr, tc.intervals, 1)
...@@ -183,8 +180,7 @@ func TestSyncFlow_PeerOutsideDepth_Historical(t *testing.T) { ...@@ -183,8 +180,7 @@ func TestSyncFlow_PeerOutsideDepth_Historical(t *testing.T) {
}) })
defer puller.Close() defer puller.Close()
defer pullsync.Close() defer pullsync.Close()
runtime.Gosched() time.Sleep(100 * time.Millisecond)
time.Sleep(10 * time.Millisecond)
kad.Trigger() kad.Trigger()
...@@ -192,8 +188,8 @@ func TestSyncFlow_PeerOutsideDepth_Historical(t *testing.T) { ...@@ -192,8 +188,8 @@ func TestSyncFlow_PeerOutsideDepth_Historical(t *testing.T) {
waitSyncCalledTimes(t, pullsync, addr, len(tc.expCalls)) waitSyncCalledTimes(t, pullsync, addr, len(tc.expCalls))
// check historical sync calls // check historical sync calls
checkCalls(t, tc.expCalls, pullsync.SyncCalls(addr)) waitCheckCalls(t, tc.expCalls, pullsync.SyncCalls, addr)
checkCalls(t, tc.expLiveCalls, pullsync.LiveSyncCalls(addr)) waitCheckCalls(t, tc.expLiveCalls, pullsync.LiveSyncCalls, addr)
// check the intervals // check the intervals
checkIntervals(t, st, addr, tc.intervals, 1) checkIntervals(t, st, addr, tc.intervals, 1)
...@@ -231,7 +227,6 @@ func TestSyncFlow_PeerWithinDepth_Live(t *testing.T) { ...@@ -231,7 +227,6 @@ func TestSyncFlow_PeerWithinDepth_Live(t *testing.T) {
}) })
defer puller.Close() defer puller.Close()
defer pullsync.Close() defer pullsync.Close()
runtime.Gosched()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
kad.Trigger() kad.Trigger()
...@@ -240,7 +235,7 @@ func TestSyncFlow_PeerWithinDepth_Live(t *testing.T) { ...@@ -240,7 +235,7 @@ func TestSyncFlow_PeerWithinDepth_Live(t *testing.T) {
waitLiveSyncCalledTimes(t, pullsync, addr, len(tc.expLiveCalls)) waitLiveSyncCalledTimes(t, pullsync, addr, len(tc.expLiveCalls))
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
checkCalls(t, tc.expCalls, pullsync.SyncCalls(addr)) // hist always empty waitCheckCalls(t, tc.expCalls, pullsync.SyncCalls, addr) // hist always empty
checkCallsUnordered(t, tc.expLiveCalls, pullsync.LiveSyncCalls(addr)) checkCallsUnordered(t, tc.expLiveCalls, pullsync.LiveSyncCalls(addr))
// check the intervals // check the intervals
...@@ -267,7 +262,6 @@ func TestPeerDisconnected(t *testing.T) { ...@@ -267,7 +262,6 @@ func TestPeerDisconnected(t *testing.T) {
p.Close() p.Close()
}) })
runtime.Gosched()
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
kad.Trigger() kad.Trigger()
...@@ -359,7 +353,6 @@ func TestDepthChange(t *testing.T) { ...@@ -359,7 +353,6 @@ func TestDepthChange(t *testing.T) {
defer puller.Close() defer puller.Close()
defer pullsync.Close() defer pullsync.Close()
runtime.Gosched()
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
for i := 0; i < len(tc.depths)-1; i++ { for i := 0; i < len(tc.depths)-1; i++ {
...@@ -411,24 +404,31 @@ func checkNotFound(t *testing.T, s storage.StateStorer, addr swarm.Address, bin ...@@ -411,24 +404,31 @@ func checkNotFound(t *testing.T, s storage.StateStorer, addr swarm.Address, bin
t.Fatalf("wanted error but got none. bin %d", bin) t.Fatalf("wanted error but got none. bin %d", bin)
} }
func checkCalls(t *testing.T, expCalls []c, calls []mockps.SyncCall) { func waitCheckCalls(t *testing.T, expCalls []c, callsFn func(swarm.Address) []mockps.SyncCall, addr swarm.Address) {
t.Helper() t.Helper()
exp := len(expCalls) for i := 0; i < 10; i++ {
if l := len(calls); l != exp { time.Sleep(50 * time.Millisecond)
t.Fatalf("expected %d calls but got %d. calls: %v", exp, l, calls) calls := callsFn(addr)
} if l := len(calls); l != len(expCalls) {
// check the calls t.Log(l, len(expCalls), "continue")
for i, v := range expCalls { continue
if b := calls[i].Bin; b != v.b {
t.Errorf("bin mismatch. got %d want %d index %d", b, v.b, i)
}
if f := calls[i].From; f != v.f {
t.Errorf("from mismatch. got %d want %d index %d", f, v.f, i)
} }
if tt := calls[i].To; tt != v.t { // check the calls
t.Errorf("to mismatch. got %d want %d index %d", tt, v.t, i) for i, v := range expCalls {
if b := calls[i].Bin; b != v.b {
t.Errorf("bin mismatch. got %d want %d index %d", b, v.b, i)
}
if f := calls[i].From; f != v.f {
t.Errorf("from mismatch. got %d want %d index %d", f, v.f, i)
}
if tt := calls[i].To; tt != v.t {
t.Errorf("to mismatch. got %d want %d index %d", tt, v.t, i)
}
} }
return
} }
calls := callsFn(addr)
t.Fatalf("expected %d calls but got %d. calls: %v", len(expCalls), len(calls), calls)
} }
// this is needed since there are several goroutines checking the calls, // this is needed since there are several goroutines checking the calls,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment