@@ -2,6 +2,9 @@ package apitask
2
2
3
3
import (
4
4
"context"
5
+ "errors"
6
+ "fmt"
7
+ "net/http"
5
8
"sync/atomic"
6
9
"testing"
7
10
"time"
@@ -22,31 +25,34 @@ func taskFn(typ string, fn func(context.Context) error) Task {
22
25
return & taskFunc {typ : typ , fn : fn }
23
26
}
24
27
25
- func TestRequestWorker (t * testing.T ) {
28
+ func TestContext (t * testing.T ) {
26
29
ctx , cancel := context .WithTimeout (context .Background (), time .Second * 2 )
27
30
defer cancel ()
28
31
29
- t .Run ("RunTasks" , func (t * testing.T ) {
30
- {
31
- rw , ok := from (ctx )
32
- require .False (t , ok , "request worker must not found in context" )
33
- require .Nil (t , rw , "request worker must be nil" )
34
- }
32
+ {
33
+ rw , ok := from (ctx )
34
+ require .False (t , ok , "request worker must not found in context" )
35
+ require .Nil (t , rw , "request worker must be nil" )
36
+ }
35
37
36
- withCtx := With (ctx )
37
- {
38
- rw , ok := from (withCtx )
39
- require .True (t , ok , "request worker not found in context" )
40
- require .NotNil (t , rw , "request worker was nil" )
41
-
42
- withCtxDupe := With (withCtx )
43
- sameRw , ok := from (withCtxDupe )
44
- require .True (t , ok , "request worker not found in context" )
45
- require .True (t , rw == sameRw , "request worker should be created only once" )
46
- }
47
- })
38
+ withCtx := With (ctx )
39
+ {
40
+ rw , ok := from (withCtx )
41
+ require .True (t , ok , "request worker not found in context" )
42
+ require .NotNil (t , rw , "request worker was nil" )
43
+
44
+ withCtxDupe := With (withCtx )
45
+ sameRw , ok := from (withCtxDupe )
46
+ require .True (t , ok , "request worker not found in context" )
47
+ require .True (t , rw == sameRw , "request worker should be created only once" )
48
+ }
49
+ }
48
50
49
- t .Run ("RunTasks" , func (t * testing.T ) {
51
+ func TestRun (t * testing.T ) {
52
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 2 )
53
+ defer cancel ()
54
+
55
+ t .Run ("Success" , func (t * testing.T ) {
50
56
withCtx := With (ctx )
51
57
52
58
calls := new (atomic.Int64 )
@@ -68,4 +74,121 @@ func TestRequestWorker(t *testing.T) {
68
74
require .Equal (t , expCalls , gotCalls )
69
75
}
70
76
})
77
+ t .Run ("Failure" , func (t * testing.T ) {
78
+
79
+ // exp no errors when async
80
+ t .Run ("WhenAsync" , func (t * testing.T ) {
81
+ withCtx := With (ctx )
82
+
83
+ calls := new (atomic.Int64 )
84
+ expCalls := 0
85
+ sentinel := errors .New ("sentinel" )
86
+ for range 16 {
87
+ expCalls ++
88
+ task := taskFn ("test.run" , func (ctx context.Context ) error {
89
+ calls .Add (1 )
90
+ return sentinel
91
+ })
92
+ err := Run (withCtx , task )
93
+ require .NoError (t , err )
94
+ }
95
+
96
+ {
97
+ Wait (withCtx )
98
+
99
+ gotCalls := int (calls .Load ())
100
+ require .Equal (t , expCalls , gotCalls )
101
+ }
102
+ })
103
+
104
+ // exp errors when sync
105
+ t .Run ("WhenSync" , func (t * testing.T ) {
106
+ withCtx := ctx
107
+
108
+ calls := new (atomic.Int64 )
109
+ expCalls := 0
110
+ sentinel := errors .New ("sentinel" )
111
+ for range 16 {
112
+ expCalls ++
113
+ task := taskFn ("test.run" , func (ctx context.Context ) error {
114
+ calls .Add (1 )
115
+ return sentinel
116
+ })
117
+ err := Run (withCtx , task )
118
+ require .Error (t , err )
119
+ }
120
+
121
+ {
122
+ Wait (withCtx )
123
+
124
+ gotCalls := int (calls .Load ())
125
+ require .Equal (t , expCalls , gotCalls )
126
+ }
127
+ })
128
+ })
129
+ }
130
+
131
+ func TestMiddleware (t * testing.T ) {
132
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second )
133
+ defer cancel ()
134
+
135
+ t .Run ("Success" , func (t * testing.T ) {
136
+ var errs []error
137
+ hrFunc := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
138
+ for i := range 10 {
139
+ typ := fmt .Sprintf ("test-task-%v" , i )
140
+ task := taskFn (typ , func (ctx context.Context ) error {
141
+ return nil
142
+ })
143
+
144
+ err := Run (r .Context (), task )
145
+ errs = append (errs , err )
146
+ }
147
+ })
148
+ hr := Middleware (hrFunc )
149
+
150
+ req , err := http .NewRequestWithContext (ctx , "GET" , "/" , nil )
151
+ require .NoError (t , err )
152
+
153
+ for i := range 10 {
154
+ hr .ServeHTTP (nil , req )
155
+ require .Equal (t , 10 * (i + 1 ), len (errs ))
156
+ }
157
+ for _ , e := range errs {
158
+ require .NoError (t , e )
159
+ }
160
+ })
161
+
162
+ t .Run ("Failure" , func (t * testing.T ) {
163
+ var errs []error
164
+ hrFunc := http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
165
+ for i := range 10 {
166
+ typ := fmt .Sprintf ("test-task-%v" , i )
167
+ task := taskFn (typ , func (ctx context.Context ) error {
168
+ return nil
169
+ })
170
+ err := Run (r .Context (), task )
171
+ errs = append (errs , err )
172
+ }
173
+ })
174
+ hr := Middleware (hrFunc )
175
+
176
+ ctx = With (ctx )
177
+ req , err := http .NewRequestWithContext (ctx , "GET" , "/" , nil )
178
+ require .NoError (t , err )
179
+
180
+ hr .ServeHTTP (nil , req )
181
+ require .Equal (t , 10 , len (errs ))
182
+
183
+ for _ , e := range errs {
184
+ require .NoError (t , e )
185
+ }
186
+
187
+ errs = nil
188
+ hr .ServeHTTP (nil , req )
189
+ require .Equal (t , 10 , len (errs ))
190
+ for _ , e := range errs {
191
+ require .Error (t , e )
192
+ }
193
+ })
71
194
}
0 commit comments