lntest: refactor WaitForBlockchainSync

This commit is contained in:
yyforyongyu 2020-11-21 19:52:50 +08:00
parent a215c55186
commit 96c3a64316
No known key found for this signature in database
GPG Key ID: 9BCD95C4FF296868

@ -1304,50 +1304,31 @@ func (hn *HarnessNode) WaitForNetworkChannelClose(ctx context.Context,
} }
} }
// WaitForBlockchainSync will block until the target nodes has fully // WaitForBlockchainSync waits for the target node to be fully synchronized with
// synchronized with the blockchain. If the passed context object has a set // the blockchain. If the passed context object has a set timeout, it will
// timeout, then the goroutine will continually poll until the timeout has // continually poll until the timeout has elapsed. In the case that the chain
// elapsed. In the case that the chain isn't synced before the timeout is up, // isn't synced before the timeout is up, this function will return an error.
// then this function will return an error.
func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error { func (hn *HarnessNode) WaitForBlockchainSync(ctx context.Context) error {
errChan := make(chan error, 1) ticker := time.NewTicker(time.Millisecond * 100)
retryDelay := time.Millisecond * 100 defer ticker.Stop()
go func() {
for { for {
select { resp, err := hn.GetInfo(ctx, &lnrpc.GetInfoRequest{})
case <-ctx.Done():
case <-hn.quit:
return
default:
}
getInfoReq := &lnrpc.GetInfoRequest{}
getInfoResp, err := hn.GetInfo(ctx, getInfoReq)
if err != nil { if err != nil {
errChan <- err return err
return
} }
if getInfoResp.SyncedToChain { if resp.SyncedToChain {
errChan <- nil return nil
return
} }
select { select {
case <-ctx.Done(): case <-ctx.Done():
return return fmt.Errorf("timeout while waiting for " +
case <-time.After(retryDelay): "blockchain sync")
}
}
}()
select {
case <-hn.quit: case <-hn.quit:
return nil return nil
case err := <-errChan: case <-ticker.C:
return err }
case <-ctx.Done():
return fmt.Errorf("timeout while waiting for blockchain sync")
} }
} }