@@ -69,21 +69,21 @@ func (m *mockRequester) doRequest(ctx context.Context, request *Request) (*enode
69
69
func TestFetcherSingleRequest (t * testing.T ) {
70
70
requester := newMockRequester ()
71
71
addr := make ([]byte , 32 )
72
- fetcher := NewFetcher (addr , requester .doRequest , true )
72
+
73
+ ctx , cancel := context .WithCancel (context .Background ())
74
+ defer cancel ()
75
+
76
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
73
77
74
78
peers := []string {"a" , "b" , "c" , "d" }
75
79
peersToSkip := & sync.Map {}
76
80
for _ , p := range peers {
77
81
peersToSkip .Store (p , time .Now ())
78
82
}
79
83
80
- ctx , cancel := context .WithCancel (context .Background ())
81
- defer cancel ()
82
-
83
- go fetcher .run (ctx , peersToSkip )
84
+ go fetcher .run (peersToSkip )
84
85
85
- rctx := context .Background ()
86
- fetcher .Request (rctx , 0 )
86
+ fetcher .Request (0 )
87
87
88
88
select {
89
89
case request := <- requester .requestC :
@@ -115,20 +115,19 @@ func TestFetcherSingleRequest(t *testing.T) {
115
115
func TestFetcherCancelStopsFetcher (t * testing.T ) {
116
116
requester := newMockRequester ()
117
117
addr := make ([]byte , 32 )
118
- fetcher := NewFetcher (addr , requester .doRequest , true )
119
-
120
- peersToSkip := & sync.Map {}
121
118
122
119
ctx , cancel := context .WithCancel (context .Background ())
123
120
121
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
122
+
123
+ peersToSkip := & sync.Map {}
124
+
124
125
// we start the fetcher, and then we immediately cancel the context
125
- go fetcher .run (ctx , peersToSkip )
126
+ go fetcher .run (peersToSkip )
126
127
cancel ()
127
128
128
- rctx , rcancel := context .WithTimeout (ctx , 100 * time .Millisecond )
129
- defer rcancel ()
130
129
// we call Request with an active context
131
- fetcher .Request (rctx , 0 )
130
+ fetcher .Request (0 )
132
131
133
132
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
134
133
select {
@@ -140,23 +139,23 @@ func TestFetcherCancelStopsFetcher(t *testing.T) {
140
139
141
140
// TestFetchCancelStopsRequest tests that calling a Request function with a cancelled context does not initiate a request
142
141
func TestFetcherCancelStopsRequest (t * testing.T ) {
142
+ t .Skip ("since context is now per fetcher, this test is likely redundant" )
143
+
143
144
requester := newMockRequester (100 * time .Millisecond )
144
145
addr := make ([]byte , 32 )
145
- fetcher := NewFetcher (addr , requester .doRequest , true )
146
-
147
- peersToSkip := & sync.Map {}
148
146
149
147
ctx , cancel := context .WithCancel (context .Background ())
150
148
defer cancel ()
151
149
152
- // we start the fetcher with an active context
153
- go fetcher .run (ctx , peersToSkip )
150
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
154
151
155
- rctx , rcancel := context .WithCancel (context .Background ())
156
- rcancel ()
152
+ peersToSkip := & sync.Map {}
153
+
154
+ // we start the fetcher with an active context
155
+ go fetcher .run (peersToSkip )
157
156
158
157
// we call Request with a cancelled context
159
- fetcher .Request (rctx , 0 )
158
+ fetcher .Request (0 )
160
159
161
160
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
162
161
select {
@@ -166,8 +165,7 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
166
165
}
167
166
168
167
// if there is another Request with active context, there should be a request, because the fetcher itself is not cancelled
169
- rctx = context .Background ()
170
- fetcher .Request (rctx , 0 )
168
+ fetcher .Request (0 )
171
169
172
170
select {
173
171
case <- requester .requestC :
@@ -182,19 +180,19 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
182
180
func TestFetcherOfferUsesSource (t * testing.T ) {
183
181
requester := newMockRequester (100 * time .Millisecond )
184
182
addr := make ([]byte , 32 )
185
- fetcher := NewFetcher (addr , requester .doRequest , true )
186
-
187
- peersToSkip := & sync.Map {}
188
183
189
184
ctx , cancel := context .WithCancel (context .Background ())
190
185
defer cancel ()
191
186
187
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
188
+
189
+ peersToSkip := & sync.Map {}
190
+
192
191
// start the fetcher
193
- go fetcher .run (ctx , peersToSkip )
192
+ go fetcher .run (peersToSkip )
194
193
195
- rctx := context .Background ()
196
194
// call the Offer function with the source peer
197
- fetcher .Offer (rctx , & sourcePeerID )
195
+ fetcher .Offer (& sourcePeerID )
198
196
199
197
// fetcher should not initiate request
200
198
select {
@@ -204,8 +202,7 @@ func TestFetcherOfferUsesSource(t *testing.T) {
204
202
}
205
203
206
204
// call Request after the Offer
207
- rctx = context .Background ()
208
- fetcher .Request (rctx , 0 )
205
+ fetcher .Request (0 )
209
206
210
207
// there should be exactly 1 request coming from fetcher
211
208
var request * Request
@@ -234,19 +231,19 @@ func TestFetcherOfferUsesSource(t *testing.T) {
234
231
func TestFetcherOfferAfterRequestUsesSourceFromContext (t * testing.T ) {
235
232
requester := newMockRequester (100 * time .Millisecond )
236
233
addr := make ([]byte , 32 )
237
- fetcher := NewFetcher (addr , requester .doRequest , true )
238
-
239
- peersToSkip := & sync.Map {}
240
234
241
235
ctx , cancel := context .WithCancel (context .Background ())
242
236
defer cancel ()
243
237
238
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
239
+
240
+ peersToSkip := & sync.Map {}
241
+
244
242
// start the fetcher
245
- go fetcher .run (ctx , peersToSkip )
243
+ go fetcher .run (peersToSkip )
246
244
247
245
// call Request first
248
- rctx := context .Background ()
249
- fetcher .Request (rctx , 0 )
246
+ fetcher .Request (0 )
250
247
251
248
// there should be a request coming from fetcher
252
249
var request * Request
@@ -260,7 +257,7 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
260
257
}
261
258
262
259
// after the Request call Offer
263
- fetcher .Offer (context . Background (), & sourcePeerID )
260
+ fetcher .Offer (& sourcePeerID )
264
261
265
262
// there should be a request coming from fetcher
266
263
select {
@@ -283,21 +280,21 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
283
280
func TestFetcherRetryOnTimeout (t * testing.T ) {
284
281
requester := newMockRequester ()
285
282
addr := make ([]byte , 32 )
286
- fetcher := NewFetcher (addr , requester .doRequest , true )
283
+
284
+ ctx , cancel := context .WithCancel (context .Background ())
285
+ defer cancel ()
286
+
287
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
287
288
// set searchTimeOut to low value so the test is quicker
288
289
fetcher .searchTimeout = 250 * time .Millisecond
289
290
290
291
peersToSkip := & sync.Map {}
291
292
292
- ctx , cancel := context .WithCancel (context .Background ())
293
- defer cancel ()
294
-
295
293
// start the fetcher
296
- go fetcher .run (ctx , peersToSkip )
294
+ go fetcher .run (peersToSkip )
297
295
298
296
// call the fetch function with an active context
299
- rctx := context .Background ()
300
- fetcher .Request (rctx , 0 )
297
+ fetcher .Request (0 )
301
298
302
299
// after 100ms the first request should be initiated
303
300
time .Sleep (100 * time .Millisecond )
@@ -339,7 +336,7 @@ func TestFetcherFactory(t *testing.T) {
339
336
340
337
fetcher := fetcherFactory .New (context .Background (), addr , peersToSkip )
341
338
342
- fetcher .Request (context . Background (), 0 )
339
+ fetcher .Request (0 )
343
340
344
341
// check if the created fetchFunction really starts a fetcher and initiates a request
345
342
select {
@@ -353,21 +350,21 @@ func TestFetcherFactory(t *testing.T) {
353
350
func TestFetcherRequestQuitRetriesRequest (t * testing.T ) {
354
351
requester := newMockRequester ()
355
352
addr := make ([]byte , 32 )
356
- fetcher := NewFetcher (addr , requester .doRequest , true )
353
+
354
+ ctx , cancel := context .WithCancel (context .Background ())
355
+ defer cancel ()
356
+
357
+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
357
358
358
359
// make sure the searchTimeout is long so it is sure the request is not
359
360
// retried because of timeout
360
361
fetcher .searchTimeout = 10 * time .Second
361
362
362
363
peersToSkip := & sync.Map {}
363
364
364
- ctx , cancel := context .WithCancel (context .Background ())
365
- defer cancel ()
366
-
367
- go fetcher .run (ctx , peersToSkip )
365
+ go fetcher .run (peersToSkip )
368
366
369
- rctx := context .Background ()
370
- fetcher .Request (rctx , 0 )
367
+ fetcher .Request (0 )
371
368
372
369
select {
373
370
case <- requester .requestC :
@@ -460,17 +457,15 @@ func TestRequestSkipPeerPermanent(t *testing.T) {
460
457
func TestFetcherMaxHopCount (t * testing.T ) {
461
458
requester := newMockRequester ()
462
459
addr := make ([]byte , 32 )
463
- fetcher := NewFetcher (addr , requester .doRequest , true )
464
460
465
461
ctx , cancel := context .WithCancel (context .Background ())
466
462
defer cancel ()
467
463
468
- peersToSkip := & sync. Map {}
464
+ fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
469
465
470
- go fetcher . run ( ctx , peersToSkip )
466
+ peersToSkip := & sync. Map {}
471
467
472
- rctx := context .Background ()
473
- fetcher .Request (rctx , maxHopCount )
468
+ go fetcher .run (peersToSkip )
474
469
475
470
// if hopCount is already at max no request should be initiated
476
471
select {
0 commit comments