package queue_test
import (
"testing"
"github.com/lightningnetwork/lnd/queue"
)
func testQueueAddDrain(t *testing.T, size, numStart, numStop, numAdd, numDrain int) {
t.Helper()
queue := queue.NewConcurrentQueue(size)
for i := 0; i < numStart; i++ {
queue.Start()
}
for i := 0; i < numStop; i++ {
defer queue.Stop()
}
for i := 0; i < numAdd; i++ {
queue.ChanIn() <- i
}
for i := 0; i < numDrain; i++ {
item := <-queue.ChanOut()
if i != item.(int) {
t.Fatalf("Dequeued wrong value: expected %d, got %d",
i, item.(int))
}
}
}
func TestConcurrentQueue(t *testing.T) {
t.Parallel()
testQueueAddDrain(t, 100, 1, 1, 1000, 1000)
}
func TestConcurrentQueueEarlyStop(t *testing.T) {
t.Parallel()
testQueueAddDrain(t, 100, 1, 1, 1000, 500)
}
func TestConcurrentQueueIdempotentStart(t *testing.T) {
t.Parallel()
testQueueAddDrain(t, 100, 10, 1, 1000, 1000)
}
func TestConcurrentQueueIdempotentStop(t *testing.T) {
t.Parallel()
testQueueAddDrain(t, 100, 1, 10, 1000, 1000)
}
func TestQueueCloseIncoming(t *testing.T) {
t.Parallel()
queue := queue.NewConcurrentQueue(10)
queue.Start()
queue.ChanIn() <- 1
close(queue.ChanIn())
item := <-queue.ChanOut()
if item.(int) != 1 {
t.Fatalf("unexpected item")
}
_, ok := <-queue.ChanOut()
if ok {
t.Fatalf("expected outgoing channel being closed")
}
}