test: use graph notifications to receive node announcement

In this commit, we rewrite the node announcement integration test to no
longer depend on a sleep interval. Instead, we use graph topology
updates in order to be notified exactly when we receive the node
announcement.
This commit is contained in:
Wilmer Paulino 2018-05-24 02:54:12 -04:00
parent d543ff5811
commit d8325e29fd
No known key found for this signature in database
GPG key ID: 6DF57B9F9514972F

View file

@ -5513,15 +5513,17 @@ func testGraphTopologyNotifications(net *lntest.NetworkHarness, t *harnessTest)
// announced to the network and reported in the network graph. // announced to the network and reported in the network graph.
func testNodeAnnouncement(net *lntest.NetworkHarness, t *harnessTest) { func testNodeAnnouncement(net *lntest.NetworkHarness, t *harnessTest) {
ctxb := context.Background() ctxb := context.Background()
aliceUpdates, quit := subscribeGraphNotifications(t, ctxb, net.Alice)
defer close(quit)
ipAddresses := map[string]bool{ advertisedAddrs := []string{
"192.168.1.1:8333": true, "192.168.1.1:8333",
"[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337": true, "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:8337",
} }
var lndArgs []string var lndArgs []string
for address := range ipAddresses { for _, addr := range advertisedAddrs {
lndArgs = append(lndArgs, "--externalip="+address) lndArgs = append(lndArgs, "--externalip="+addr)
} }
dave, err := net.NewNode("Dave", lndArgs) dave, err := net.NewNode("Dave", lndArgs)
@ -5541,43 +5543,49 @@ func testNodeAnnouncement(net *lntest.NetworkHarness, t *harnessTest) {
ctxt, t, net, net.Bob, dave, 1000000, 0, false, ctxt, t, net, net.Bob, dave, 1000000, 0, false,
) )
// When Alice now connects with Dave, Alice will get his node announcement. // When Alice now connects with Dave, Alice will get his node
// announcement.
if err := net.ConnectNodes(ctxb, net.Alice, dave); err != nil { if err := net.ConnectNodes(ctxb, net.Alice, dave); err != nil {
t.Fatalf("unable to connect bob to carol: %v", err) t.Fatalf("unable to connect bob to carol: %v", err)
} }
time.Sleep(time.Second * 1) assertAddrs := func(addrsFound []string, targetAddrs ...string) {
req := &lnrpc.ChannelGraphRequest{} addrs := make(map[string]struct{}, len(addrsFound))
chanGraph, err := net.Alice.DescribeGraph(ctxb, req) for _, addr := range addrsFound {
if err != nil { addrs[addr] = struct{}{}
t.Fatalf("unable to query for alice's routing table: %v", err) }
}
for _, node := range chanGraph.Nodes { for _, addr := range targetAddrs {
if node.PubKey == dave.PubKeyStr { if _, ok := addrs[addr]; !ok {
for _, address := range node.Addresses { t.Fatalf("address %v not found in node "+
addrStr := address.String() "announcement", addr)
// parse the IP address from the string
// representation of the TCPAddr
parts := strings.Split(addrStr, "\"")
if ipAddresses[parts[3]] {
delete(ipAddresses, parts[3])
} else {
if !strings.HasPrefix(parts[3],
"127.0.0.1:") {
t.Fatalf("unexpected IP: %v",
parts[3])
}
}
} }
} }
} }
if len(ipAddresses) != 0 {
t.Fatalf("expected IP addresses not in channel "+ waitForAddrsInUpdate := func(graphUpdates <-chan *lnrpc.GraphTopologyUpdate,
"graph: %v", ipAddresses) nodePubKey string, targetAddrs ...string) {
for {
select {
case graphUpdate := <-graphUpdates:
for _, update := range graphUpdate.NodeUpdates {
if update.IdentityKey == nodePubKey {
assertAddrs(
update.Addresses,
targetAddrs...,
)
return
}
}
case <-time.After(20 * time.Second):
t.Fatalf("did not receive node ann update")
}
}
} }
waitForAddrsInUpdate(aliceUpdates, dave.PubKeyStr, advertisedAddrs...)
// Close the channel between Bob and Dave. // Close the channel between Bob and Dave.
ctxt, _ = context.WithTimeout(ctxb, timeout) ctxt, _ = context.WithTimeout(ctxb, timeout)
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false) closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint, false)