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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package memory_test
import (
"net"
"net/rpc"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
"github.com/spiral/endure"
goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc"
"github.com/spiral/roadrunner/v2/plugins/config"
httpPlugin "github.com/spiral/roadrunner/v2/plugins/http"
"github.com/spiral/roadrunner/v2/plugins/kv"
"github.com/spiral/roadrunner/v2/plugins/kv/memory"
"github.com/spiral/roadrunner/v2/plugins/logger"
rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc"
"github.com/spiral/roadrunner/v2/plugins/server"
"github.com/stretchr/testify/assert"
)
func TestInMemory(t *testing.T) {
cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.DebugLevel))
assert.NoError(t, err)
cfg := &config.Viper{
Path: "configs/.rr-init.yaml",
Prefix: "rr",
}
err = cont.RegisterAll(
cfg,
&memory.Plugin{},
&rpcPlugin.Plugin{},
&logger.ZapLogger{},
&server.Plugin{},
&httpPlugin.Plugin{},
)
assert.NoError(t, err)
err = cont.Init()
if err != nil {
t.Fatal(err)
}
ch, err := cont.Serve()
assert.NoError(t, err)
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
wg := &sync.WaitGroup{}
wg.Add(1)
stopCh := make(chan struct{}, 1)
go func() {
defer wg.Done()
for {
select {
case e := <-ch:
assert.Fail(t, "error", e.Error.Error())
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
case <-sig:
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
return
case <-stopCh:
// timeout
err = cont.Stop()
if err != nil {
assert.FailNow(t, "error", err.Error())
}
return
}
}
}()
time.Sleep(time.Second * 1)
t.Run("testInMemoryRPCMethods", testRPCMethods)
stopCh <- struct{}{}
wg.Wait()
_ = os.Remove("rr")
}
func testRPCMethods(t *testing.T) {
conn, err := net.Dial("tcp", "127.0.0.1:6001")
assert.NoError(t, err)
client := rpc.NewClientWithCodec(goridgeRpc.NewClientCodec(conn))
var setRes bool
items := make([]kv.Item, 0, 5)
items = append(items, kv.Item{
Key: "a",
Value: "aa",
})
items = append(items, kv.Item{
Key: "b",
Value: "bb",
})
// add 5 second ttl
tt := time.Now().Add(time.Second * 5).Format(time.RFC3339)
items = append(items, kv.Item{
Key: "c",
Value: "cc",
TTL: tt,
})
items = append(items, kv.Item{
Key: "d",
Value: "dd",
})
items = append(items, kv.Item{
Key: "e",
Value: "ee",
})
// Register 3 keys with values
err = client.Call("memory.Set", items, &setRes)
assert.NoError(t, err)
assert.True(t, setRes)
ret := make(map[string]bool)
keys := []string{"a", "b", "c"}
err = client.Call("memory.Has", keys, &ret)
assert.NoError(t, err)
assert.Len(t, ret, 3) // should be 3
// key "c" should be deleted
time.Sleep(time.Second * 7)
ret = make(map[string]bool)
err = client.Call("memory.Has", keys, &ret)
assert.NoError(t, err)
assert.Len(t, ret, 2) // should be 2
mGet := make(map[string]interface{})
keys = []string{"a", "b", "c"}
err = client.Call("memory.MGet", keys, &mGet)
assert.NoError(t, err)
assert.Len(t, mGet, 2) // c is expired
assert.Equal(t, string("aa"), mGet["a"].(string))
assert.Equal(t, string("bb"), mGet["b"].(string))
mExpKeys := make([]kv.Item, 0, 2)
tt2 := time.Now().Add(time.Second * 10).Format(time.RFC3339)
mExpKeys = append(mExpKeys, kv.Item{Key: "a", TTL: tt2})
mExpKeys = append(mExpKeys, kv.Item{Key: "b", TTL: tt2})
mExpKeys = append(mExpKeys, kv.Item{Key: "d", TTL: tt2})
// MEXPIRE
var mExpRes bool
err = client.Call("memory.MExpire", mExpKeys, &mExpRes)
assert.NoError(t, err)
assert.True(t, mExpRes)
// TTL
keys = []string{"a", "b", "d"}
ttlRes := make(map[string]interface{})
err = client.Call("memory.TTL", keys, &ttlRes)
assert.NoError(t, err)
assert.Len(t, ttlRes, 3)
// HAS AFTER TTL
time.Sleep(time.Second * 15)
ret = make(map[string]bool)
keys = []string{"a", "b", "d"}
err = client.Call("memory.Has", keys, &ret)
assert.NoError(t, err)
assert.Len(t, ret, 0)
// DELETE
keys = []string{"e"}
var delRet bool
err = client.Call("memory.Delete", keys, &delRet)
assert.NoError(t, err)
assert.True(t, delRet)
// HAS AFTER DELETE
ret = make(map[string]bool)
keys = []string{"e"}
err = client.Call("memory.Has", keys, &ret)
assert.NoError(t, err)
assert.Len(t, ret, 0)
}
|