2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-09-20 19:06:46 +08:00
asynq/rdb_test.go

129 lines
2.9 KiB
Go
Raw Normal View History

2019-11-20 23:01:24 +08:00
package asynq
import (
"encoding/json"
"math/rand"
2019-11-20 23:01:24 +08:00
"testing"
"time"
"github.com/go-redis/redis/v7"
"github.com/google/go-cmp/cmp"
2019-11-22 22:16:43 +08:00
"github.com/google/uuid"
2019-11-20 23:01:24 +08:00
)
var client *redis.Client
func init() {
rand.Seed(time.Now().UnixNano())
}
2019-11-20 23:01:24 +08:00
// setup connects to a redis database and flush all keys
// before returning an instance of rdb.
func setup() *rdb {
client = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 15, // use database 15 to separate from other applications
})
// Start each test with a clean slate.
if err := client.FlushDB().Err(); err != nil {
panic(err)
}
return newRDB(client)
}
func randomTask(taskType, qname string) *taskMessage {
return &taskMessage{
2019-11-22 22:16:43 +08:00
ID: uuid.New(),
Type: taskType,
Queue: qname,
Retry: rand.Intn(100),
2019-11-20 23:01:24 +08:00
}
}
func TestPush(t *testing.T) {
r := setup()
msg := randomTask("send_email", "default")
2019-11-20 23:01:24 +08:00
err := r.push(msg)
if err != nil {
t.Fatalf("could not push message to queue: %v", err)
}
res := client.LRange("asynq:queues:default", 0, -1).Val()
if len(res) != 1 {
t.Fatalf("len(res) = %d, want %d", len(res), 1)
}
bytes, err := json.Marshal(msg)
if err != nil {
t.Fatalf("json.Marshal(msg) failed: %v", err)
}
if res[0] != string(bytes) {
t.Fatalf("res[0] = %s, want %s", res[0], string(bytes))
}
}
2019-11-22 13:45:27 +08:00
func TestDequeueImmediateReturn(t *testing.T) {
2019-11-20 23:01:24 +08:00
r := setup()
msg := randomTask("export_csv", "csv")
2019-11-20 23:01:24 +08:00
r.push(msg)
res, err := r.dequeue("asynq:queues:csv", time.Second)
2019-11-20 23:01:24 +08:00
if err != nil {
t.Fatalf("r.bpop() failed: %v", err)
}
2019-11-22 13:45:27 +08:00
2019-11-20 23:01:24 +08:00
if !cmp.Equal(res, msg) {
t.Errorf("cmp.Equal(res, msg) = %t, want %t", false, true)
}
jobs := client.LRange(inProgress, 0, -1).Val()
2019-11-22 13:45:27 +08:00
if len(jobs) != 1 {
t.Fatalf("len(jobs) = %d, want %d", len(jobs), 1)
}
var tm taskMessage
if err := json.Unmarshal([]byte(jobs[0]), &tm); err != nil {
t.Fatalf("json.Marshal() failed: %v", err)
}
if diff := cmp.Diff(res, &tm); diff != "" {
t.Errorf("cmp.Diff(res, tm) = %s", diff)
}
2019-11-20 23:01:24 +08:00
}
2019-11-22 13:45:27 +08:00
func TestDequeueTimeout(t *testing.T) {
2019-11-20 23:01:24 +08:00
r := setup()
_, err := r.dequeue("asynq:queues:default", time.Second)
2019-11-20 23:01:24 +08:00
if err != errQueuePopTimeout {
t.Errorf("err = %v, want %v", err, errQueuePopTimeout)
}
}
func TestMoveAll(t *testing.T) {
r := setup()
seed := []*taskMessage{
randomTask("send_email", "default"),
randomTask("export_csv", "csv"),
randomTask("sync_stuff", "sync"),
}
for _, task := range seed {
bytes, err := json.Marshal(task)
if err != nil {
t.Errorf("json.Marhsal() failed: %v", err)
}
if err := client.LPush(inProgress, string(bytes)).Err(); err != nil {
t.Errorf("LPUSH %q %s failed: %v", inProgress, string(bytes), err)
}
}
err := r.moveAll(inProgress, defaultQueue)
if err != nil {
t.Errorf("moveAll failed: %v", err)
}
if l := client.LLen(inProgress).Val(); l != 0 {
t.Errorf("LLEN %q = %d, want 0", inProgress, l)
}
if l := client.LLen(defaultQueue).Val(); int(l) != len(seed) {
t.Errorf("LLEN %q = %d, want %d", defaultQueue, l, len(seed))
}
}