forked from latolukasz/beeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_pipeline_test.go
102 lines (90 loc) · 2.82 KB
/
redis_pipeline_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
package beeorm
import (
"testing"
"time"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
)
func TestRedisPipeline(t *testing.T) {
registry := &Registry{}
registry.RegisterRedis("localhost:6382", "", 15)
registry.RegisterRedisStream("test-stream", "default", []string{"test-group"})
validatedRegistry, def, err := registry.Validate()
assert.Nil(t, err)
defer def()
engine := validatedRegistry.CreateEngine()
r := engine.GetRedis()
r.FlushDB()
pipeLine := r.PipeLine()
r.Set("a", "A", 10)
r.Set("c", "C", 10)
testLogger := &testLogHandler{}
engine.RegisterQueryLogger(testLogger, false, true, false)
c1 := pipeLine.Get("a")
c2 := pipeLine.Get("b")
c3 := pipeLine.Get("c")
pipeLine.Exec()
assert.Len(t, testLogger.Logs, 1)
val, has := c1.Result()
assert.Equal(t, "A", val)
assert.True(t, has)
val, has = c2.Result()
assert.Equal(t, "", val)
assert.False(t, has)
val, has = c3.Result()
assert.Equal(t, "C", val)
assert.True(t, has)
pipeLine = r.PipeLine()
c4 := pipeLine.XAdd("test-stream", []string{"key", "a", "key2", "b"})
c5 := pipeLine.XAdd("test-stream", []string{"key", "c", "key2", "d"})
_ = pipeLine.XAdd("a", []string{"key", "c", "key2", "d"})
c7 := pipeLine.XAdd("test-stream", []string{"key", "e", "key2", "f"})
assert.Panics(t, func() {
pipeLine.Exec()
})
val = c4.Result()
assert.NotEmpty(t, val)
val = c5.Result()
assert.NotEmpty(t, val)
val = c7.Result()
assert.NotEmpty(t, val)
assert.Equal(t, int64(3), r.XLen("test-stream"))
events := r.XRead(&redis.XReadArgs{Count: 10, Streams: []string{"test-stream", "0"}, Block: time.Millisecond * 500})
assert.Len(t, events, 1)
assert.Len(t, events[0].Messages, 3)
pipeLine = r.PipeLine()
pipeLine.Set("test_set", "test_value", time.Minute)
pipeLine.Exec()
returnedVal, has := engine.GetRedis().Get("test_set")
assert.True(t, has)
assert.Equal(t, "test_value", returnedVal)
pipeLine = r.PipeLine()
boolResult := pipeLine.Expire("test_set", time.Hour)
pipeLine.Exec()
assert.True(t, boolResult.Result())
pipeLine = r.PipeLine()
pipeLine.Del("test_set")
pipeLine.Exec()
_, has = engine.GetRedis().Get("test_set")
assert.False(t, has)
pipeLine = r.PipeLine()
pipeLine.HSet("test_hset", "a", "b", "c", "d")
pipeLine.Exec()
hSetValues := engine.GetRedis().HGetAll("test_hset")
assert.Len(t, hSetValues, 2)
assert.Equal(t, "b", hSetValues["a"])
assert.Equal(t, "d", hSetValues["c"])
pipeLine = r.PipeLine()
pipeLine.HDel("test_hset", "c")
pipeLine.Exec()
hSetValues = engine.GetRedis().HGetAll("test_hset")
assert.Len(t, hSetValues, 1)
assert.Equal(t, "b", hSetValues["a"])
pipeLine = r.PipeLine()
intRes := pipeLine.HIncrBy("test_inc", "a", 2)
pipeLine.Exec()
returnedVal, hasVal := engine.GetRedis().HGet("test_inc", "a")
assert.True(t, hasVal)
assert.Equal(t, "2", returnedVal)
assert.Equal(t, int64(2), intRes.Result())
}