itest: refactor assertNumConnections to use wait.NoError
This commit refactors the function assertNumConnections to use wait.NoError. Prior to this commit, `make lint` will fail on this function. While fixing it, it's noticed that wait.NoError suits the case so it's refactored to use it.
This commit is contained in:
parent
0612ced087
commit
b657a977e3
@ -543,56 +543,49 @@ func assertNumOpenChannelsPending(ctxt context.Context, t *harnessTest,
|
|||||||
}
|
}
|
||||||
|
|
||||||
// assertNumConnections asserts number current connections between two peers.
|
// assertNumConnections asserts number current connections between two peers.
|
||||||
// TODO(yy): refactor to use wait.
|
|
||||||
func assertNumConnections(t *harnessTest, alice, bob *lntest.HarnessNode,
|
func assertNumConnections(t *harnessTest, alice, bob *lntest.HarnessNode,
|
||||||
expected int) {
|
expected int) {
|
||||||
ctxb := context.Background()
|
ctxb := context.Background()
|
||||||
|
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
||||||
|
|
||||||
const nPolls = 10
|
err := wait.NoError(func() error {
|
||||||
|
aNumPeers, err := alice.ListPeers(
|
||||||
tick := time.NewTicker(300 * time.Millisecond)
|
ctxt, &lnrpc.ListPeersRequest{},
|
||||||
defer tick.Stop()
|
)
|
||||||
|
if err != nil {
|
||||||
for i := nPolls - 1; i >= 0; i-- {
|
return fmt.Errorf(
|
||||||
select {
|
"unable to fetch %s's node (%v) list peers %v",
|
||||||
case <-tick.C:
|
alice.Name(), alice.NodeID, err,
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
)
|
||||||
aNumPeers, err := alice.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to fetch alice's node (%v) list peers %v",
|
|
||||||
alice.NodeID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
|
||||||
bNumPeers, err := bob.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to fetch bob's node (%v) list peers %v",
|
|
||||||
bob.NodeID, err)
|
|
||||||
}
|
|
||||||
if len(aNumPeers.Peers) != expected {
|
|
||||||
// Continue polling if this is not the final
|
|
||||||
// loop.
|
|
||||||
if i > 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t.Fatalf("number of peers connected to alice is incorrect: "+
|
|
||||||
"expected %v, got %v", expected, len(aNumPeers.Peers))
|
|
||||||
}
|
|
||||||
if len(bNumPeers.Peers) != expected {
|
|
||||||
// Continue polling if this is not the final
|
|
||||||
// loop.
|
|
||||||
if i > 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
t.Fatalf("number of peers connected to bob is incorrect: "+
|
|
||||||
"expected %v, got %v", expected, len(bNumPeers.Peers))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Alice and Bob both have the required number of
|
|
||||||
// peers, stop polling and return to caller.
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
bNumPeers, err := bob.ListPeers(ctxt, &lnrpc.ListPeersRequest{})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"unable to fetch %s's node (%v) list peers %v",
|
||||||
|
bob.Name(), bob.NodeID, err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(aNumPeers.Peers) != expected {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"number of peers connected to %s is "+
|
||||||
|
"incorrect: expected %v, got %v",
|
||||||
|
alice.Name(), expected, len(aNumPeers.Peers),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if len(bNumPeers.Peers) != expected {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"number of peers connected to %s is "+
|
||||||
|
"incorrect: expected %v, got %v",
|
||||||
|
bob.Name(), expected, len(bNumPeers.Peers),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
|
||||||
|
}, defaultTimeout)
|
||||||
|
require.NoError(t.t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// shutdownAndAssert shuts down the given node and asserts that no errors
|
// shutdownAndAssert shuts down the given node and asserts that no errors
|
||||||
|
Loading…
Reference in New Issue
Block a user