-
Notifications
You must be signed in to change notification settings - Fork 0
/
rede_test.go
135 lines (125 loc) · 3.14 KB
/
rede_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
125
126
127
128
129
130
131
132
133
134
135
package go_rede
import (
"context"
"os"
"reflect"
"testing"
"time"
)
var (
ctx = context.Background()
rede *Client
)
func TestMain(m *testing.M) {
rede = NewClient(&Options{
Namespaces: "rede",
Addr: os.Getenv("REDIS_ADDR"),
})
exitCode := m.Run()
os.Exit(exitCode)
}
func TestClient_Push(t *testing.T) {
now := float64(time.Now().Unix())
type input struct {
member string
ttl time.Duration
}
tests := []struct {
input input
want float64
wantErr bool
}{
{input: input{member: "a", ttl: 1 * time.Second}, want: now + 1},
{input: input{member: "b", ttl: 2 * time.Second}, want: now + 2},
{input: input{member: "c", ttl: 3 * time.Second}, want: now + 3},
{input: input{member: "d", ttl: 500 * time.Millisecond}, want: now + 0.5},
{input: input{member: "e", ttl: 1 * time.Minute}, want: now + 60},
}
for _, ts := range tests {
_, err := rede.Push(ctx, ts.input.member, ts.input.ttl)
if (err != nil) != ts.wantErr {
t.Errorf("Push() error = %v, wantErr %v", err, ts.wantErr)
return
}
got, err := rede.ZScore(ctx, rede.Namespaces, ts.input.member).Result()
if (err != nil) != ts.wantErr {
t.Errorf("ZScore() error = %v, wantErr %v", err, ts.wantErr)
return
}
if !reflect.DeepEqual(int64(ts.want*1e6), int64(got*1e6)) {
t.Errorf("ZScore() got = %v, want %v", int64(got*1e6), int64(ts.want*1e6))
}
}
}
func TestClient_Look(t *testing.T) {
type input struct {
member string
ttl time.Duration
}
tests := []struct {
input input
want float64
wantErr bool
}{
{input: input{member: "a", ttl: 1 * time.Second}, want: 1},
{input: input{member: "b", ttl: 2 * time.Second}, want: 2},
{input: input{member: "c", ttl: 3 * time.Second}, want: 3},
{input: input{member: "d", ttl: 500 * time.Millisecond}, want: 0.5},
{input: input{member: "e", ttl: 1 * time.Minute}, want: 60},
}
for _, ts := range tests {
_, _ = rede.Push(ctx, ts.input.member, ts.input.ttl)
got, err := rede.Look(ctx, ts.input.member)
if (err != nil) != ts.wantErr {
t.Errorf("Look() error = %v, wantErr %v", err, ts.wantErr)
return
}
if !reflect.DeepEqual(got, ts.want) {
t.Errorf("ZScore() got = %v, want %v", got, ts.want)
}
}
}
func TestClient_Ttn(t *testing.T) {
r, err := rede.Ttn(ctx)
t.Log(r, err)
}
func TestClient_Poll(t *testing.T) {
type input struct {
member string
ttl time.Duration
}
tests := struct {
input []input
sleep time.Duration
want []string
wantErr bool
}{
input: []input{
{member: "a", ttl: 1 * time.Second},
{member: "b", ttl: 2 * time.Second},
{member: "c", ttl: 3 * time.Second},
{member: "d", ttl: 4 * time.Second},
},
sleep: 2 * time.Second,
want: []string{"a", "b"},
}
rede.Del(ctx, rede.Namespaces)
for _, ts := range tests.input {
_, _ = rede.Push(ctx, ts.member, ts.ttl)
}
time.Sleep(tests.sleep)
cur := rede.Poll(ctx)
i := 0
for cur.Next() {
got, err := cur.Get()
t.Log(got, err)
if (err != nil) != tests.wantErr {
t.Errorf("Poll() error = %v, wantErr %v", err, tests.wantErr)
return
}
if !reflect.DeepEqual(tests.want[i], got) {
t.Errorf("Next() got = %v, want %v", got, tests.want[i])
}
i++
}
}