2017-05-24 17:34:35 +03:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestWaitingQueueThreadSafety test the thread safety properties of the
|
2017-09-23 01:54:10 +03:00
|
|
|
// waiting queue, by executing methods in separate goroutines which operates
|
2017-05-24 17:34:35 +03:00
|
|
|
// with the same data.
|
|
|
|
func TestWaitingQueueThreadSafety(t *testing.T) {
|
2017-06-17 01:41:42 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-09-25 22:47:28 +03:00
|
|
|
const numPkts = 1000
|
|
|
|
|
|
|
|
q := newPacketQueue(numPkts)
|
2017-09-23 01:54:10 +03:00
|
|
|
q.Start()
|
|
|
|
defer q.Stop()
|
2017-05-24 17:34:35 +03:00
|
|
|
|
2017-10-30 22:21:07 +03:00
|
|
|
a := make([]uint64, numPkts)
|
2017-09-23 01:54:10 +03:00
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-10-30 22:21:07 +03:00
|
|
|
a[i] = uint64(i)
|
2017-09-23 01:54:10 +03:00
|
|
|
q.AddPkt(&htlcPacket{
|
2017-10-30 22:21:07 +03:00
|
|
|
incomingHTLCID: a[i],
|
|
|
|
htlc: &lnwire.UpdateAddHTLC{},
|
2017-05-24 17:34:35 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-26 02:55:13 +03: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 22:21:07 +03:00
|
|
|
var b []uint64
|
2017-09-23 01:54:10 +03:00
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-09-25 22:47:28 +03:00
|
|
|
q.SignalFreeSlot()
|
|
|
|
|
2017-05-24 17:34:35 +03:00
|
|
|
select {
|
2017-09-23 01:54:10 +03:00
|
|
|
case packet := <-q.outgoingPkts:
|
2017-10-30 22:21:07 +03:00
|
|
|
b = append(b, packet.incomingHTLCID)
|
2017-05-24 17:34:35 +03:00
|
|
|
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Fatal("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-26 02:55:13 +03: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 17:34:35 +03:00
|
|
|
if !reflect.DeepEqual(b, a) {
|
|
|
|
t.Fatal("wrong order of the objects")
|
|
|
|
}
|
|
|
|
}
|