This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tunnel_test.go
288 lines (246 loc) · 7.59 KB
/
tunnel_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Copyright (c) 2012-2018 Red Hat, Inc.
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
// Contributors:
// Red Hat, Inc. - initial API and implementation
//
package jsonrpc_test
import (
"bytes"
"encoding/json"
"github.com/eclipse/che-go-jsonrpc/jsonrpctest"
"testing"
"time"
"github.com/eclipse/che-go-jsonrpc"
)
func TestChannelSaysHello(t *testing.T) {
beforeConnected := time.Now()
// initialization routine
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
// send hello notification
tunnel.SayHello()
// wait while this notification is received by connection
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(err)
}
// check the received notification is expected one
helloNotification := &jsonrpc.TunnelNotification{}
connRecorder.UnmarshalRequestParams(0, helloNotification)
if helloNotification.ChannelID != tunnel.ID() {
t.Fatalf("Tunnel ids are different %s != %s", helloNotification.ChannelID, tunnel.ID())
}
if helloNotification.Text != "Hello!" {
t.Fatalf("Expected text to be 'Hello' but it is %s", helloNotification.Text)
}
now := time.Now()
if !beforeConnected.Before(helloNotification.Time) || !helloNotification.Time.Before(now) {
t.Fatalf("Expected event time to be between %v < x < %v", beforeConnected, now)
}
}
// X Notification -> X'
func TestSendingNotification(t *testing.T) {
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
method := "event:my-event"
tunnel.Notify(method, &testStruct{"Test"})
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(err)
}
// check request
req, err := connRecorder.GetRequest(0)
if err != nil {
t.Fatal(err)
}
if req.Method != method {
t.Fatalf("Expected to send %s method but sent %s", method, req.Method)
}
if !req.IsNotification() {
t.Fatalf("Expected request to be notification but it has id %v", req.ID)
}
// check params
event := &testStruct{}
json.Unmarshal(req.Params, event)
if event.Data != "Test" {
t.Fatal("Expected event data to be 'Test'")
}
}
// X Request -> X'
func TestSendingRequest(t *testing.T) {
tunnel, connRecorder, _ := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
method := "domain.doSomething"
tunnel.Request(method, &testStruct{"Test"}, func(r []byte, err *jsonrpc.Error) {
// do nothing
})
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(t)
}
// check request
req, err := connRecorder.GetRequest(0)
if err != nil {
t.Fatal(err)
}
if req.Method != method {
t.Fatalf("Expected to send %s method but sent %s", method, req.Method)
}
if req.IsNotification() {
t.Fatal("Expected request not to be notification but it does not have id")
}
// check params
event := &testStruct{}
json.Unmarshal(req.Params, event)
if event.Data != "Test" {
t.Fatal("Expected event data to be 'Test'")
}
}
// X' Request -> X
func TestReceivingRequest(t *testing.T) {
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
// prepare a test request object and put it in native connection read stream
reqBody, err := json.Marshal(testStruct{"Test"})
if err != nil {
t.Fatal(err)
}
sentReq := &jsonrpc.Request{
ID: "1",
Method: "domain.doSomething",
Params: reqBody,
}
connRecorder.PushNext(sentReq)
// tunnel needs some time to call the handler
if err := reqRecorder.WaitUntil(jsonrpctest.ResponseArrivedAtLeast(1)); err != nil {
t.Fatal(err)
}
receivedReq, _ := reqRecorder.Get(0)
if string(receivedReq.Params) != string(sentReq.Params) {
t.Fatalf("Sent params %s but received %s", string(sentReq.Params), string(receivedReq.Params))
}
}
// X' Request -> X
// X' <- Response X
func TestSendingResponseBack(t *testing.T) {
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
// prepare a test request object and put it in native connection read stream
reqBody, err := json.Marshal(testStruct{"Test"})
if err != nil {
t.Fatal(err)
}
req := &jsonrpc.Request{
ID: 1,
Method: "domain.doSomething",
Params: reqBody,
}
connRecorder.PushNext(req)
// wait for request to arrive
if err := reqRecorder.WaitUntil(jsonrpctest.ResponseArrivedAtLeast(1)); err != nil {
t.Fatal(t)
}
// respond back
_, transmitter := reqRecorder.Get(0)
sentBody := testStruct{"response test data"}
transmitter.Send(sentBody)
// wait for response to be written
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(err)
}
resp, err := connRecorder.GetResponse(0)
if err != nil {
t.Fatal(err)
}
// check the response is ok
if resp.ID != req.ID {
t.Fatalf("Expected ids to be the same but resp id %v != req id %v", resp.ID, req.ID)
}
if resp.Error != nil {
t.Fatalf("Expected to get response without error, but got %d %s", resp.Error.Code, resp.Error.Message)
}
respBody := testStruct{}
if err := json.Unmarshal(resp.Result, &respBody); err != nil {
t.Fatal(err)
}
if respBody != sentBody {
t.Fatalf("Expected to get the same body but got %v != %v", respBody, sentBody)
}
}
// X Request -> X'
// X <- Response X'
func TestRequestResponseHandling(t *testing.T) {
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
respChan := make(chan []byte, 1)
// X Request -> X'
tunnel.Request("domain.doSomething", &testStruct{"req-params"}, func(r []byte, err *jsonrpc.Error) {
respChan <- r
})
// wait for the response and catch its id
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(err)
}
req, err := connRecorder.GetRequest(0)
if err != nil {
t.Fatal(t)
}
// X' Response -> X
repsBody := testStruct{"resp-body"}
marshaledBody, err := json.Marshal(&repsBody)
if err != nil {
t.Fatal(err)
}
connRecorder.PushNext(&jsonrpc.Response{
ID: req.ID,
Result: marshaledBody,
})
// wait for the response handler function to be called
select {
case resp := <-respChan:
if bytes.Compare(resp, marshaledBody) != 0 {
t.Fatalf("Received different response body %s != %s", string(resp), string(marshaledBody))
}
case <-time.After(time.Second * 2):
t.Fatal("Didn't receieve the response in 2seconds")
}
}
func TestSendingBrokenData(t *testing.T) {
tunnel, connRecorder, reqRecorder := jsonrpctest.NewTmpTunnel(2*time.Second)
defer tunnel.Close()
defer reqRecorder.Close()
connRecorder.PushNextRaw([]byte("{not-a-json}"))
if err := connRecorder.WaitUntil(jsonrpctest.WriteCalledAtLeast(1)); err != nil {
t.Fatal(err)
}
response, err := connRecorder.GetResponse(0)
if err != nil {
t.Fatal(err)
}
if response.ID != nil {
t.Fatal("Response id must be nill")
}
if response.Version != jsonrpc.DefaultVersion {
t.Fatalf("Exected response version to be %sbut it is %s", jsonrpc.DefaultVersion, response.Version)
}
if response.Result != nil {
t.Fatalf("Expected response result to be nil, but it is %v", string(response.Result))
}
if response.Error == nil {
t.Fatal("Expected response to contain error")
}
if response.Error.Code != jsonrpc.ParseErrorCode {
t.Fatalf("Expected error code to be %d but it is %d", jsonrpc.ParseErrorCode, response.Error.Code)
}
}
type testStruct struct {
Data string `json:"data"`
}