queue/queue_test.go

105 lines
1.7 KiB
Go
Raw Normal View History

2021-10-31 17:55:40 +02:00
package queue
import (
"log"
"sync"
"testing"
"time"
)
func TestPut(t *testing.T) {
q := NewQueue()
2021-11-01 23:00:17 +02:00
q.Put(1)
if q.Get().(int) != 1 {
2021-10-31 17:55:40 +02:00
t.Fatal("wrong item in queue")
}
}
func TestPutMultiple(t *testing.T) {
q := NewQueue()
2021-11-01 23:00:17 +02:00
q.Put(1)
q.Put(2)
first, second := q.Get().(int), q.Get().(int)
2021-10-31 17:55:40 +02:00
if first != 1 || second != 2 {
t.Fatal("wrong item in queue or wrong order")
}
}
func TestEmptyGet(t *testing.T) {
result := func() chan Item {
q := NewQueue()
c := make(chan Item)
go func() { c <- q.Get() }()
return c
}
select {
case <-result():
log.Fatal("empty queue should block")
case <-time.After(time.Millisecond):
}
}
func TestGetMany(t *testing.T) {
q := NewQueue()
result2 := func() chan []Item {
c := make(chan []Item)
go func() { c <- q.GetMany(2) }()
return c
}
2021-11-01 23:00:17 +02:00
q.Put(1)
2021-10-31 17:55:40 +02:00
select {
case <-result2():
log.Fatal("GetMany should block if not enough items in queue")
case <-time.After(time.Millisecond):
}
// this call unblocks first GetMany call and empties queue
2021-11-01 23:00:17 +02:00
q.Put(2)
2021-10-31 17:55:40 +02:00
// Put enough items in queue for result2 not to block
2021-11-01 23:00:17 +02:00
q.Put(3)
q.Put(4)
2021-10-31 17:55:40 +02:00
select {
case res := <-result2():
2021-11-01 23:00:17 +02:00
third, fourth := res[0].(int), res[1].(int)
2021-10-31 17:55:40 +02:00
if third != 3 || fourth != 4 {
t.Fatal("wrong item in queue or wrong order")
}
case <-time.After(time.Millisecond):
log.Fatal("GetMany shouldn't block when queue has enough items")
}
}
func TestConcurrent(t *testing.T) {
q := NewQueue()
wg := sync.WaitGroup{}
sum := 0
wg.Add(2000)
for i := 0; i < 1000; i++ {
go func(i int) {
defer wg.Done()
2021-11-01 23:00:17 +02:00
q.Put(i)
2021-10-31 17:55:40 +02:00
}(i)
}
for i := 0; i < 1000; i++ {
go func() {
defer wg.Done()
2021-11-01 23:00:17 +02:00
sum += q.Get().(int)
2021-10-31 17:55:40 +02:00
}()
}
wg.Wait()
if sum != 1000*(0+999)/2 {
log.Fatalf("data race. Sum: %v", sum)
}
}