Merge pull request #3298 from filecoin-project/fix/flaky-mining-tests

improve flakiness of a few tests
This commit is contained in:
Łukasz Magiera 2020-08-26 16:07:52 +02:00 committed by GitHub
commit 6d328c0a71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 11 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()
}
@ -419,13 +419,13 @@ func MockSbBuilder(t *testing.T, nFull int, storage []test.StorageMiner) ([]test
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()
}