-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.go
62 lines (56 loc) · 1.78 KB
/
test_helper.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
package service
import (
"context"
"errors"
"time"
)
// Server is an interface to use in your code in order to be able to switch implementations
// between application and testing code
type Server interface {
Serve(ctx context.Context, req Request) (Response, error)
}
// TestService is an implementation of the Server interface for testing purposes
type TestService struct {
// The response that should be returned
Res Response
// DelayReponse is the time to delay the response of the test service.
// Should be used when testing with cancellable context
DelayReponse time.Duration
// Err is the error that should be returned
Err error
// Recorder stores informations about the Serve execution
Recorder struct {
// Request is the actual request that was served
Request Request
// CtxCancelled is a flag showing if the context was cancelled or not
CtxCancelled bool
// CtxCancelled is a flag showing if the context exceeded a deadline
CtxDeadlineExceeded bool
// CtxErr is the error returned in case of context cancellation.
CtxErr error
}
}
// Serve serves and records the request and context cancellation and error, and replys back with
// a predefined response or error
func (t *TestService) Serve(ctx context.Context, req Request) (Response, error) {
// record the request param
t.Recorder.Request = req
// create a channel to signal that the actual work was finished
done := make(chan bool, 1)
go func() {
time.Sleep(t.DelayReponse)
done <- true
}()
select {
case <-ctx.Done():
t.Recorder.CtxErr = ctx.Err()
if errors.Is(ctx.Err(), context.Canceled) {
t.Recorder.CtxCancelled = true
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
t.Recorder.CtxDeadlineExceeded = true
}
return Response{}, ctx.Err()
case <-done:
return t.Res, t.Err
}
}