-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhandler_test.go
More file actions
69 lines (62 loc) · 1.74 KB
/
Copy pathhandler_test.go
File metadata and controls
69 lines (62 loc) · 1.74 KB
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
// Copyright 2026 The Go Language Server Authors. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"context"
"errors"
"testing"
)
func TestMethodNotFoundHandler(t *testing.T) {
t.Parallel()
tests := map[string]struct {
req Request
wantErr bool
}{
"error: call is answered with ErrMethodNotFound": {
req: Request{id: NewNumberID(1), method: "missing", isCall: true},
wantErr: true,
},
"success: notification is dropped without failing the connection": {
req: Request{method: "missing"},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
result, err := MethodNotFoundHandler(t.Context(), &tt.req)
if result != nil {
t.Fatalf("result = %v, want nil", result)
}
if tt.wantErr && !errors.Is(err, ErrMethodNotFound) {
t.Fatalf("err = %v, want ErrMethodNotFound", err)
}
if !tt.wantErr && err != nil {
t.Fatalf("err = %v, want nil for a notification", err)
}
})
}
}
func TestCancelHandlerCancellerUnknownID(t *testing.T) {
t.Parallel()
// A canceller for an id that is not being handled must be a safe no-op.
_, canceller := CancelHandler(MethodNotFoundHandler)
canceller(NewNumberID(999))
canceller(NewStringID("nope"))
}
func TestAsyncOnNonRequestContextIsNoop(t *testing.T) {
t.Parallel()
// Calling Async on a context without a release token must not panic.
Async(t.Context())
}
func TestAsyncDoubleCallPanics(t *testing.T) {
t.Parallel()
rel := &releaser{ch: make(chan struct{})}
ctx := context.WithValue(t.Context(), asyncKey{}, rel)
Async(ctx) // first hard release
defer func() {
if recover() == nil {
t.Fatal("expected a panic on a second Async call")
}
}()
Async(ctx) // second hard release must panic
}