lnd/htlcswitch/queue_test.go
Olaoluwa Osuntokun 6f5ef249e4
htlcswitch: re-write link's packet overflow queue for readability+extensibility
This commit completes a full re-write of the link’s packet overflow
queue with the goals of the making the code itself more understandable
and also allowing it to be more extensible in the future with various
algorithms for handling HTLC congestion avoidance and persistent queue
back pressure.

The new design is simpler and consumes much less coroutines (no longer
a new goroutine for each active HLTC). We now implement a simple
synchronized queue using a standard condition variable.
2017-09-22 15:54:16 -07:00

46 lines
911 B
Go

package htlcswitch
import (
"reflect"
"testing"
"time"
"github.com/lightningnetwork/lnd/lnwire"
)
// TestWaitingQueueThreadSafety test the thread safety properties of the
// waiting queue, by executing methods in separate goroutines which operates
// with the same data.
func TestWaitingQueueThreadSafety(t *testing.T) {
t.Parallel()
q := newPacketQueue()
q.Start()
defer q.Stop()
const numPkts = 1000
a := make([]lnwire.MilliSatoshi, numPkts)
for i := 0; i < numPkts; i++ {
a[i] = lnwire.MilliSatoshi(i)
q.AddPkt(&htlcPacket{
amount: lnwire.MilliSatoshi(i),
htlc: &lnwire.UpdateAddHTLC{},
})
}
var b []lnwire.MilliSatoshi
for i := 0; i < numPkts; i++ {
select {
case packet := <-q.outgoingPkts:
b = append(b, packet.amount)
case <-time.After(2 * time.Second):
t.Fatal("timeout")
}
}
if !reflect.DeepEqual(b, a) {
t.Fatal("wrong order of the objects")
}
}