From f4f024aff2a62e726a7c29939762504fc2244d5e Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Fri, 22 Dec 2017 13:49:49 +0100 Subject: [PATCH] lnd_test: extract graph topology subscription into own method This commit extracts the launching of a goroutine subscribing to and forwarding graph topology notifications into its own utility method, such that it can be used in other tests as well. --- lnd_test.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/lnd_test.go b/lnd_test.go index a9a0ab72..3cf28b4e 100644 --- a/lnd_test.go +++ b/lnd_test.go @@ -3878,28 +3878,22 @@ out: } } -func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) { - const chanAmt = maxFundingAmount - timeout := time.Duration(time.Second * 5) - ctxb := context.Background() - - // We'll first start by establishing a notification client to Alice which - // will send us notifications upon detected changes in the channel graph. +// subscribeGraphNotifications subscribes to channel graph updates and launches +// a goroutine that forwards these to the returned channel. +func subscribeGraphNotifications(t *harnessTest, ctxb context.Context, + node *lntest.HarnessNode) (chan *lnrpc.GraphTopologyUpdate, chan struct{}) { + // We'll first start by establishing a notification client which will + // send us notifications upon detected changes in the channel graph. req := &lnrpc.GraphTopologySubscription{} - topologyClient, err := net.Alice.SubscribeChannelGraph(ctxb, req) + topologyClient, err := node.SubscribeChannelGraph(ctxb, req) if err != nil { t.Fatalf("unable to create topology client: %v", err) } - // Open a new channel between Alice and Bob. - ctxt, _ := context.WithTimeout(ctxb, timeout) - chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob, - chanAmt, 0) - // We'll launch a goroutine that'll be responsible for proxying all // notifications recv'd from the client into the channel below. quit := make(chan struct{}) - graphUpdates := make(chan *lnrpc.GraphTopologyUpdate, 4) + graphUpdates := make(chan *lnrpc.GraphTopologyUpdate, 20) go func() { for { select { @@ -3916,7 +3910,8 @@ func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) if err == io.EOF { return } else if err != nil { - t.Fatalf("unable to recv graph update: %v", err) + t.Fatalf("unable to recv graph update: %v", + err) } select { @@ -3927,6 +3922,21 @@ func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) } } }() + return graphUpdates, quit +} + +func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest) { + const chanAmt = maxFundingAmount + timeout := time.Duration(time.Second * 5) + ctxb := context.Background() + + // Let Alice subscribe to graph notifications. + graphUpdates, quit := subscribeGraphNotifications(t, ctxb, net.Alice) + + // Open a new channel between Alice and Bob. + ctxt, _ := context.WithTimeout(ctxb, timeout) + chanPoint := openChannelAndAssert(ctxt, t, net, net.Alice, net.Bob, + chanAmt, 0) // The channel opening above should've triggered a few notifications // sent to the notification client. We'll expect two channel updates,