2017-05-24 16:34:35 +02:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestWaitingQueueThreadSafety test the thread safety properties of the
|
2017-09-23 00:54:10 +02:00
|
|
|
// waiting queue, by executing methods in separate goroutines which operates
|
2017-05-24 16:34:35 +02:00
|
|
|
// with the same data.
|
|
|
|
func TestWaitingQueueThreadSafety(t *testing.T) {
|
2017-06-17 00:41:42 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-25 21:47:28 +02:00
|
|
|
const numPkts = 1000
|
|
|
|
|
|
|
|
q := newPacketQueue(numPkts)
|
2017-09-23 00:54:10 +02:00
|
|
|
q.Start()
|
|
|
|
defer q.Stop()
|
2017-05-24 16:34:35 +02:00
|
|
|
|
2017-10-30 20:21:07 +01:00
|
|
|
a := make([]uint64, numPkts)
|
2017-09-23 00:54:10 +02:00
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-10-30 20:21:07 +01:00
|
|
|
a[i] = uint64(i)
|
2017-09-23 00:54:10 +02:00
|
|
|
q.AddPkt(&htlcPacket{
|
2017-10-30 20:21:07 +01:00
|
|
|
incomingHTLCID: a[i],
|
|
|
|
htlc: &lnwire.UpdateAddHTLC{},
|
2017-05-24 16:34:35 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:55:13 +02:00
|
|
|
// The reported length of the queue should be the exact number of
|
|
|
|
// packets we added above.
|
|
|
|
queueLength := q.Length()
|
|
|
|
if queueLength != numPkts {
|
|
|
|
t.Fatalf("queue has wrong length: expected %v, got %v", numPkts,
|
|
|
|
queueLength)
|
|
|
|
}
|
|
|
|
|
2017-10-30 20:21:07 +01:00
|
|
|
var b []uint64
|
2017-09-23 00:54:10 +02:00
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-09-25 21:47:28 +02:00
|
|
|
q.SignalFreeSlot()
|
|
|
|
|
2017-05-24 16:34:35 +02:00
|
|
|
select {
|
2017-09-23 00:54:10 +02:00
|
|
|
case packet := <-q.outgoingPkts:
|
2017-10-30 20:21:07 +01:00
|
|
|
b = append(b, packet.incomingHTLCID)
|
2017-05-24 16:34:35 +02:00
|
|
|
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 01:55:13 +02:00
|
|
|
// The length of the queue should be zero at this point.
|
|
|
|
time.Sleep(time.Millisecond * 50)
|
|
|
|
queueLength = q.Length()
|
|
|
|
if queueLength != 0 {
|
|
|
|
t.Fatalf("queue has wrong length: expected %v, got %v", 0,
|
|
|
|
queueLength)
|
|
|
|
}
|
|
|
|
|
2017-05-24 16:34:35 +02:00
|
|
|
if !reflect.DeepEqual(b, a) {
|
|
|
|
t.Fatal("wrong order of the objects")
|
|
|
|
}
|
|
|
|
}
|