Skip to content

Commit 07437f6

Browse files
committed
test: update the ConnectPeer framework method to block until connect
This commit modifies the ConnectPeer method on the testing framework to block (with a timeout) until the target peer is actually detected as being connected. This was added as the peer connection logic was made to be more asynchronous in a prior commit.
1 parent e43d1dd commit 07437f6

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

networktest.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,11 @@ func (n *networkHarness) NewNode(extraArgs []string) (*lightningNode, error) {
786786
}
787787

788788
// ConnectNodes establishes an encrypted+authenticated p2p connection from node
789-
// a towards node b.
789+
// a towards node b. The function will return a non-nil error if the connection
790+
// was unable to be established.
791+
//
792+
// NOTE: This function may block for up to 15-seconds as it will not return
793+
// until the new connection is detected as being known to both nodes.
790794
func (n *networkHarness) ConnectNodes(ctx context.Context, a, b *lightningNode) error {
791795
bobInfo, err := b.GetInfo(ctx, &lnrpc.GetInfoRequest{})
792796
if err != nil {
@@ -799,8 +803,32 @@ func (n *networkHarness) ConnectNodes(ctx context.Context, a, b *lightningNode)
799803
Host: b.p2pAddr,
800804
},
801805
}
802-
_, err = a.ConnectPeer(ctx, req)
803-
return err
806+
if _, err := a.ConnectPeer(ctx, req); err != nil {
807+
return err
808+
}
809+
810+
timeout := time.After(time.Second * 15)
811+
for {
812+
813+
select {
814+
case <-timeout:
815+
return fmt.Errorf("peers not connected within 15 seconds")
816+
default:
817+
}
818+
819+
// If node B is seen in the ListPeers response from node A,
820+
// then we can exit early as the connection has been fully
821+
// established.
822+
resp, err := a.ListPeers(ctx, &lnrpc.ListPeersRequest{})
823+
if err != nil {
824+
return err
825+
}
826+
for _, peer := range resp.Peers {
827+
if peer.PubKey == b.PubKeyStr {
828+
return nil
829+
}
830+
}
831+
}
804832
}
805833

806834
// RestartNode attempts to restart a lightning node by shutting it down

0 commit comments

Comments
 (0)