lnd_test: fix channel event subscription test flake.

This flake was caused by the rpcserver receiving a CloseChannel request
before Alice's channel event subscription request, causing Alice to miss one
notification. As a result, we move Alice's subscription to the beginning of the
test.

Additionally, we add a check to ensure the opening notifications are
received in the right order.
This commit is contained in:
Valentine Wallace 2019-02-11 17:23:47 -08:00
parent 9d213948ba
commit 21c685d530
No known key found for this signature in database
GPG Key ID: F88EC43B95E601B8

@ -5972,10 +5972,13 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe
amount = lnd.MaxBtcFundingAmount amount = lnd.MaxBtcFundingAmount
) )
// Let Bob subscribe to channel notifications. // Subscribe Bob and Alice to channel event notifications.
bobChanSub := subscribeChannelNotifications(ctxb, t, net.Bob) bobChanSub := subscribeChannelNotifications(ctxb, t, net.Bob)
defer close(bobChanSub.quit) defer close(bobChanSub.quit)
aliceChanSub := subscribeChannelNotifications(ctxb, t, net.Alice)
defer close(aliceChanSub.quit)
// Open the channel between Alice and Bob, asserting that the // Open the channel between Alice and Bob, asserting that the
// channel has been properly open on-chain. // channel has been properly open on-chain.
chanPoints := make([]*lnrpc.ChannelPoint, numChannels) chanPoints := make([]*lnrpc.ChannelPoint, numChannels)
@ -5989,31 +5992,52 @@ func testBasicChannelCreationAndUpdates(net *lntest.NetworkHarness, t *harnessTe
) )
} }
// Since each of the channels just became open, Bob should we receive an // Since each of the channels just became open, Bob and Alice should
// open and an active notification for each channel. // each receive an open and an active notification for each channel.
var numChannelUpds int var numChannelUpds int
for numChannelUpds < 2*numChannels { const totalNtfns = 2 * numChannels
select { verifyOpenUpdatesReceived := func(sub channelSubscription) error {
case update := <-bobChanSub.updateChan: numChannelUpds = 0
switch update.Type { for numChannelUpds < totalNtfns {
case lnrpc.ChannelEventUpdate_ACTIVE_CHANNEL: select {
case lnrpc.ChannelEventUpdate_OPEN_CHANNEL: case update := <-sub.updateChan:
default: switch update.Type {
t.Fatalf("update type mismatch: expected open or active "+ case lnrpc.ChannelEventUpdate_ACTIVE_CHANNEL:
"channel notification, got: %v", update.Type) if numChannelUpds%2 != 1 {
return fmt.Errorf("expected open" +
"channel ntfn, got active " +
"channel ntfn instead")
}
case lnrpc.ChannelEventUpdate_OPEN_CHANNEL:
if numChannelUpds%2 != 0 {
return fmt.Errorf("expected active" +
"channel ntfn, got open" +
"channel ntfn instead")
}
default:
return fmt.Errorf("update type mismatch: "+
"expected open or active channel "+
"notification, got: %v",
update.Type)
}
numChannelUpds++
case <-time.After(time.Second * 10):
return fmt.Errorf("timeout waiting for channel "+
"notifications, only received %d/%d "+
"chanupds", numChannelUpds,
totalNtfns)
} }
numChannelUpds++
case <-time.After(time.Second * 10):
t.Fatalf("timeout waiting for channel notifications, "+
"only received %d/%d chanupds", numChannelUpds,
numChannels)
} }
return nil
} }
// Subscribe Alice to channel updates so we can test that both remote if err := verifyOpenUpdatesReceived(bobChanSub); err != nil {
// and local force close notifications are received correctly. t.Fatalf("error verifying open updates: %v", err)
aliceChanSub := subscribeChannelNotifications(ctxb, t, net.Alice) }
defer close(aliceChanSub.quit) if err := verifyOpenUpdatesReceived(aliceChanSub); err != nil {
t.Fatalf("error verifying open updates: %v", err)
}
// Close the channel between Alice and Bob, asserting that the channel // Close the channel between Alice and Bob, asserting that the channel
// has been properly closed on-chain. // has been properly closed on-chain.