-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
92 lines (81 loc) · 2.28 KB
/
queue_test.go
File metadata and controls
92 lines (81 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package kick
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/go-redis/redis"
"github.com/google/uuid"
)
func createTestingRedisClient() *redis.Client {
redisClient := redis.NewClient(&redis.Options{
Addr: "localhost:63790",
DB: 0,
})
redisClient.Del("testingQueue")
redisClient.Del("testingQueue::InprogressSet")
redisClient.Del("testingQueue::ScheduledSet")
return redisClient
}
func TestQueueEnqueueJob(t *testing.T) {
redisClient := createTestingRedisClient()
q := NewQueue("testingQueue", redisClient)
job := &Job{
ID: uuid.New(),
}
q.EnqueueJob(time.Now(), job)
str, _ := redisClient.LPop(q.name).Result()
var actualJob Job
json.Unmarshal([]byte(str), &actualJob)
if actualJob.ID != job.ID {
t.Errorf("it should return inprogress %s but get %s", job.ID, actualJob.ID)
}
}
func TestQueueEnqueueJobWithFutureTime(t *testing.T) {
redisClient := createTestingRedisClient()
q := NewQueue("testingQueue", redisClient)
job := &Job{
ID: uuid.New(),
}
performAt := time.Now().Add(10 * time.Minute)
q.EnqueueJob(performAt, job)
z := redis.ZRangeBy{
Min: "-inf",
Max: fmt.Sprintf("%f", float64(performAt.UnixNano())),
Count: 10,
}
items, _ := redisClient.ZRangeByScore(q.ScheduledSetName(), z).Result()
if len(items) != 1 {
t.Errorf("scheduledSet size should equal 1 but get %d", len(items))
}
var actualJob Job
json.Unmarshal([]byte(items[0]), &actualJob)
if actualJob.ID != job.ID {
t.Errorf("it should return inprogress %s but get %s", job.ID, actualJob.ID)
}
}
func TestQueueRemoveJobFromInprogress(t *testing.T) {
redisClient := createTestingRedisClient()
q := NewQueue("testingQueue", redisClient)
job1 := &Job{
ID: uuid.New(),
}
job2 := &Job{
ID: uuid.New(),
}
bytes1, _ := json.Marshal(job1)
bytes2, _ := json.Marshal(job2)
redisClient.LPush(q.InprogressSetName(), bytes1)
redisClient.LPush(q.InprogressSetName(), bytes2)
q.RemoveJobFromInprogress(job1)
size, _ := redisClient.LLen(q.InprogressSetName()).Result()
if size != 1 {
t.Errorf("inprogress list size should equal 1 but get %d", size)
}
str, _ := redisClient.LPop(q.InprogressSetName()).Result()
var actualJob Job
json.Unmarshal([]byte(str), &actualJob)
if actualJob.ID != job2.ID {
t.Errorf("it should return inprogress %s but get %s", job2.ID, actualJob.ID)
}
}