forked from mediocregopher/radix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stub_test.go
127 lines (108 loc) · 2.57 KB
/
stub_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
package radix
import (
"fmt"
"net"
"strconv"
"sync"
. "testing"
"time"
errors "golang.org/x/xerrors"
"github.com/mediocregopher/radix/v3/resp/resp2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Watching the watchmen
func testStub() Conn {
m := map[string]string{}
return Stub("tcp", "127.0.0.1:6379", func(args []string) interface{} {
switch args[0] {
case "GET":
return m[args[1]]
case "SET":
m[args[1]] = args[2]
return nil
case "ECHO":
return args[1]
default:
return errors.Errorf("testStub doesn't support command %q", args[0])
}
})
}
func TestStub(t *T) {
stub := testStub()
{ // Basic test
var foo string
require.Nil(t, stub.Do(Cmd(nil, "SET", "foo", "a")))
require.Nil(t, stub.Do(Cmd(&foo, "GET", "foo")))
assert.Equal(t, "a", foo)
}
{ // Basic test with an int, to ensure marshalling/unmarshalling all works
var foo int
require.Nil(t, stub.Do(FlatCmd(nil, "SET", "foo", 1)))
require.Nil(t, stub.Do(Cmd(&foo, "GET", "foo")))
assert.Equal(t, 1, foo)
}
}
func TestStubPipeline(t *T) {
stub := testStub()
var out string
err := stub.Do(Pipeline(
Cmd(nil, "SET", "foo", "bar"),
Cmd(&out, "GET", "foo"),
))
require.Nil(t, err)
assert.Equal(t, "bar", out)
}
func TestStubLockingTimeout(t *T) {
stub := testStub()
wg := new(sync.WaitGroup)
c := 1000
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < c; i++ {
require.Nil(t, stub.Encode(Cmd(nil, "ECHO", strconv.Itoa(i))))
}
}()
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < c; i++ {
var j int
require.Nil(t, stub.Decode(resp2.Any{I: &j}))
assert.Equal(t, i, j)
}
}()
wg.Wait()
// test out timeout. do a write-then-read to ensure nothing bad happens
// when there's actually data to read
now := time.Now()
conn := stub.NetConn()
conn.SetDeadline(now.Add(2 * time.Second))
require.Nil(t, stub.Encode(Cmd(nil, "ECHO", "1")))
require.Nil(t, stub.Decode(resp2.Any{}))
// now there's no data to read, should return after 2-ish seconds with a
// timeout error
err := stub.Decode(resp2.Any{})
nerr, ok := err.(*net.OpError)
assert.True(t, ok)
assert.True(t, nerr.Timeout())
}
func ExampleStub() {
m := map[string]string{}
stub := Stub("tcp", "127.0.0.1:6379", func(args []string) interface{} {
switch args[0] {
case "GET":
return m[args[1]]
case "SET":
m[args[1]] = args[2]
return nil
default:
return errors.Errorf("this stub doesn't support command %q", args[0])
}
})
stub.Do(Cmd(nil, "SET", "foo", "1"))
var foo int
stub.Do(Cmd(&foo, "GET", "foo"))
fmt.Printf("foo: %d\n", foo)
}