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-23 01:54:10 +03:00
|
|
|
q := newPacketQueue()
|
|
|
|
q.Start()
|
|
|
|
defer q.Stop()
|
2017-05-24 17:34:35 +03:00
|
|
|
|
2017-09-23 01:54:10 +03:00
|
|
|
const numPkts = 1000
|
|
|
|
a := make([]lnwire.MilliSatoshi, numPkts)
|
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-08-22 09:36:43 +03:00
|
|
|
a[i] = lnwire.MilliSatoshi(i)
|
2017-09-23 01:54:10 +03:00
|
|
|
q.AddPkt(&htlcPacket{
|
2017-08-22 09:36:43 +03:00
|
|
|
amount: lnwire.MilliSatoshi(i),
|
2017-05-24 17:34:35 +03:00
|
|
|
htlc: &lnwire.UpdateAddHTLC{},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
var b []lnwire.MilliSatoshi
|
2017-09-23 01:54:10 +03:00
|
|
|
for i := 0; i < numPkts; i++ {
|
2017-05-24 17:34:35 +03:00
|
|
|
select {
|
2017-09-23 01:54:10 +03:00
|
|
|
case packet := <-q.outgoingPkts:
|
2017-05-24 17:34:35 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|