-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue_test.go
117 lines (95 loc) · 2.29 KB
/
priority_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
package priority_queue
import (
"context"
"github.com/go-redis/redis/v8"
"testing"
)
func TestPriorityQueue(t *testing.T) {
// prepare
ctx := context.Background()
rds := redis.NewClient(&redis.Options{
Addr: "192.168.0.11:6379",
Password: "1234",
DB: 2,
})
if err := rds.Ping(ctx).Err(); err != nil {
t.Fatalf("can't connect to redis: %v", err)
}
defer func() {
rds.Del(ctx, "test_queue")
}()
queue := NewPriorityQueue("test_queue", rds)
// push some keys to top of the queue
if err := queue.LPush(ctx, "key1"); err != nil {
t.Fatalf("something went wrong: %v", err)
}
if err := queue.LPush(ctx, "key2"); err != nil {
t.Fatalf("something went wrong: %v", err)
}
if err := queue.LPush(ctx, "key3"); err != nil {
t.Fatalf("something went wrong: %v", err)
}
// check queue
key, err := queue.Head(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if key != "key3" {
t.Fatalf("expect key: key3, got: %s", key)
}
key, err = queue.Tail(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if key != "key1" {
t.Fatalf("expect key: key1, got: %s", key)
}
// put key to bottom of the queue
if err := queue.RPush(ctx, "key4"); err != nil {
t.Fatalf("something went wrong: %v", err)
}
key, err = queue.Tail(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if key != "key4" {
t.Fatalf("expect key: key4, got: %s", key)
}
// test count
count, err := queue.Count(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if count != 4 {
t.Fatalf("expect 4, bug got: %d", count)
}
// test delete
if err := queue.Delete(ctx, "key1"); err != nil {
t.Fatalf("something went wrong: %v", err)
}
count, err = queue.Count(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if count != 3 {
t.Fatalf("expect 3, bug got: %d", count)
}
// test increase
if err := queue.Increase(ctx, "key4", 4); err != nil {
t.Fatalf("something went wrong: %v", err)
}
key, err = queue.Head(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if key != "key4" {
t.Fatalf("expect key: key4, got: %s", key)
}
key, err = queue.Tail(ctx)
if err != nil {
t.Fatalf("something went wrong: %v", err)
}
if key != "key2" {
t.Fatalf("expect key: key2, got: %s", key)
}
}