spoc-bot-vr/queue_test.go

43 lines
895 B
Go

package main
import (
"context"
"strconv"
"testing"
"time"
)
func TestQueue(t *testing.T) {
ctx, can := context.WithTimeout(context.Background(), time.Second*10)
defer can()
driver, _ := NewDriver(ctx, "/tmp/f.db")
q, err := NewQueue(ctx, driver)
if err != nil {
t.Fatal(err)
}
t.Run("enqueue", func(t *testing.T) {
for i := 0; i < 39; i++ {
if err := q.Enqueue(ctx, []byte(strconv.Itoa(i))); err != nil {
t.Fatal(i, err)
}
}
})
t.Run("syn ack", func(t *testing.T) {
found := map[string]struct{}{}
for i := 0; i < 39; i++ {
if reservation, b, err := q.Syn(ctx); err != nil {
t.Fatal(i, "syn err", err)
} else if _, ok := found[string(b)]; ok {
t.Errorf("syn'd %q twice (%+v)", b, found)
} else if err := q.Ack(ctx, reservation); err != nil {
t.Fatal(i, "failed to ack", err)
} else {
found[string(b)] = struct{}{}
}
}
})
}