43 lines
906 B
Go
43 lines
906 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("dequeue", func(t *testing.T) {
|
|
found := map[string]struct{}{}
|
|
for i := 0; i < 39; i++ {
|
|
if reservation, b, err := q.Dequeue(ctx); err != nil {
|
|
t.Fatal(i, "dequeue err", err)
|
|
} else if _, ok := found[string(b)]; ok {
|
|
t.Errorf("dequeued %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{}{}
|
|
}
|
|
}
|
|
})
|
|
}
|