@@ -69,21 +69,21 @@ func (m *mockRequester) doRequest(ctx context.Context, request *Request) (*enode
6969func TestFetcherSingleRequest (t * testing.T ) {
7070 requester := newMockRequester ()
7171 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 )
7377
7478 peers := []string {"a" , "b" , "c" , "d" }
7579 peersToSkip := & sync.Map {}
7680 for _ , p := range peers {
7781 peersToSkip .Store (p , time .Now ())
7882 }
7983
80- ctx , cancel := context .WithCancel (context .Background ())
81- defer cancel ()
82-
83- go fetcher .run (ctx , peersToSkip )
84+ go fetcher .run (peersToSkip )
8485
85- rctx := context .Background ()
86- fetcher .Request (rctx , 0 )
86+ fetcher .Request (0 )
8787
8888 select {
8989 case request := <- requester .requestC :
@@ -115,20 +115,19 @@ func TestFetcherSingleRequest(t *testing.T) {
115115func TestFetcherCancelStopsFetcher (t * testing.T ) {
116116 requester := newMockRequester ()
117117 addr := make ([]byte , 32 )
118- fetcher := NewFetcher (addr , requester .doRequest , true )
119-
120- peersToSkip := & sync.Map {}
121118
122119 ctx , cancel := context .WithCancel (context .Background ())
123120
121+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
122+
123+ peersToSkip := & sync.Map {}
124+
124125 // we start the fetcher, and then we immediately cancel the context
125- go fetcher .run (ctx , peersToSkip )
126+ go fetcher .run (peersToSkip )
126127 cancel ()
127128
128- rctx , rcancel := context .WithTimeout (ctx , 100 * time .Millisecond )
129- defer rcancel ()
130129 // we call Request with an active context
131- fetcher .Request (rctx , 0 )
130+ fetcher .Request (0 )
132131
133132 // fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
134133 select {
@@ -140,23 +139,23 @@ func TestFetcherCancelStopsFetcher(t *testing.T) {
140139
141140// TestFetchCancelStopsRequest tests that calling a Request function with a cancelled context does not initiate a request
142141func TestFetcherCancelStopsRequest (t * testing.T ) {
142+ t .Skip ("since context is now per fetcher, this test is likely redundant" )
143+
143144 requester := newMockRequester (100 * time .Millisecond )
144145 addr := make ([]byte , 32 )
145- fetcher := NewFetcher (addr , requester .doRequest , true )
146-
147- peersToSkip := & sync.Map {}
148146
149147 ctx , cancel := context .WithCancel (context .Background ())
150148 defer cancel ()
151149
152- // we start the fetcher with an active context
153- go fetcher .run (ctx , peersToSkip )
150+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
154151
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 )
157156
158157 // we call Request with a cancelled context
159- fetcher .Request (rctx , 0 )
158+ fetcher .Request (0 )
160159
161160 // fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
162161 select {
@@ -166,8 +165,7 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
166165 }
167166
168167 // 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 )
171169
172170 select {
173171 case <- requester .requestC :
@@ -182,19 +180,19 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
182180func TestFetcherOfferUsesSource (t * testing.T ) {
183181 requester := newMockRequester (100 * time .Millisecond )
184182 addr := make ([]byte , 32 )
185- fetcher := NewFetcher (addr , requester .doRequest , true )
186-
187- peersToSkip := & sync.Map {}
188183
189184 ctx , cancel := context .WithCancel (context .Background ())
190185 defer cancel ()
191186
187+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
188+
189+ peersToSkip := & sync.Map {}
190+
192191 // start the fetcher
193- go fetcher .run (ctx , peersToSkip )
192+ go fetcher .run (peersToSkip )
194193
195- rctx := context .Background ()
196194 // call the Offer function with the source peer
197- fetcher .Offer (rctx , & sourcePeerID )
195+ fetcher .Offer (& sourcePeerID )
198196
199197 // fetcher should not initiate request
200198 select {
@@ -204,8 +202,7 @@ func TestFetcherOfferUsesSource(t *testing.T) {
204202 }
205203
206204 // call Request after the Offer
207- rctx = context .Background ()
208- fetcher .Request (rctx , 0 )
205+ fetcher .Request (0 )
209206
210207 // there should be exactly 1 request coming from fetcher
211208 var request * Request
@@ -234,19 +231,19 @@ func TestFetcherOfferUsesSource(t *testing.T) {
234231func TestFetcherOfferAfterRequestUsesSourceFromContext (t * testing.T ) {
235232 requester := newMockRequester (100 * time .Millisecond )
236233 addr := make ([]byte , 32 )
237- fetcher := NewFetcher (addr , requester .doRequest , true )
238-
239- peersToSkip := & sync.Map {}
240234
241235 ctx , cancel := context .WithCancel (context .Background ())
242236 defer cancel ()
243237
238+ fetcher := NewFetcher (ctx , addr , requester .doRequest , true )
239+
240+ peersToSkip := & sync.Map {}
241+
244242 // start the fetcher
245- go fetcher .run (ctx , peersToSkip )
243+ go fetcher .run (peersToSkip )
246244
247245 // call Request first
248- rctx := context .Background ()
249- fetcher .Request (rctx , 0 )
246+ fetcher .Request (0 )
250247
251248 // there should be a request coming from fetcher
252249 var request * Request
@@ -260,7 +257,7 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
260257 }
261258
262259 // after the Request call Offer
263- fetcher .Offer (context . Background (), & sourcePeerID )
260+ fetcher .Offer (& sourcePeerID )
264261
265262 // there should be a request coming from fetcher
266263 select {
@@ -283,21 +280,21 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
283280func TestFetcherRetryOnTimeout (t * testing.T ) {
284281 requester := newMockRequester ()
285282 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 )
287288 // set searchTimeOut to low value so the test is quicker
288289 fetcher .searchTimeout = 250 * time .Millisecond
289290
290291 peersToSkip := & sync.Map {}
291292
292- ctx , cancel := context .WithCancel (context .Background ())
293- defer cancel ()
294-
295293 // start the fetcher
296- go fetcher .run (ctx , peersToSkip )
294+ go fetcher .run (peersToSkip )
297295
298296 // call the fetch function with an active context
299- rctx := context .Background ()
300- fetcher .Request (rctx , 0 )
297+ fetcher .Request (0 )
301298
302299 // after 100ms the first request should be initiated
303300 time .Sleep (100 * time .Millisecond )
@@ -339,7 +336,7 @@ func TestFetcherFactory(t *testing.T) {
339336
340337 fetcher := fetcherFactory .New (context .Background (), addr , peersToSkip )
341338
342- fetcher .Request (context . Background (), 0 )
339+ fetcher .Request (0 )
343340
344341 // check if the created fetchFunction really starts a fetcher and initiates a request
345342 select {
@@ -353,21 +350,21 @@ func TestFetcherFactory(t *testing.T) {
353350func TestFetcherRequestQuitRetriesRequest (t * testing.T ) {
354351 requester := newMockRequester ()
355352 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 )
357358
358359 // make sure the searchTimeout is long so it is sure the request is not
359360 // retried because of timeout
360361 fetcher .searchTimeout = 10 * time .Second
361362
362363 peersToSkip := & sync.Map {}
363364
364- ctx , cancel := context .WithCancel (context .Background ())
365- defer cancel ()
366-
367- go fetcher .run (ctx , peersToSkip )
365+ go fetcher .run (peersToSkip )
368366
369- rctx := context .Background ()
370- fetcher .Request (rctx , 0 )
367+ fetcher .Request (0 )
371368
372369 select {
373370 case <- requester .requestC :
@@ -460,17 +457,15 @@ func TestRequestSkipPeerPermanent(t *testing.T) {
460457func TestFetcherMaxHopCount (t * testing.T ) {
461458 requester := newMockRequester ()
462459 addr := make ([]byte , 32 )
463- fetcher := NewFetcher (addr , requester .doRequest , true )
464460
465461 ctx , cancel := context .WithCancel (context .Background ())
466462 defer cancel ()
467463
468- peersToSkip := & sync. Map {}
464+ fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
469465
470- go fetcher . run ( ctx , peersToSkip )
466+ peersToSkip := & sync. Map {}
471467
472- rctx := context .Background ()
473- fetcher .Request (rctx , maxHopCount )
468+ go fetcher .run (peersToSkip )
474469
475470 // if hopCount is already at max no request should be initiated
476471 select {
0 commit comments