remove sleep from stale test use timeout and select loop
@@ -2,6 +2,7 @@ package p2p
import (
"fmt"
+ "github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/p2p/simulator"
"github.com/stretchr/testify/assert"
"testing"
@@ -86,11 +87,21 @@ func TestProtocol_CleanOldPendingMessages(t *testing.T) {
err := fnd2.SendAsyncRequest(1, nil, n1.PublicKey().String(), callback)
assert.NoError(t, err, "Should not return error")
- assert.EqualValues(t, 1, fnd2.pendingQueue.Len(), "value received did not match correct value")
-
- for fnd2.pendingQueue.Len() > 0 {
-
+ assert.EqualValues(t, 1, fnd2.pendingQueue.Len(), "value received did not match correct value1")
+
+ timeout := time.After(3 * time.Second)
+ // Keep trying until we're timed out or got a result or got an error
+
+ select {
+ // Got a timeout! fail with a timeout error
+ case <-timeout:
+ log.Debug("timeout")
+ t.Error("timeout")
+ return
+ default:
+ if fnd2.pendingQueue.Len() == 0 {
+ assert.EqualValues(t, 0, fnd2.pendingQueue.Len(), "value received did not match correct value2")
+ }
}
- assert.EqualValues(t, 0, fnd2.pendingQueue.Len(), "value received did not match correct value")
}
