-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsyncclient_test.go
More file actions
238 lines (218 loc) · 6.52 KB
/
Copy pathsyncclient_test.go
File metadata and controls
238 lines (218 loc) · 6.52 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
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
// Copyright 2026 The Go Language Server Authors. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2
import (
"context"
"net"
"sync"
"testing"
"time"
gocmp "github.com/google/go-cmp/cmp"
)
// syncClientServer starts an ordinary Conn server answering "echo" (params back
// under "got"), "fail" (an InvalidParams error), and "void" (nil result), paired
// with a SyncClient over a net.Pipe with the given framer. It returns the client
// and a cleanup function.
func syncClientServer(t *testing.T, framer Framer) (*SyncClient, func()) {
t.Helper()
ca, cb := net.Pipe()
client, err := NewSyncClient(framer(ca))
if err != nil {
t.Fatalf("NewSyncClient: %v", err)
}
server := NewConn(framer(cb))
ctx := t.Context()
server.Go(ctx, func(ctx context.Context, req *Request) (any, error) {
switch req.Method() {
case "echo":
return raw(`{"got":` + string(orNull(req.Params())) + `}`), nil
case "fail":
return nil, NewError(InvalidParams, "bad params")
default:
return nil, nil
}
})
cleanup := func() {
_ = client.Close()
_ = server.Close()
<-server.Done()
}
return client, cleanup
}
func TestSyncClientCallRoundTrip(t *testing.T) {
t.Parallel()
for name, framer := range map[string]Framer{
"ndjson": NewNDJSONStream,
"header": NewHeaderStream,
} {
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := t.Context()
client, cleanup := syncClientServer(t, framer)
defer cleanup()
var got RawMessage
id, err := client.Call(ctx, "echo", raw(`{"x":1}`), &got)
if err != nil {
t.Fatalf("Call echo: %v", err)
}
if !id.IsValid() {
t.Fatalf("Call returned invalid id")
}
if want := `{"got":{"x":1}}`; string(got) != want {
t.Fatalf("echo result: got %s want %s", got, want)
}
// A second call reuses the single read loop and gets a distinct id.
var got2 RawMessage
id2, err := client.Call(ctx, "echo", raw(`{"x":2}`), &got2)
if err != nil {
t.Fatalf("Call echo 2: %v", err)
}
if id2 == id {
t.Fatalf("second call reused id %v", id)
}
if want := `{"got":{"x":2}}`; string(got2) != want {
t.Fatalf("echo result 2: got %s want %s", got2, want)
}
})
}
}
func TestSyncClientCallError(t *testing.T) {
t.Parallel()
ctx := t.Context()
client, cleanup := syncClientServer(t, NewNDJSONStream)
defer cleanup()
var got RawMessage
if _, err := client.Call(ctx, "fail", nil, &got); err == nil {
t.Fatal("Call fail: expected error, got nil")
} else if e, ok := err.(*Error); !ok || e.Code != InvalidParams {
t.Fatalf("Call fail: got %v want InvalidParams *Error", err)
}
// The client is still usable after an error response.
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
t.Fatalf("Call void after error: %v", err)
}
}
func TestSyncClientNotifyThenCall(t *testing.T) {
t.Parallel()
ctx := t.Context()
ca, cb := net.Pipe()
client, err := NewSyncClient(NewNDJSONStream(ca))
if err != nil {
t.Fatalf("NewSyncClient: %v", err)
}
server := NewConn(NewNDJSONStream(cb))
var notes sync.WaitGroup
notes.Add(1)
var seen string
var seenMu sync.Mutex
serverCtx := t.Context()
server.Go(serverCtx, func(ctx context.Context, req *Request) (any, error) {
if req.Method() == "note" {
seenMu.Lock()
seen = string(orNull(req.Params()))
seenMu.Unlock()
notes.Done()
}
return nil, nil
})
defer func() {
_ = client.Close()
_ = server.Close()
<-server.Done()
}()
// A notification produces no response and does not block the next call.
if err := client.Notify(ctx, "note", raw(`"hi"`)); err != nil {
t.Fatalf("Notify: %v", err)
}
if _, err := client.Call(ctx, "void", nil, nil); err != nil {
t.Fatalf("Call void after notify: %v", err)
}
done := make(chan struct{})
go func() { notes.Wait(); close(done) }()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("server did not observe the notification")
}
seenMu.Lock()
defer seenMu.Unlock()
if seen != `"hi"` {
t.Fatalf("notification params: got %s want %q", seen, `"hi"`)
}
}
// TestSyncClientEquivalentToConn asserts the SyncClient produces the same result
// bytes as an ordinary Conn client for the same request, proving the mode change
// does not alter observable RPC behavior.
func TestSyncClientEquivalentToConn(t *testing.T) {
t.Parallel()
ctx := t.Context()
// SyncClient path.
sc, scCleanup := syncClientServer(t, NewNDJSONStream)
defer scCleanup()
var scResult RawMessage
if _, err := sc.Call(ctx, "echo", raw(`{"a":[1,2,3]}`), &scResult); err != nil {
t.Fatalf("SyncClient echo: %v", err)
}
// Conn path.
ca, cb := net.Pipe()
cc := NewConn(NewNDJSONStream(ca))
server := NewConn(NewNDJSONStream(cb))
connCtx := t.Context()
cc.Go(connCtx, MethodNotFoundHandler)
server.Go(connCtx, func(ctx context.Context, req *Request) (any, error) {
return raw(`{"got":` + string(orNull(req.Params())) + `}`), nil
})
defer func() {
_ = cc.Close()
<-cc.Done()
_ = server.Close()
<-server.Done()
}()
var connResult RawMessage
if _, err := cc.Call(ctx, "echo", raw(`{"a":[1,2,3]}`), &connResult); err != nil {
t.Fatalf("Conn echo: %v", err)
}
if diff := gocmp.Diff(string(connResult), string(scResult)); diff != "" {
t.Fatalf("SyncClient vs Conn result mismatch (-conn +sync):\n%s", diff)
}
}
// TestCallMarshalErrorZeroID locks the cross-client contract that a local marshal
// failure — which happens before anything is sent or registered — returns a zero
// ID, never a "would-have-been" id. Conn and SyncClient must behave identically.
func TestCallMarshalErrorZeroID(t *testing.T) {
t.Parallel()
// A channel cannot be marshaled by any codec, so marshalParams fails before
// the call is assigned an id, registered, or written.
badParams := make(chan int)
t.Run("conn", func(t *testing.T) {
t.Parallel()
ca, cb := net.Pipe()
defer cb.Close()
c := NewConn(NewNDJSONStream(ca))
defer c.Close()
id, err := c.Call(t.Context(), "m", badParams, nil)
if err == nil {
t.Fatal("Conn.Call: expected a marshal error, got nil")
}
if id.IsValid() {
t.Fatalf("Conn.Call returned valid id %v on marshal error; want zero ID", id)
}
})
t.Run("syncclient", func(t *testing.T) {
t.Parallel()
ca, cb := net.Pipe()
defer cb.Close()
sc, err := NewSyncClient(NewNDJSONStream(ca))
if err != nil {
t.Fatalf("NewSyncClient: %v", err)
}
defer sc.Close()
id, err := sc.Call(t.Context(), "m", badParams, nil)
if err == nil {
t.Fatal("SyncClient.Call: expected a marshal error, got nil")
}
if id.IsValid() {
t.Fatalf("SyncClient.Call returned valid id %v on marshal error; want zero ID", id)
}
})
}