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
/
internals_test.go
212 lines (188 loc) · 5.76 KB
/
internals_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
//
// 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
import (
"encoding/json"
"errors"
"sync"
"testing"
"time"
)
func TestRespTransmitterSendsBody(t *testing.T) {
tunnel := &Tunnel{jsonOut: make(chan interface{}, 1)}
transmitter := &respTransmitter{
reqID: "1",
tunnel: tunnel,
once: &sync.Once{},
done: make(chan bool, 1),
}
type body struct{ Message string }
sentBody := body{"transmitted response"}
transmitter.Send(&sentBody)
resp := waitResp(t, tunnel.jsonOut)
if resp.ID != transmitter.reqID {
t.Fatalf("Expected transmitter to use request id '%v' as response id but used '%v' ", transmitter.reqID, resp.ID)
}
if resp.Error != nil {
t.Fatalf("Expected transmitter sends no error in response, but found %v", resp.Error)
}
if resp.Version != DefaultVersion {
t.Fatalf("Expected respnonse version to be %s but found %s", DefaultVersion, resp.Version)
}
respBody := body{}
if err := json.Unmarshal(resp.Result, &respBody); err != nil {
t.Fatal(err)
}
if respBody != sentBody {
t.Fatalf("Expected to receieve response result %v but received %v", sentBody, respBody)
}
}
func TestRespTransmitterSendsError(t *testing.T) {
c := &Tunnel{jsonOut: make(chan interface{}, 1)}
transmitter := &respTransmitter{
reqID: "1",
tunnel: c,
once: &sync.Once{},
done: make(chan bool, 1),
}
errMessage := "no!no!no!"
transmitter.SendError(NewArgsError(errors.New(errMessage)))
resp := waitResp(t, c.jsonOut)
if resp.ID != transmitter.reqID {
t.Fatalf("Expected transmitter to use request id '%v' as response id but used '%v' ", transmitter.reqID, resp.ID)
}
if resp.Version != DefaultVersion {
t.Fatalf("Expected respnonse version to be %s but found %s", DefaultVersion, resp.Version)
}
if resp.Result != nil {
t.Fatalf("Expected transmitter sends no result in response, but result is %v", resp.Result)
}
if resp.Error == nil {
t.Fatal("Expected transmitter sends error in response")
}
if resp.Error.Message != errMessage {
t.Fatalf("Expected to receive error message %s but received %s", errMessage, resp.Error.Message)
}
if resp.Error.Code != InvalidParamsErrorCode {
t.Fatalf("Expected to receive error code %d but received %d", InvalidParamsErrorCode, resp.Error.Code)
}
}
func TestTransmitterDoesNotSendBodyTwice(t *testing.T) {
c := &Tunnel{jsonOut: make(chan interface{}, 2)}
transmitter := &respTransmitter{
reqID: "1",
tunnel: c,
once: &sync.Once{},
done: make(chan bool, 1),
}
type body struct{ Message string }
transmitter.Send(&body{"hello"})
transmitter.Send(&body{"hello"})
// read first time
select {
case <-c.jsonOut:
default:
t.Fatal("Must send first message to the tunnel")
}
// must not read the second time
select {
case <-c.jsonOut:
t.Fatal("Must not send second second message to the tunnel")
default:
// ok
}
}
func TestTransmitterRespondsIfTimeoutIsReached(t *testing.T) {
c := &Tunnel{jsonOut: make(chan interface{}, 1)}
transmitter := &respTransmitter{
reqID: "1",
tunnel: c,
once: &sync.Once{},
done: make(chan bool, 1),
}
transmitter.watch(0)
resp := waitResp(t, c.jsonOut)
if resp.ID != transmitter.reqID {
t.Fatalf("Expected transmitter to use request id '%v' as response id but used '%v' ", transmitter.reqID, resp.ID)
}
if resp.Version != DefaultVersion {
t.Fatalf("Expected respnonse version to be %s but found %s", DefaultVersion, resp.Version)
}
if resp.Error == nil {
t.Fatal("Expected transmitter sends error in response")
}
if resp.Error.Code != TimeoutErrorCode {
t.Fatalf("Expected to receive error code %d but received %d", TimeoutErrorCode, resp.Error.Code)
}
}
func TestRequestQueueRemovesResponsesAfterTimeout(t *testing.T) {
q := &requestQ{pairs: make(map[int64]*rqPair)}
now := time.Now()
cases := []struct {
id int64
time time.Time
mustBeDropped bool
}{
{id: 1, time: now.Add(-time.Second * 30), mustBeDropped: true},
{id: 2, time: now.Add(-time.Minute * 30), mustBeDropped: true},
{id: 3, time: now.Add(-time.Hour * 30), mustBeDropped: true},
{id: 4, time: now.Add(-time.Second * 15), mustBeDropped: true},
{id: 5, time: now.Add(-time.Second * 14), mustBeDropped: false},
{id: 6, time: now.Add(-time.Second * 5), mustBeDropped: false},
{id: 7, time: now, mustBeDropped: false},
}
dropped := 0
mustBeDropped := 0
for _, _case := range cases {
if _case.mustBeDropped {
mustBeDropped++
q.add(_case.id, &Request{}, _case.time, func(res []byte, err *Error) {
if err == nil {
t.Fatal("Must receive timeout error, but no error received")
}
if err.Code != TimeoutErrorCode {
t.Fatalf("Must receive TimeoutError, but received %T %s", err, err.Error())
}
dropped++
})
} else {
q.add(_case.id, &Request{}, _case.time, func(res []byte, err *Error) {
t.Fatalf("The handler for request %d must not be called", _case.id)
})
}
}
q.allowedResponseDelay = time.Second * 15
q.dropOutdatedOnce()
if dropped != mustBeDropped {
t.Fatalf("Must be dropped %d but actually dropped %d", mustBeDropped, dropped)
}
for _, _case := range cases {
if _case.mustBeDropped {
if _, ok := q.pairs[_case.id]; ok {
t.Fatalf("The request with id %d must not be present in q", _case.id)
}
}
}
}
func waitResp(t *testing.T, c chan interface{}) *Response {
select {
case v := <-c:
if r, ok := v.(*Response); !ok {
t.Fatal("Expected to receive response")
} else {
return r
}
case <-time.After(time.Second):
t.Fatal("Didn't receive response in specified timeout")
}
return nil
}