-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchangesingle_test.go
199 lines (176 loc) · 4.27 KB
/
exchangesingle_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
package harpy_test
import (
"context"
"encoding/json"
"errors"
. "github.com/dogmatiq/harpy"
. "github.com/dogmatiq/harpy/internal/fixtures"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
var _ = Describe("func Exchange() (single request)", func() {
var (
exchanger *ExchangerStub
request Request
reader *RequestSetReaderStub
writer *ResponseWriterStub
logs *observer.ObservedLogs
logger ExchangeLogger
closed bool
)
BeforeEach(func() {
exchanger = &ExchangerStub{}
exchanger.CallFunc = func(
_ context.Context,
req Request,
) Response {
return SuccessResponse{
Version: "2.0",
RequestID: req.ID,
Result: json.RawMessage(`"<result>"`),
}
}
request = Request{
Version: "2.0",
ID: json.RawMessage(`123`),
Method: "<method>",
Parameters: json.RawMessage(`[]`),
}
reader = &RequestSetReaderStub{
ReadFunc: func(context.Context) (RequestSet, error) {
return RequestSet{
Requests: []Request{request},
IsBatch: false,
}, nil
},
}
writer = &ResponseWriterStub{
WriteErrorFunc: func(ErrorResponse) error {
panic("unexpected call to WriteErrorFunc()")
},
WriteUnbatchedFunc: func(Response) error {
panic("unexpected call to WriteUnbatchedFunc()")
},
WriteBatchedFunc: func(Response) error {
panic("unexpected call to WriteBatchedFunc()")
},
CloseFunc: func() error {
Expect(closed).To(BeFalse(), "response writer was closed multiple times")
closed = true
return nil
},
}
var core zapcore.Core
core, logs = observer.New(zapcore.DebugLevel)
logger = NewZapExchangeLogger(zap.New(core))
closed = false
})
AfterEach(func() {
// The response writer must always be closed.
Expect(closed).To(BeTrue())
})
When("the request is a call", func() {
It("passes the request to the exchanger and writes an unbatched response", func() {
next := exchanger.CallFunc
exchanger.CallFunc = func(
ctx context.Context,
req Request,
) Response {
Expect(req).To(Equal(request))
return next(ctx, req)
}
writer.WriteUnbatchedFunc = func(res Response) error {
Expect(res).To(Equal(SuccessResponse{
Version: "2.0",
RequestID: json.RawMessage(`123`),
Result: json.RawMessage(`"<result>"`),
}))
return nil
}
err := Exchange(
context.Background(),
exchanger,
reader,
writer,
logger,
)
Expect(err).ShouldNot(HaveOccurred())
Expect(logs.AllUntimed()).To(ContainElement(
observer.LoggedEntry{
Entry: zapcore.Entry{
Level: zapcore.InfoLevel,
Message: `call`,
},
Context: []zapcore.Field{
zap.String("method", "<method>"),
zap.Int("param_size", 2),
zap.Int("result_size", 10),
},
},
))
})
It("logs and returns errors the occur when writing the response", func() {
writer.WriteUnbatchedFunc = func(Response) error {
return errors.New("<write error>")
}
err := Exchange(
context.Background(),
exchanger,
reader,
writer,
logger,
)
Expect(err).To(MatchError("<write error>"))
Expect(logs.AllUntimed()).To(ContainElement(
observer.LoggedEntry{
Entry: zapcore.Entry{
Level: zapcore.ErrorLevel,
Message: `unable to write JSON-RPC response`,
},
Context: []zapcore.Field{
zap.String("error", "<write error>"),
},
},
))
})
})
When("the request is a notification", func() {
BeforeEach(func() {
request.ID = nil
})
It("passes the request to the exchanger and does not write any responses", func() {
called := true
exchanger.NotifyFunc = func(
_ context.Context,
req Request,
) error {
Expect(req).To(Equal(request))
return nil
}
err := Exchange(
context.Background(),
exchanger,
reader,
writer,
logger,
)
Expect(err).ShouldNot(HaveOccurred())
Expect(called).To(BeTrue())
Expect(logs.AllUntimed()).To(ContainElement(
observer.LoggedEntry{
Entry: zapcore.Entry{
Level: zapcore.InfoLevel,
Message: `notify`,
},
Context: []zapcore.Field{
zap.String("method", "<method>"),
zap.Int("param_size", 2),
},
},
))
})
})
})