-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed_queue_test.go
124 lines (115 loc) · 2.7 KB
/
distributed_queue_test.go
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package glock
import (
"encoding/json"
"log"
"testing"
"time"
"github.com/go-redis/redis/v8"
)
type User struct {
Id string `json:"id"`
}
func TestSet(t *testing.T) {
client := redis.NewClient(&redis.Options{
Password: "",
Addr: "localhost:6379",
DB: 1, // use default DB
})
q, err := ConnectDistributedQueue(client, "queue", 3*time.Second)
if err != nil {
log.Print(err)
}
if err := q.Set("test1", &User{"1"}, &User{"2"}, &User{"3"}, &User{"4"}, &User{"1"}); err != nil {
t.Fail()
}
s, err := q.Size("test1")
if err != nil || s != 5 {
t.Fail()
}
log.Print(s, err)
}
func TestPushAndPop(t *testing.T) {
client := redis.NewClient(&redis.Options{
Password: "",
Addr: "localhost:6379",
DB: 1, // use default DB
})
q, err := ConnectDistributedQueue(client, "queue", 3*time.Second)
if err != nil {
log.Print(err)
}
if err := q.Set("test1", &User{"1"}, &User{"2"}, &User{"3"}, &User{"4"}, &User{"5"}, &User{"6"}); err != nil {
log.Print(err)
t.Fail()
}
if err := q.Push("test1", &User{"Tenguyen"}); err != nil {
log.Print(2222, err)
}
out := &User{}
if err := q.Pop("test1", out); err != nil || out.Id != "Tenguyen" {
log.Print(out, err)
t.Fail()
}
}
func TestUnshiftAndShift(t *testing.T) {
client := redis.NewClient(&redis.Options{
Password: "",
Addr: "localhost:6379",
DB: 1, // use default DB
})
q, err := ConnectDistributedQueue(client, "queue", 3*time.Second)
if err != nil {
log.Print(err)
}
if err := q.Set("test1", &User{"1"}, &User{"2"}, &User{"3"}, &User{"4"}, &User{"1"}); err != nil {
log.Print(err)
t.Fail()
}
if err := q.Unshift("test1", &User{"Tenguyen"}); err != nil {
log.Print(2222, err)
}
out := &User{}
if err := q.Shift("test1", out); err != nil || out.Id != "Tenguyen" {
log.Print(out, err)
t.Fail()
}
}
func TestList(t *testing.T) {
client := redis.NewClient(&redis.Options{
Password: "",
Addr: "localhost:6379",
DB: 1, // use default DB
})
q, err := ConnectDistributedQueue(client, "queue", 3*time.Second)
if err != nil {
log.Print(err)
}
out := []*User{}
err = q.List("test1", 0, 3, func(data []string) error {
for _, item := range data {
u := &User{}
if err := json.Unmarshal([]byte(item), u); err != nil {
log.Print(err)
continue
}
out = append(out, u)
}
return nil
})
bin, _ := json.Marshal(out)
log.Print(11111, string(bin), err)
out = []*User{}
err = q.List("test1", 4, 7, func(data []string) error {
for _, item := range data {
u := &User{}
if err := json.Unmarshal([]byte(item), u); err != nil {
log.Print(err)
continue
}
out = append(out, u)
}
return nil
})
bin2, _ := json.Marshal(out)
log.Print(2222222, string(bin2), err)
}