-
Notifications
You must be signed in to change notification settings - Fork 113
/
stub_test.go
133 lines (113 loc) · 2.68 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
128
129
130
131
132
133
package radix
import (
"errors"
"fmt"
"net"
"strconv"
"sync"
. "testing"
"time"
"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 fmt.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()
err := conn.SetDeadline(now.Add(2 * time.Second))
assert.NoError(t, err)
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{})
var nerr *net.OpError
assert.True(t, errors.As(err, &nerr))
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 fmt.Errorf("this stub doesn't support command %q", args[0])
}
})
if err := stub.Do(Cmd(nil, "SET", "foo", "1")); err != nil {
// handle error
}
var foo int
if err := stub.Do(Cmd(&foo, "GET", "foo")); err != nil {
// handle error
}
fmt.Printf("foo: %d\n", foo)
}