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
|
|
|
|
// waiting queue, by executing methods in seprate goroutines which operates
|
|
|
|
// with the same data.
|
|
|
|
func TestWaitingQueueThreadSafety(t *testing.T) {
|
2017-06-17 01:41:42 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-05-24 17:34:35 +03:00
|
|
|
q := newWaitingQueue()
|
|
|
|
|
2017-08-22 09:36:43 +03:00
|
|
|
a := make([]lnwire.MilliSatoshi, 1000)
|
2017-05-24 17:34:35 +03:00
|
|
|
for i := 0; i < len(a); i++ {
|
2017-08-22 09:36:43 +03:00
|
|
|
a[i] = lnwire.MilliSatoshi(i)
|
2017-05-24 17:34:35 +03:00
|
|
|
q.consume(&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-05-24 17:34:35 +03:00
|
|
|
for i := 0; i < len(a); i++ {
|
|
|
|
q.release()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case packet := <-q.pending:
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|