forked from actatum/stormrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
245 lines (211 loc) · 5.14 KB
/
client_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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package stormrpc
import (
"context"
"errors"
"fmt"
"math/rand"
"strconv"
"testing"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
)
func TestNewClient(t *testing.T) {
t.Run("no nats server running", func(t *testing.T) {
_, err := NewClient(nats.DefaultURL)
if err == nil {
t.Fatal("expected error got nil")
}
})
t.Run("nats server running", func(t *testing.T) {
ns, err := server.NewServer(&server.Options{
Port: 41397,
})
if err != nil {
t.Fatal(err)
}
go ns.Start()
t.Cleanup(func() {
ns.Shutdown()
ns.WaitForShutdown()
})
if !ns.ReadyForConnections(1 * time.Second) {
t.Error("timeout waiting for nats server")
return
}
c, err := NewClient(ns.ClientURL())
if err != nil {
t.Fatal(err)
}
c.Close()
})
}
func TestClient_Do(t *testing.T) {
t.Parallel()
rand.Seed(time.Now().UnixNano())
ns, err := server.NewServer(&server.Options{
Port: 41397,
})
if err != nil {
t.Fatal(err)
}
go ns.Start()
t.Cleanup(func() {
ns.Shutdown()
ns.WaitForShutdown()
})
if !ns.ReadyForConnections(1 * time.Second) {
t.Error("timeout waiting for nats server")
return
}
t.Run("deadline exceeded", func(t *testing.T) {
t.Parallel()
timeout := 50 * time.Millisecond
subject := strconv.Itoa(rand.Int())
srv, err := NewServer("test", ns.ClientURL())
if err != nil {
t.Fatal(err)
}
srv.Handle(subject, func(ctx context.Context, r Request) Response {
time.Sleep(timeout + 10*time.Millisecond)
return Response{Msg: &nats.Msg{Subject: r.Reply}}
})
go func() {
_ = srv.Run()
}()
t.Cleanup(func() {
_ = srv.Shutdown(context.Background())
})
client, err := NewClient(ns.ClientURL())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
r, err := NewRequest(subject, map[string]string{"howdy": "partner"})
if err != nil {
t.Fatal(err)
}
resp := client.Do(ctx, r)
if resp.Err == nil {
t.Fatal("expected error got nil")
}
if !errors.Is(resp.Err, context.DeadlineExceeded) {
t.Fatalf("got = %v, want %v", resp.Err, context.DeadlineExceeded)
}
})
t.Run("rpc error", func(t *testing.T) {
t.Parallel()
timeout := 50 * time.Millisecond
subject := strconv.Itoa(rand.Int())
srv, err := NewServer("test", ns.ClientURL())
if err != nil {
t.Fatal(err)
}
srv.Handle(subject, func(ctx context.Context, r Request) Response {
return NewErrorResponse(r.Reply, Errorf(ErrorCodeNotFound, "thingy not found"))
})
go func() {
_ = srv.Run()
}()
t.Cleanup(func() {
_ = srv.Shutdown(context.Background())
})
client, err := NewClient(ns.ClientURL())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
r, err := NewRequest(subject, map[string]string{"howdy": "partner"})
if err != nil {
t.Fatal(err)
}
resp := client.Do(ctx, r)
if resp.Err == nil {
t.Fatal("expected error got nil")
}
code := CodeFromErr(resp.Err)
if code != ErrorCodeNotFound {
t.Fatalf("got = %v, want %v", code, ErrorCodeNotFound)
}
msg := MessageFromErr(resp.Err)
if msg != "thingy not found" {
t.Fatalf("got = %v, want %v", msg, "thingy not found")
}
})
t.Run("no servers", func(t *testing.T) {
t.Parallel()
subject := strconv.Itoa(rand.Int())
client, err := NewClient(ns.ClientURL())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
req, err := NewRequest(subject, map[string]string{"x": "D"})
if err != nil {
t.Fatal(err)
}
resp := client.Do(ctx, req)
if resp.Err == nil {
t.Fatal("expected error got nil")
}
code := CodeFromErr(resp.Err)
if code != ErrorCodeInternal {
t.Fatalf("got = %v, want %v", code, ErrorCodeInternal)
}
msg := MessageFromErr(resp.Err)
if msg != fmt.Sprintf("no servers available for subject: %s", subject) {
t.Fatalf(
"got = %v, want %v",
msg,
fmt.Sprintf("no servers available for subject: %s", subject),
)
}
})
t.Run("successful request", func(t *testing.T) {
t.Parallel()
timeout := 50 * time.Millisecond
subject := strconv.Itoa(rand.Int())
srv, err := NewServer("test", ns.ClientURL())
if err != nil {
t.Fatal(err)
}
srv.Handle(subject, func(ctx context.Context, r Request) Response {
var resp Response
resp, err = NewResponse(r.Reply, map[string]string{"hello": "world"})
if err != nil {
return NewErrorResponse(r.Reply, err)
}
return resp
})
go func() {
_ = srv.Run()
}()
t.Cleanup(func() {
_ = srv.Shutdown(context.Background())
})
client, err := NewClient(ns.ClientURL())
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
r, err := NewRequest(subject, map[string]string{"howdy": "partner"})
if err != nil {
t.Fatal(err)
}
resp := client.Do(ctx, r)
if resp.Err != nil {
t.Fatal(resp.Err)
}
var result map[string]string
if err = resp.Decode(&result); err != nil {
t.Fatal(err)
}
if result["hello"] != "world" {
t.Fatalf("got = %v, want %v", result["hello"], "world")
}
})
}