-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasync_request_test.go
More file actions
276 lines (267 loc) · 8.18 KB
/
async_request_test.go
File metadata and controls
276 lines (267 loc) · 8.18 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
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
package middleware
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func handleResponse(handler func(w http.ResponseWriter, r *http.Request) HandlerTask) http.HandlerFunc {
// create wrapper handler
return func(w http.ResponseWriter, r *http.Request) {
// call handler
job := handler(w, r)
// read the result
data, err := job.Resolve()
// check error
switch err {
// job was successfully done
case nil:
// send the response
json.NewEncoder(w).Encode(data)
// do nothing: middleware will send the response depending on request type
case ErrNotCompleted:
// return or ignore
return
// done with error
default:
// send error
http.Error(w, err.Error(), 500)
}
}
}
func handlerAsync(w http.ResponseWriter, r *http.Request) HandlerTask {
// get job from context
job, _ := GetHandlerTask(r.Context())
// start the job if it is new one, otherwise pass the job to handleResponse func
if job.Status() == StatusWaiting {
// define reply type
var reply []int
// start the job
job.Do(r.Context(), func(stop <-chan struct{}) error {
// emulate task which takes a lot of time to complete
for i := 0; i < 10; i++ {
// add values one by one
reply = append(reply, i)
// catch stop signal or wait
select {
// request timeout (context deadline - stopped externally)
case <-stop:
// do something to terminate handler
return nil
// wait
case <-time.After(10 * time.Millisecond):
}
}
// do not forget to complete the task (otherwise it will stay "in progress" forever)
return job.Complete(reply, nil)
})
}
// return current task to be processed by handleResponse func
return job
}
func Test_task_Complete(t *testing.T) {
t.Run("should throw an error trying to complete the task which was already done", func(t *testing.T) {
job := &task{status: StatusDone}
if err := job.Complete(nil, nil); err != ErrAlreadyDone {
t.Errorf("should return error: %s", ErrAlreadyDone)
}
})
t.Run("should throw an error trying to complete the task which was not started", func(t *testing.T) {
job := &task{status: StatusWaiting}
if err := job.Complete(nil, nil); err != ErrNotStarted {
t.Errorf("should return error: %s", ErrNotStarted)
}
})
t.Run("should pass actual error trying to complete the task which is in progress", func(t *testing.T) {
job := &task{status: StatusInProgress}
actual := errors.New("Actual error")
if err := job.Complete(nil, actual); err != actual {
t.Errorf("should return error: %s", actual)
}
})
}
func Test_AsyncRequest_input_arguments(t *testing.T) {
t.Run("request timeout should be less than async timeout", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("the code should panic")
}
}()
AsyncRequest(100, 50, 80)(http.HandlerFunc(blobHandler))
})
t.Run("keep result should be greater than async timeout", func(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("the code should panic")
}
}()
AsyncRequest(100, 200, 150)(http.HandlerFunc(blobHandler))
})
}
func Test_AsyncRequest(t *testing.T) {
type request struct {
title string
timeout time.Duration
timestamps bool
hasID bool
code int
data string
}
type testCase struct {
title string
headers map[string]string
handler http.Handler
requests []request
}
cases := []testCase{
{
title: "async middleware with synchronous request",
headers: make(map[string]string), // just in case to avoid panics
handler: AsyncRequest(10*time.Millisecond, 20*time.Millisecond, 30*time.Millisecond)(
handleResponse(handlerAsync),
),
requests: []request{
{
title: "should fail with timeout error if handler does not have enough time to complete the task",
code: http.StatusRequestTimeout,
data: "context deadline exceeded\n",
},
},
},
{
title: "async middleware with synchronous request",
headers: make(map[string]string), // just in case to avoid panics
handler: AsyncRequest(200*time.Millisecond, 300*time.Millisecond, 500*time.Millisecond)(
handleResponse(handlerAsync),
),
requests: []request{
{
title: "should be successful if handler has enough time to complete the task",
code: http.StatusOK,
data: "[0,1,2,3,4,5,6,7,8,9]\n",
},
},
},
{
title: "async middleware with asynchronous request",
headers: map[string]string{
asyncHeader: "",
},
handler: AsyncRequest(50*time.Millisecond, 300*time.Millisecond, 500*time.Millisecond)(
handleResponse(handlerAsync),
),
requests: []request{
{
title: "should produce response with request ID if handler did not have enough time to complete the task",
hasID: true,
timestamps: true,
code: http.StatusAccepted,
data: "request is in progress\n",
},
{
title: "should provide status of the current job if async request is still in progress",
timeout: 20 * time.Millisecond,
hasID: true,
timestamps: true,
code: http.StatusAccepted,
data: "request is in progress\n",
},
{
title: "should store the result after task is completed and be able to return it (in cooperation with handler)",
timeout: 50 * time.Millisecond,
code: http.StatusOK,
data: "[0,1,2,3,4,5,6,7,8,9]\n",
},
{
title: "should provided the result only once and delete after that",
code: http.StatusBadRequest,
data: "invalid or expired request\n",
},
},
},
{
title: "async middleware with asynchronous request",
headers: map[string]string{
asyncHeader: "",
},
handler: AsyncRequest(200*time.Millisecond, 300*time.Millisecond, 500*time.Millisecond)(
handleResponse(handlerAsync),
),
requests: []request{
{
title: "should be successful if handler has enough time to complete the task",
code: http.StatusOK,
data: "[0,1,2,3,4,5,6,7,8,9]\n",
},
},
},
{
title: "async middleware with asynchronous request",
headers: map[string]string{
asyncHeader: "",
},
handler: AsyncRequest(50*time.Millisecond, 199*time.Millisecond, 200*time.Millisecond)(
handleResponse(handlerAsync),
),
requests: []request{
{
title: "should produce response with request ID if handler did not have enough time to complete the task",
hasID: true,
code: http.StatusAccepted,
data: "request is in progress\n",
},
{
title: "should be deleted (as expired) after keep result timeout",
timeout: 300 * time.Millisecond,
code: http.StatusBadRequest,
data: "invalid or expired request\n",
},
},
},
}
for _, tc := range cases {
t.Run(tc.title, func(t *testing.T) {
for _, req := range tc.requests {
t.Run(req.title, func(t *testing.T) {
// sleep before request
time.Sleep(req.timeout)
w := httptest.NewRecorder()
r, _ := http.NewRequest("", "", nil)
// copy headers
for key, value := range tc.headers {
r.Header.Set(key, value)
}
tc.handler.ServeHTTP(w, r)
// compare status code
if w.Code != req.code {
t.Errorf("status code %d is expected to be %d", w.Code, req.code)
}
// compare response body
if w.Body.String() != req.data {
t.Errorf("the output %q is expected to be %q", w.Body.String(), req.data)
}
// set request ID (for the next async requests)
if id := w.Header().Get(asyncRequestID); id != "" {
if !req.hasID {
t.Error("the response should not contain request id")
}
tc.headers[asyncRequestID] = id
} else if req.hasID {
t.Error("the response should contain request id")
}
if req.timestamps {
started, keepUntil := w.Header().Get(asyncRequestAccepted), w.Header().Get(asyncRequestKeepUntil)
if _, err := time.Parse(DefaultTimeFormat, started); err != nil {
t.Errorf("should contain %q header and have valid format %q", asyncRequestAccepted, DefaultTimeFormat)
}
if _, err := time.Parse(DefaultTimeFormat, keepUntil); err != nil {
t.Errorf("should contain %q header and have valid format %q", asyncRequestKeepUntil, DefaultTimeFormat)
}
}
})
}
})
}
}