2019-11-30 09:40:31 +08:00
|
|
|
package asynq
|
|
|
|
|
|
|
|
import (
|
2019-12-04 13:01:26 +08:00
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
2019-11-30 09:40:31 +08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
r := setup(t)
|
2019-12-04 13:01:26 +08:00
|
|
|
client := &Client{rdb.NewRDB(r)}
|
2019-11-30 09:40:31 +08:00
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
task *Task
|
|
|
|
processAt time.Time
|
|
|
|
wantQueueSize int64
|
|
|
|
wantScheduledSize int64
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
task: &Task{Type: "send_email", Payload: map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}},
|
|
|
|
processAt: time.Now(),
|
|
|
|
wantQueueSize: 1,
|
|
|
|
wantScheduledSize: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
task: &Task{Type: "send_email", Payload: map[string]interface{}{"to": "customer@gmail.com", "from": "merchant@example.com"}},
|
|
|
|
processAt: time.Now().Add(2 * time.Hour),
|
|
|
|
wantQueueSize: 0,
|
|
|
|
wantScheduledSize: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
// clean up db before each test case.
|
2019-12-04 13:01:26 +08:00
|
|
|
if err := r.FlushDB().Err(); err != nil {
|
2019-11-30 09:40:31 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := client.Process(tc.task, tc.processAt)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-04 13:01:26 +08:00
|
|
|
if l := r.LLen(rdb.DefaultQueue).Val(); l != tc.wantQueueSize {
|
|
|
|
t.Errorf("%q has length %d, want %d", rdb.DefaultQueue, l, tc.wantQueueSize)
|
2019-11-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
|
2019-12-04 13:01:26 +08:00
|
|
|
if l := r.ZCard(rdb.Scheduled).Val(); l != tc.wantScheduledSize {
|
|
|
|
t.Errorf("%q has length %d, want %d", rdb.Scheduled, l, tc.wantScheduledSize)
|
2019-11-30 09:40:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|