| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package jsonrpc2_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"encoding/json" |
| 10 |
"fmt" |
| 11 |
"path" |
| 12 |
"reflect" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"golang.org/x/exp/event/eventtest" |
| 18 |
"golang.org/x/exp/jsonrpc2" |
| 19 |
"golang.org/x/exp/jsonrpc2/internal/stack/stacktest" |
| 20 |
errors "golang.org/x/xerrors" |
| 21 |
) |
| 22 |
|
| 23 |
var callTests = []invoker{ |
| 24 |
call{"no_args", nil, true}, |
| 25 |
call{"one_string", "fish", "got:fish"}, |
| 26 |
call{"one_number", 10, "got:10"}, |
| 27 |
call{"join", []string{"a", "b", "c"}, "a/b/c"}, |
| 28 |
sequence{"notify", []invoker{ |
| 29 |
notify{"set", 3}, |
| 30 |
notify{"add", 5}, |
| 31 |
call{"get", nil, 8}, |
| 32 |
}}, |
| 33 |
sequence{"preempt", []invoker{ |
| 34 |
async{"a", "wait", "a"}, |
| 35 |
notify{"unblock", "a"}, |
| 36 |
collect{"a", true, false}, |
| 37 |
}}, |
| 38 |
sequence{"basic cancel", []invoker{ |
| 39 |
async{"b", "wait", "b"}, |
| 40 |
cancel{"b"}, |
| 41 |
collect{"b", nil, true}, |
| 42 |
}}, |
| 43 |
sequence{"queue", []invoker{ |
| 44 |
async{"a", "wait", "a"}, |
| 45 |
notify{"set", 1}, |
| 46 |
notify{"add", 2}, |
| 47 |
notify{"add", 3}, |
| 48 |
notify{"add", 4}, |
| 49 |
call{"peek", nil, 0}, |
| 50 |
notify{"unblock", "a"}, |
| 51 |
collect{"a", true, false}, |
| 52 |
call{"get", nil, 10}, |
| 53 |
}}, |
| 54 |
sequence{"fork", []invoker{ |
| 55 |
async{"a", "fork", "a"}, |
| 56 |
notify{"set", 1}, |
| 57 |
notify{"add", 2}, |
| 58 |
notify{"add", 3}, |
| 59 |
notify{"add", 4}, |
| 60 |
call{"get", nil, 10}, |
| 61 |
notify{"unblock", "a"}, |
| 62 |
collect{"a", true, false}, |
| 63 |
}}, |
| 64 |
callErr{"error", func() {}, "marshaling call parameters: json: unsupported type"}, |
| 65 |
} |
| 66 |
|
| 67 |
type binder struct { |
| 68 |
framer jsonrpc2.Framer |
| 69 |
runTest func(*handler) |
| 70 |
} |
| 71 |
|
| 72 |
type handler struct { |
| 73 |
conn *jsonrpc2.Connection |
| 74 |
accumulator int |
| 75 |
waitersBox chan map[string]chan struct{} |
| 76 |
calls map[string]*jsonrpc2.AsyncCall |
| 77 |
} |
| 78 |
|
| 79 |
type invoker interface { |
| 80 |
Name() string |
| 81 |
Invoke(t *testing.T, ctx context.Context, h *handler) |
| 82 |
} |
| 83 |
|
| 84 |
type notify struct { |
| 85 |
method string |
| 86 |
params interface{} |
| 87 |
} |
| 88 |
|
| 89 |
type call struct { |
| 90 |
method string |
| 91 |
params interface{} |
| 92 |
expect interface{} |
| 93 |
} |
| 94 |
|
| 95 |
type callErr struct { |
| 96 |
method string |
| 97 |
params interface{} |
| 98 |
expectErr string |
| 99 |
} |
| 100 |
|
| 101 |
type async struct { |
| 102 |
name string |
| 103 |
method string |
| 104 |
params interface{} |
| 105 |
} |
| 106 |
|
| 107 |
type collect struct { |
| 108 |
name string |
| 109 |
expect interface{} |
| 110 |
fails bool |
| 111 |
} |
| 112 |
|
| 113 |
type cancel struct { |
| 114 |
name string |
| 115 |
} |
| 116 |
|
| 117 |
type sequence struct { |
| 118 |
name string |
| 119 |
tests []invoker |
| 120 |
} |
| 121 |
|
| 122 |
type echo call |
| 123 |
|
| 124 |
type cancelParams struct{ ID int64 } |
| 125 |
|
| 126 |
func TestConnectionRaw(t *testing.T) { |
| 127 |
testConnection(t, jsonrpc2.RawFramer()) |
| 128 |
} |
| 129 |
|
| 130 |
func TestConnectionHeader(t *testing.T) { |
| 131 |
testConnection(t, jsonrpc2.HeaderFramer()) |
| 132 |
} |
| 133 |
|
| 134 |
func testConnection(t *testing.T, framer jsonrpc2.Framer) { |
| 135 |
stacktest.NoLeak(t) |
| 136 |
ctx := eventtest.NewContext(context.Background(), t) |
| 137 |
listener, err := jsonrpc2.NetPipe(ctx) |
| 138 |
if err != nil { |
| 139 |
t.Fatal(err) |
| 140 |
} |
| 141 |
server, err := jsonrpc2.Serve(ctx, listener, binder{framer, nil}) |
| 142 |
if err != nil { |
| 143 |
t.Fatal(err) |
| 144 |
} |
| 145 |
defer func() { |
| 146 |
listener.Close() |
| 147 |
server.Wait() |
| 148 |
}() |
| 149 |
|
| 150 |
for _, test := range callTests { |
| 151 |
t.Run(test.Name(), func(t *testing.T) { |
| 152 |
client, err := jsonrpc2.Dial(ctx, |
| 153 |
listener.Dialer(), binder{framer, func(h *handler) { |
| 154 |
defer h.conn.Close() |
| 155 |
ctx := eventtest.NewContext(ctx, t) |
| 156 |
test.Invoke(t, ctx, h) |
| 157 |
if call, ok := test.(*call); ok { |
| 158 |
|
| 159 |
(*echo)(call).Invoke(t, ctx, h) |
| 160 |
} |
| 161 |
}}) |
| 162 |
if err != nil { |
| 163 |
t.Fatal(err) |
| 164 |
} |
| 165 |
client.Wait() |
| 166 |
}) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
func (test notify) Name() string { return test.method } |
| 171 |
func (test notify) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 172 |
if err := h.conn.Notify(ctx, test.method, test.params); err != nil { |
| 173 |
t.Fatalf("%v:Notify failed: %v", test.method, err) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func (test call) Name() string { return test.method } |
| 178 |
func (test call) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 179 |
results := newResults(test.expect) |
| 180 |
if err := h.conn.Call(ctx, test.method, test.params).Await(ctx, results); err != nil { |
| 181 |
t.Fatalf("%v:Call failed: %v", test.method, err) |
| 182 |
} |
| 183 |
verifyResults(t, test.method, results, test.expect) |
| 184 |
} |
| 185 |
|
| 186 |
func (test callErr) Name() string { return test.method } |
| 187 |
func (test callErr) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 188 |
var results interface{} |
| 189 |
if err := h.conn.Call(ctx, test.method, test.params).Await(ctx, &results); err != nil { |
| 190 |
if serr := err.Error(); !strings.Contains(serr, test.expectErr) { |
| 191 |
t.Fatalf("%v:Call failed but with unexpected error: %q does not contain %q", test.method, serr, test.expectErr) |
| 192 |
} |
| 193 |
return |
| 194 |
} |
| 195 |
t.Fatalf("%v:Call succeeded (%v) but should have failed with error containing %q", test.method, results, test.expectErr) |
| 196 |
} |
| 197 |
|
| 198 |
func (test echo) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 199 |
results := newResults(test.expect) |
| 200 |
if err := h.conn.Call(ctx, "echo", []interface{}{test.method, test.params}).Await(ctx, results); err != nil { |
| 201 |
t.Fatalf("%v:Echo failed: %v", test.method, err) |
| 202 |
} |
| 203 |
verifyResults(t, test.method, results, test.expect) |
| 204 |
} |
| 205 |
|
| 206 |
func (test async) Name() string { return test.name } |
| 207 |
func (test async) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 208 |
h.calls[test.name] = h.conn.Call(ctx, test.method, test.params) |
| 209 |
} |
| 210 |
|
| 211 |
func (test collect) Name() string { return test.name } |
| 212 |
func (test collect) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 213 |
o := h.calls[test.name] |
| 214 |
results := newResults(test.expect) |
| 215 |
err := o.Await(ctx, results) |
| 216 |
switch { |
| 217 |
case test.fails && err == nil: |
| 218 |
t.Fatalf("%v:Collect was supposed to fail", test.name) |
| 219 |
case !test.fails && err != nil: |
| 220 |
t.Fatalf("%v:Collect failed: %v", test.name, err) |
| 221 |
} |
| 222 |
verifyResults(t, test.name, results, test.expect) |
| 223 |
} |
| 224 |
|
| 225 |
func (test cancel) Name() string { return test.name } |
| 226 |
func (test cancel) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 227 |
o := h.calls[test.name] |
| 228 |
if err := h.conn.Notify(ctx, "cancel", &cancelParams{o.ID().Raw().(int64)}); err != nil { |
| 229 |
t.Fatalf("%v:Collect failed: %v", test.name, err) |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
func (test sequence) Name() string { return test.name } |
| 234 |
func (test sequence) Invoke(t *testing.T, ctx context.Context, h *handler) { |
| 235 |
for _, child := range test.tests { |
| 236 |
child.Invoke(t, ctx, h) |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
func newResults(expect interface{}) interface{} { |
| 242 |
switch e := expect.(type) { |
| 243 |
case []interface{}: |
| 244 |
var r []interface{} |
| 245 |
for _, v := range e { |
| 246 |
r = append(r, reflect.New(reflect.TypeOf(v)).Interface()) |
| 247 |
} |
| 248 |
return r |
| 249 |
case nil: |
| 250 |
return nil |
| 251 |
default: |
| 252 |
return reflect.New(reflect.TypeOf(expect)).Interface() |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
func verifyResults(t *testing.T, method string, results interface{}, expect interface{}) { |
| 258 |
if expect == nil { |
| 259 |
if results != nil { |
| 260 |
t.Errorf("%v:Got results %+v where none expeted", method, expect) |
| 261 |
} |
| 262 |
return |
| 263 |
} |
| 264 |
val := reflect.Indirect(reflect.ValueOf(results)).Interface() |
| 265 |
if !reflect.DeepEqual(val, expect) { |
| 266 |
t.Errorf("%v:Results are incorrect, got %+v expect %+v", method, val, expect) |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
func (b binder) Bind(ctx context.Context, conn *jsonrpc2.Connection) (jsonrpc2.ConnectionOptions, error) { |
| 271 |
h := &handler{ |
| 272 |
conn: conn, |
| 273 |
waitersBox: make(chan map[string]chan struct{}, 1), |
| 274 |
calls: make(map[string]*jsonrpc2.AsyncCall), |
| 275 |
} |
| 276 |
h.waitersBox <- make(map[string]chan struct{}) |
| 277 |
if b.runTest != nil { |
| 278 |
go b.runTest(h) |
| 279 |
} |
| 280 |
return jsonrpc2.ConnectionOptions{ |
| 281 |
Framer: b.framer, |
| 282 |
Preempter: h, |
| 283 |
Handler: h, |
| 284 |
}, nil |
| 285 |
} |
| 286 |
|
| 287 |
func (h *handler) waiter(name string) chan struct{} { |
| 288 |
waiters := <-h.waitersBox |
| 289 |
defer func() { h.waitersBox <- waiters }() |
| 290 |
waiter, found := waiters[name] |
| 291 |
if !found { |
| 292 |
waiter = make(chan struct{}) |
| 293 |
waiters[name] = waiter |
| 294 |
} |
| 295 |
return waiter |
| 296 |
} |
| 297 |
|
| 298 |
func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) { |
| 299 |
switch req.Method { |
| 300 |
case "unblock": |
| 301 |
var name string |
| 302 |
if err := json.Unmarshal(req.Params, &name); err != nil { |
| 303 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 304 |
} |
| 305 |
close(h.waiter(name)) |
| 306 |
return nil, nil |
| 307 |
case "peek": |
| 308 |
if len(req.Params) > 0 { |
| 309 |
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams) |
| 310 |
} |
| 311 |
return h.accumulator, nil |
| 312 |
case "cancel": |
| 313 |
var params cancelParams |
| 314 |
if err := json.Unmarshal(req.Params, ¶ms); err != nil { |
| 315 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 316 |
} |
| 317 |
h.conn.Cancel(jsonrpc2.Int64ID(params.ID)) |
| 318 |
return nil, nil |
| 319 |
default: |
| 320 |
return nil, jsonrpc2.ErrNotHandled |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{}, error) { |
| 325 |
switch req.Method { |
| 326 |
case "no_args": |
| 327 |
if len(req.Params) > 0 { |
| 328 |
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams) |
| 329 |
} |
| 330 |
return true, nil |
| 331 |
case "one_string": |
| 332 |
var v string |
| 333 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 334 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 335 |
} |
| 336 |
return "got:" + v, nil |
| 337 |
case "one_number": |
| 338 |
var v int |
| 339 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 340 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 341 |
} |
| 342 |
return fmt.Sprintf("got:%d", v), nil |
| 343 |
case "set": |
| 344 |
var v int |
| 345 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 346 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 347 |
} |
| 348 |
h.accumulator = v |
| 349 |
return nil, nil |
| 350 |
case "add": |
| 351 |
var v int |
| 352 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 353 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 354 |
} |
| 355 |
h.accumulator += v |
| 356 |
return nil, nil |
| 357 |
case "get": |
| 358 |
if len(req.Params) > 0 { |
| 359 |
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams) |
| 360 |
} |
| 361 |
return h.accumulator, nil |
| 362 |
case "join": |
| 363 |
var v []string |
| 364 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 365 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 366 |
} |
| 367 |
return path.Join(v...), nil |
| 368 |
case "echo": |
| 369 |
var v []interface{} |
| 370 |
if err := json.Unmarshal(req.Params, &v); err != nil { |
| 371 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 372 |
} |
| 373 |
var result interface{} |
| 374 |
err := h.conn.Call(ctx, v[0].(string), v[1]).Await(ctx, &result) |
| 375 |
return result, err |
| 376 |
case "wait": |
| 377 |
var name string |
| 378 |
if err := json.Unmarshal(req.Params, &name); err != nil { |
| 379 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 380 |
} |
| 381 |
select { |
| 382 |
case <-h.waiter(name): |
| 383 |
return true, nil |
| 384 |
case <-ctx.Done(): |
| 385 |
return nil, ctx.Err() |
| 386 |
case <-time.After(time.Second): |
| 387 |
return nil, errors.Errorf("wait for %q timed out", name) |
| 388 |
} |
| 389 |
case "fork": |
| 390 |
var name string |
| 391 |
if err := json.Unmarshal(req.Params, &name); err != nil { |
| 392 |
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err) |
| 393 |
} |
| 394 |
waitFor := h.waiter(name) |
| 395 |
go func() { |
| 396 |
select { |
| 397 |
case <-waitFor: |
| 398 |
h.conn.Respond(req.ID, true, nil) |
| 399 |
case <-ctx.Done(): |
| 400 |
h.conn.Respond(req.ID, nil, ctx.Err()) |
| 401 |
case <-time.After(time.Second): |
| 402 |
h.conn.Respond(req.ID, nil, errors.Errorf("wait for %q timed out", name)) |
| 403 |
} |
| 404 |
}() |
| 405 |
return nil, jsonrpc2.ErrAsyncResponse |
| 406 |
default: |
| 407 |
return nil, jsonrpc2.ErrNotHandled |
| 408 |
} |
| 409 |
} |