test: ensure blocks are synchronized before opening channels, allow more time for sync payments

Fix bug with synchronizing blockchain by adding several retries. Allow
to launch individual tests. Increase timeout for async payments.

Fixes #213.
This commit is contained in:
BitfuryLightning 2017-06-19 16:53:52 +03:00 committed by Olaoluwa Osuntokun
parent 276a360353
commit e324fe5818
3 changed files with 65 additions and 5 deletions

@ -504,6 +504,7 @@ func (f *fundingManager) handleFundingRequest(fmsg *fundingRequestMsg) {
fndgLog.Errorf("unable to query wallet: %v", err)
return
}
if !isSynced {
errMsg := &lnwire.Error{
ChanID: fmsg.msg.PendingChannelID,

@ -82,7 +82,7 @@ func (h *harnessTest) RunTestCase(testCase *testCase, net *networkHarness) {
}()
testCase.test(net, h)
h.t.Logf("Passed: (%v)", h.testCase.name)
return
}
@ -2823,15 +2823,16 @@ func testBidirectionalAsyncPayments(net *networkHarness, t *harnessTest) {
// Wait for Alice and Bob receive their payments, and throw and error
// if something goes wrong.
maxTime := 20 * time.Second
for i := 0; i < 2; i++ {
select {
case err := <-errChan:
if err != nil {
t.Fatalf(err.Error())
}
case <-time.After(time.Second * 7):
t.Fatalf("waiting for payments to finish too long " +
"(7s)")
case <-time.After(time.Second * maxTime):
t.Fatalf("waiting for payments to finish too long "+
"(%v)", maxTime)
}
}
@ -3038,7 +3039,14 @@ func TestLightningNetworkDaemon(t *testing.T) {
t.Logf("Running %v integration tests", len(testsCases))
for _, testCase := range testsCases {
ht.RunTestCase(testCase, lndHarness)
success := t.Run(testCase.name, func(t1 *testing.T) {
ht := newHarnessTest(t1)
ht.RunTestCase(testCase, lndHarness)
})
// Stop at the first failure. Mimic behavior of original test
if !success {
break
}
}
close(testsFin)

@ -574,6 +574,41 @@ func (l *lightningNode) WaitForNetworkChannelClose(ctx context.Context,
}
}
// WaitForBlockchainSync will block until node synchronizes its blockchain
func (l *lightningNode) WaitForBlockchainSync(ctx context.Context) error {
errChan := make(chan error, 1)
retryDelay := time.Millisecond * 100
go func() {
for {
select {
case <-ctx.Done():
break
default:
}
getInfoReq := &lnrpc.GetInfoRequest{}
getInfoResp, err := l.GetInfo(ctx, getInfoReq)
if err != nil {
errChan <- err
break
}
if getInfoResp.SyncedToChain {
errChan <- nil
}
select {
case <-ctx.Done():
break
case <-time.After(retryDelay):
}
}
}()
select {
case err := <-errChan:
return err
case <-ctx.Done():
return fmt.Errorf("Timeout while waiting for blockchain sync")
}
}
// networkHarness is an integration testing harness for the lightning network.
// The harness by default is created with two active nodes on the network:
// Alice and Bob.
@ -972,6 +1007,14 @@ func (n *networkHarness) OpenChannel(ctx context.Context,
srcNode, destNode *lightningNode, amt btcutil.Amount,
pushAmt btcutil.Amount, numConfs uint32) (lnrpc.Lightning_OpenChannelClient, error) {
// Wait until srcNode and destNode have blockchain synced
if err := srcNode.WaitForBlockchainSync(ctx); err != nil {
return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err)
}
if err := destNode.WaitForBlockchainSync(ctx); err != nil {
return nil, fmt.Errorf("Unable to sync destNode chain: %v", err)
}
openReq := &lnrpc.OpenChannelRequest{
NodePubkey: destNode.PubKey[:],
LocalFundingAmount: int64(amt),
@ -1024,6 +1067,14 @@ func (n *networkHarness) OpenPendingChannel(ctx context.Context,
srcNode, destNode *lightningNode, amt btcutil.Amount,
pushAmt btcutil.Amount, numConfs uint32) (*lnrpc.PendingUpdate, error) {
// Wait until srcNode and destNode have blockchain synced
if err := srcNode.WaitForBlockchainSync(ctx); err != nil {
return nil, fmt.Errorf("Unable to sync srcNode chain: %v", err)
}
if err := destNode.WaitForBlockchainSync(ctx); err != nil {
return nil, fmt.Errorf("Unable to sync destNode chain: %v", err)
}
openReq := &lnrpc.OpenChannelRequest{
NodePubkey: destNode.PubKey[:],
LocalFundingAmount: int64(amt),