-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrediszsetdriver_test.go
94 lines (78 loc) · 2.28 KB
/
rediszsetdriver_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
package redisdriver_test
import (
"context"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
"github.com/dcron-contrib/commons"
"github.com/dcron-contrib/commons/dlog"
"github.com/dcron-contrib/redisdriver"
redis "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)
func testFuncNewRedisZSetDriver(addr string) commons.DriverV2 {
redisCli := redis.NewClient(&redis.Options{
Addr: addr,
})
return redisdriver.NewZSetDriver(redisCli)
}
func TestRedisZSetDriver_GetNodes(t *testing.T) {
rds := miniredis.RunT(t)
drvs := make([]commons.DriverV2, 0)
N := 10
for i := 0; i < N; i++ {
drv := testFuncNewRedisZSetDriver(rds.Addr())
drv.Init(
t.Name(),
commons.NewTimeoutOption(5*time.Second),
commons.NewLoggerOption(dlog.NewLoggerForTest(t)))
err := drv.Start(context.Background())
require.Nil(t, err)
drvs = append(drvs, drv)
}
for _, v := range drvs {
nodes, err := v.GetNodes(context.Background())
require.Nil(t, err)
require.Equal(t, N, len(nodes))
}
for _, v := range drvs {
v.Stop(context.Background())
}
}
func TestRedisZSetDriver_Stop(t *testing.T) {
var err error
var nodes []string
rds := miniredis.RunT(t)
drv1 := testFuncNewRedisZSetDriver(rds.Addr())
drv1.Init(t.Name(),
commons.NewTimeoutOption(5*time.Second),
commons.NewLoggerOption(dlog.NewLoggerForTest(t)))
drv2 := testFuncNewRedisZSetDriver(rds.Addr())
drv2.Init(t.Name(),
commons.NewTimeoutOption(5*time.Second),
commons.NewLoggerOption(dlog.NewLoggerForTest(t)))
require.Nil(t, drv2.Start(context.Background()))
require.Nil(t, drv1.Start(context.Background()))
nodes, err = drv1.GetNodes(context.Background())
require.Nil(t, err)
require.Len(t, nodes, 2)
nodes, err = drv2.GetNodes(context.Background())
require.Nil(t, err)
require.Len(t, nodes, 2)
drv1.Stop(context.Background())
<-time.After(6 * time.Second)
nodes, err = drv2.GetNodes(context.Background())
require.Nil(t, err)
require.Len(t, nodes, 1)
err = drv1.Start(context.Background())
require.Nil(t, err)
<-time.After(5 * time.Second)
nodes, err = drv2.GetNodes(context.Background())
require.Nil(t, err)
require.Len(t, nodes, 2)
nodes, err = drv1.GetNodes(context.Background())
require.Nil(t, err)
require.Len(t, nodes, 2)
drv2.Stop(context.Background())
drv1.Stop(context.Background())
}