improve flakiness of a few tests

This commit is contained in:
whyrusleeping 2020-08-25 15:07:10 -07:00
parent 14cda27e9f
commit d0585ca7bb
3 changed files with 35 additions and 7 deletions

View File

@ -39,7 +39,7 @@ func (ts *testSuite) testMining(t *testing.T) {
require.NoError(t, err)
require.Equal(t, abi.ChainEpoch(2), h1.Height())
err = sn[0].MineOne(ctx, MineNext)
MineUntilBlock(ctx, t, sn[0], nil)
require.NoError(t, err)
<-newHeads
@ -70,7 +70,7 @@ func (ts *testSuite) testMiningReal(t *testing.T) {
require.NoError(t, err)
require.Equal(t, abi.ChainEpoch(2), h1.Height())
err = sn[0].MineOne(ctx, MineNext)
MineUntilBlock(ctx, t, sn[0], nil)
require.NoError(t, err)
<-newHeads
@ -79,7 +79,7 @@ func (ts *testSuite) testMiningReal(t *testing.T) {
require.NoError(t, err)
require.Equal(t, abi.ChainEpoch(3), h2.Height())
err = sn[0].MineOne(ctx, MineNext)
MineUntilBlock(ctx, t, sn[0], nil)
require.NoError(t, err)
<-newHeads

View File

@ -8,6 +8,7 @@ import (
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/miner"
)
func SendFunds(ctx context.Context, t *testing.T, sender TestNode, addr address.Address, amount abi.TokenAmount) {
@ -34,3 +35,30 @@ func SendFunds(ctx context.Context, t *testing.T, sender TestNode, addr address.
t.Fatal("did not successfully send money")
}
}
func MineUntilBlock(ctx context.Context, t *testing.T, sn TestStorageNode, cb func()) {
for i := 0; i < 1000; i++ {
var success bool
var err error
wait := make(chan struct{})
sn.MineOne(ctx, miner.MineReq{
Done: func(win bool, e error) {
success = win
err = e
wait <- struct{}{}
},
})
<-wait
if err != nil {
t.Fatal(err)
}
if success {
if cb != nil {
cb()
}
return
}
t.Log("did not mine block, trying again", i)
}
t.Fatal("failed to mine 1000 times in a row...")
}

View File

@ -270,13 +270,13 @@ func Builder(t *testing.T, nFull int, storage []test.StorageMiner) ([]test.TestN
var wait sync.Mutex
wait.Lock()
_ = storers[0].MineOne(ctx, miner2.MineReq{Done: func(bool, error) {
test.MineUntilBlock(ctx, t, storers[0], func() {
wait.Unlock()
}})
})
wait.Lock()
_ = storers[0].MineOne(ctx, miner2.MineReq{Done: func(bool, error) {
test.MineUntilBlock(ctx, t, storers[0], func() {
wait.Unlock()
}})
})
wait.Lock()
}