Merge pull request #4205 from cfromknecht/mailbox-test-fixups

htlcswitch/mailbox_test: use mailboxContext everywhere
This commit is contained in:
Olaoluwa Osuntokun 2020-04-21 16:12:15 -07:00 committed by GitHub
commit 024980a23f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -11,6 +11,8 @@ import (
"github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/lnwire"
) )
const testExpiry = time.Minute
// TestMailBoxCouriers tests that both aspects of the mailBox struct works // TestMailBoxCouriers tests that both aspects of the mailBox struct works
// properly. Both packets and messages should be able to added to each // properly. Both packets and messages should be able to added to each
// respective mailbox concurrently, and also messages/packets should also be // respective mailbox concurrently, and also messages/packets should also be
@ -20,12 +22,8 @@ func TestMailBoxCouriers(t *testing.T) {
// First, we'll create new instance of the current default mailbox // First, we'll create new instance of the current default mailbox
// type. // type.
mailBox := newMemoryMailBox(&mailBoxConfig{ ctx := newMailboxContext(t, time.Now(), testExpiry)
clock: clock.NewDefaultClock(), defer ctx.mailbox.Stop()
expiry: time.Minute,
})
mailBox.Start()
defer mailBox.Stop()
// We'll be adding 10 message of both types to the mailbox. // We'll be adding 10 message of both types to the mailbox.
const numPackets = 10 const numPackets = 10
@ -44,7 +42,7 @@ func TestMailBoxCouriers(t *testing.T) {
} }
sentPackets[i] = pkt sentPackets[i] = pkt
err := mailBox.AddPacket(pkt) err := ctx.mailbox.AddPacket(pkt)
if err != nil { if err != nil {
t.Fatalf("unable to add packet: %v", err) t.Fatalf("unable to add packet: %v", err)
} }
@ -59,7 +57,10 @@ func TestMailBoxCouriers(t *testing.T) {
} }
sentMessages[i] = msg sentMessages[i] = msg
mailBox.AddMessage(msg) err := ctx.mailbox.AddMessage(msg)
if err != nil {
t.Fatalf("unable to add message: %v", err)
}
} }
// Now we'll attempt to read back the packets/messages we added to the // Now we'll attempt to read back the packets/messages we added to the
@ -73,14 +74,14 @@ func TestMailBoxCouriers(t *testing.T) {
select { select {
case <-timeout: case <-timeout:
t.Fatalf("didn't recv pkt after timeout") t.Fatalf("didn't recv pkt after timeout")
case pkt := <-mailBox.PacketOutBox(): case pkt := <-ctx.mailbox.PacketOutBox():
recvdPackets = append(recvdPackets, pkt) recvdPackets = append(recvdPackets, pkt)
} }
} else { } else {
select { select {
case <-timeout: case <-timeout:
t.Fatalf("didn't recv message after timeout") t.Fatalf("didn't recv message after timeout")
case msg := <-mailBox.MessageOutBox(): case msg := <-ctx.mailbox.MessageOutBox():
recvdMessages = append(recvdMessages, msg) recvdMessages = append(recvdMessages, msg)
} }
} }
@ -111,13 +112,19 @@ func TestMailBoxCouriers(t *testing.T) {
// Now that we've received all of the intended msgs/pkts, ack back half // Now that we've received all of the intended msgs/pkts, ack back half
// of the packets. // of the packets.
for _, recvdPkt := range recvdPackets[:halfPackets] { for _, recvdPkt := range recvdPackets[:halfPackets] {
mailBox.AckPacket(recvdPkt.inKey()) ctx.mailbox.AckPacket(recvdPkt.inKey())
} }
// With the packets drained and partially acked, we reset the mailbox, // With the packets drained and partially acked, we reset the mailbox,
// simulating a link shutting down and then coming back up. // simulating a link shutting down and then coming back up.
mailBox.ResetMessages() err := ctx.mailbox.ResetMessages()
mailBox.ResetPackets() if err != nil {
t.Fatalf("unable to reset messages: %v", err)
}
err = ctx.mailbox.ResetPackets()
if err != nil {
t.Fatalf("unable to reset packets: %v", err)
}
// Now, we'll use the same alternating strategy to read from our // Now, we'll use the same alternating strategy to read from our
// mailbox. All wire messages are dropped on startup, but any unacked // mailbox. All wire messages are dropped on startup, but any unacked
@ -130,12 +137,12 @@ func TestMailBoxCouriers(t *testing.T) {
select { select {
case <-timeout: case <-timeout:
t.Fatalf("didn't recv pkt after timeout") t.Fatalf("didn't recv pkt after timeout")
case pkt := <-mailBox.PacketOutBox(): case pkt := <-ctx.mailbox.PacketOutBox():
recvdPackets2 = append(recvdPackets2, pkt) recvdPackets2 = append(recvdPackets2, pkt)
} }
} else { } else {
select { select {
case <-mailBox.MessageOutBox(): case <-ctx.mailbox.MessageOutBox():
t.Fatalf("should not receive wire msg after reset") t.Fatalf("should not receive wire msg after reset")
default: default:
} }
@ -163,18 +170,17 @@ func TestMailBoxCouriers(t *testing.T) {
func TestMailBoxResetAfterShutdown(t *testing.T) { func TestMailBoxResetAfterShutdown(t *testing.T) {
t.Parallel() t.Parallel()
m := newMemoryMailBox(&mailBoxConfig{}) ctx := newMailboxContext(t, time.Now(), time.Second)
m.Start()
// Stop the mailbox, then try to reset the message and packet couriers. // Stop the mailbox, then try to reset the message and packet couriers.
m.Stop() ctx.mailbox.Stop()
err := m.ResetMessages() err := ctx.mailbox.ResetMessages()
if err != ErrMailBoxShuttingDown { if err != ErrMailBoxShuttingDown {
t.Fatalf("expected ErrMailBoxShuttingDown, got: %v", err) t.Fatalf("expected ErrMailBoxShuttingDown, got: %v", err)
} }
err = m.ResetPackets() err = ctx.mailbox.ResetPackets()
if err != ErrMailBoxShuttingDown { if err != ErrMailBoxShuttingDown {
t.Fatalf("expected ErrMailBoxShuttingDown, got: %v", err) t.Fatalf("expected ErrMailBoxShuttingDown, got: %v", err)
} }
@ -375,12 +381,8 @@ func TestMailBoxPacketPrioritization(t *testing.T) {
// First, we'll create new instance of the current default mailbox // First, we'll create new instance of the current default mailbox
// type. // type.
mailBox := newMemoryMailBox(&mailBoxConfig{ ctx := newMailboxContext(t, time.Now(), testExpiry)
clock: clock.NewDefaultClock(), defer ctx.mailbox.Stop()
expiry: time.Minute,
})
mailBox.Start()
defer mailBox.Stop()
const numPackets = 5 const numPackets = 5
@ -418,7 +420,7 @@ func TestMailBoxPacketPrioritization(t *testing.T) {
sentPackets[i] = pkt sentPackets[i] = pkt
err := mailBox.AddPacket(pkt) err := ctx.mailbox.AddPacket(pkt)
if err != nil { if err != nil {
t.Fatalf("failed to add packet: %v", err) t.Fatalf("failed to add packet: %v", err)
} }
@ -435,7 +437,7 @@ func TestMailBoxPacketPrioritization(t *testing.T) {
// or Add2 due to the prioritization between the split queue. // or Add2 due to the prioritization between the split queue.
for i := 0; i < numPackets; i++ { for i := 0; i < numPackets; i++ {
select { select {
case pkt := <-mailBox.PacketOutBox(): case pkt := <-ctx.mailbox.PacketOutBox():
var expPkt *htlcPacket var expPkt *htlcPacket
switch i { switch i {
case 0: case 0:
@ -504,21 +506,19 @@ func TestMailBoxAddExpiry(t *testing.T) {
func TestMailBoxDuplicateAddPacket(t *testing.T) { func TestMailBoxDuplicateAddPacket(t *testing.T) {
t.Parallel() t.Parallel()
mailBox := newMemoryMailBox(&mailBoxConfig{ ctx := newMailboxContext(t, time.Now(), testExpiry)
clock: clock.NewDefaultClock(), ctx.mailbox.Start()
}) defer ctx.mailbox.Stop()
mailBox.Start()
defer mailBox.Stop()
addTwice := func(t *testing.T, pkt *htlcPacket) { addTwice := func(t *testing.T, pkt *htlcPacket) {
// The first add should succeed. // The first add should succeed.
err := mailBox.AddPacket(pkt) err := ctx.mailbox.AddPacket(pkt)
if err != nil { if err != nil {
t.Fatalf("unable to add packet: %v", err) t.Fatalf("unable to add packet: %v", err)
} }
// Adding again with the same incoming circuit key should fail. // Adding again with the same incoming circuit key should fail.
err = mailBox.AddPacket(pkt) err = ctx.mailbox.AddPacket(pkt)
if err != ErrPacketAlreadyExists { if err != ErrPacketAlreadyExists {
t.Fatalf("expected ErrPacketAlreadyExists, got: %v", err) t.Fatalf("expected ErrPacketAlreadyExists, got: %v", err)
} }
@ -548,8 +548,22 @@ func TestMailOrchestrator(t *testing.T) {
// First, we'll create a new instance of our orchestrator. // First, we'll create a new instance of our orchestrator.
mo := newMailOrchestrator(&mailOrchConfig{ mo := newMailOrchestrator(&mailOrchConfig{
clock: clock.NewDefaultClock(), fetchUpdate: func(sid lnwire.ShortChannelID) (
expiry: time.Minute, *lnwire.ChannelUpdate, error) {
return &lnwire.ChannelUpdate{
ShortChannelID: sid,
}, nil
},
forwardPackets: func(_ chan struct{},
pkts ...*htlcPacket) chan error {
// Close the channel immediately so the goroutine
// logging errors can exit.
errChan := make(chan error)
close(errChan)
return errChan
},
clock: clock.NewTestClock(time.Now()),
expiry: testExpiry,
}) })
defer mo.Stop() defer mo.Stop()