mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
htlcswitch: add new mailBox abstraction to the package
In this commit, we add a new abstraction to the package: the mailBox. The mailBox is a non-blocking, concurrent safe, in-order queue for delivering messages to a given channelLink instance. With this abstraction in place, we can now allow the switch to no longer launch a new goroutine for each forwarded HTLC, or instantiated user payment.
This commit is contained in:
100
htlcswitch/mailbox_test.go
Normal file
100
htlcswitch/mailbox_test.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package htlcswitch
|
||||
|
||||
import (
|
||||
prand "math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
)
|
||||
|
||||
// TestMailBoxCouriers tests that both aspects of the mailBox struct works
|
||||
// properly. Both packets and messages should be able to added to each
|
||||
// respective mailbox concurrently, and also messages/packets should also be
|
||||
// able to be received concurrently.
|
||||
func TestMailBoxCouriers(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// First, we'll create new instance of the current default mailbox
|
||||
// type.
|
||||
mailBox := newMemoryMailBox()
|
||||
mailBox.Start()
|
||||
defer mailBox.Stop()
|
||||
|
||||
// We'll be adding 10 message of both types to the mailbox.
|
||||
const numPackets = 10
|
||||
|
||||
// We'll add a set of random packets to the mailbox.
|
||||
sentPackets := make([]*htlcPacket, numPackets)
|
||||
for i := 0; i < numPackets; i++ {
|
||||
pkt := &htlcPacket{
|
||||
dest: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
|
||||
src: lnwire.NewShortChanIDFromInt(uint64(prand.Int63())),
|
||||
amount: lnwire.MilliSatoshi(prand.Int63()),
|
||||
isObfuscated: i%2 == 0,
|
||||
}
|
||||
sentPackets[i] = pkt
|
||||
|
||||
mailBox.AddPacket(pkt)
|
||||
}
|
||||
|
||||
// Next, we'll do the same, but this time adding wire messages.
|
||||
sentMessages := make([]lnwire.Message, numPackets)
|
||||
for i := 0; i < numPackets; i++ {
|
||||
msg := &lnwire.UpdateAddHTLC{
|
||||
ID: uint64(prand.Int63()),
|
||||
Amount: lnwire.MilliSatoshi(prand.Int63()),
|
||||
}
|
||||
sentMessages[i] = msg
|
||||
|
||||
mailBox.AddMessage(msg)
|
||||
}
|
||||
|
||||
// Now we'll attempt to read back the packets/messages we added to the
|
||||
// mailbox. We'll alternative reading from the message outbox vs the
|
||||
// packet outbox to ensure that they work concurrently properly.
|
||||
recvdPackets := make([]*htlcPacket, 0, numPackets)
|
||||
recvdMessages := make([]lnwire.Message, 0, numPackets)
|
||||
for i := 0; i < numPackets*2; i++ {
|
||||
timeout := time.After(time.Second * 5)
|
||||
if i%2 == 0 {
|
||||
select {
|
||||
case <-timeout:
|
||||
t.Fatalf("didn't recv pkt after timeout")
|
||||
case pkt := <-mailBox.PacketOutBox():
|
||||
recvdPackets = append(recvdPackets, pkt)
|
||||
}
|
||||
} else {
|
||||
select {
|
||||
case <-timeout:
|
||||
t.Fatalf("didn't recv message after timeout")
|
||||
case msg := <-mailBox.MessageOutBox():
|
||||
recvdMessages = append(recvdMessages, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The number of messages/packets we sent, and the number we received
|
||||
// should match exactly.
|
||||
if len(sentPackets) != len(recvdPackets) {
|
||||
t.Fatalf("expected %v packets instead got %v", len(sentPackets),
|
||||
len(recvdPackets))
|
||||
}
|
||||
if len(sentMessages) != len(recvdMessages) {
|
||||
t.Fatalf("expected %v messages instead got %v", len(sentMessages),
|
||||
len(recvdMessages))
|
||||
}
|
||||
|
||||
// Additionally, the set of packets should match exactly, as we should
|
||||
// have received the packets int he exact same ordering that we added.
|
||||
if !reflect.DeepEqual(sentPackets, recvdPackets) {
|
||||
t.Fatalf("recvd packets mismatched: expected %v, got %v",
|
||||
spew.Sdump(sentPackets), spew.Sdump(recvdPackets))
|
||||
}
|
||||
if !reflect.DeepEqual(recvdMessages, recvdMessages) {
|
||||
t.Fatalf("recvd messages mismatched: expected %v, got %v",
|
||||
spew.Sdump(sentMessages), spew.Sdump(recvdMessages))
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user