htlcswitch: add packet queue

In this commit pending packet queue have been added. This queue
consumes the htlc packet, store it inside the golang list and send it
to the pending channel upon release notification.
This commit is contained in:
Andrey Samokhvalov
2017-05-24 17:34:35 +03:00
committed by Olaoluwa Osuntokun
parent 06946f3911
commit 22d90d6b35
2 changed files with 160 additions and 0 deletions

43
htlcswitch/queue_test.go Normal file
View File

@@ -0,0 +1,43 @@
package htlcswitch
import (
"reflect"
"testing"
"time"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/roasbeef/btcutil"
)
// 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) {
q := newWaitingQueue()
a := make([]btcutil.Amount, 1000)
for i := 0; i < len(a); i++ {
a[i] = btcutil.Amount(i)
q.consume(&htlcPacket{
amount: btcutil.Amount(i),
htlc: &lnwire.UpdateAddHTLC{},
})
}
var b []btcutil.Amount
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")
}
}