@ -69,7 +69,11 @@ func (m *mockRequester) doRequest(ctx context.Context, request *Request) (*enode
func TestFetcherSingleRequest ( t * testing . T ) {
func TestFetcherSingleRequest ( t * testing . T ) {
requester := newMockRequester ( )
requester := newMockRequester ( )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
peers := [ ] string { "a" , "b" , "c" , "d" }
peers := [ ] string { "a" , "b" , "c" , "d" }
peersToSkip := & sync . Map { }
peersToSkip := & sync . Map { }
@ -77,13 +81,9 @@ func TestFetcherSingleRequest(t *testing.T) {
peersToSkip . Store ( p , time . Now ( ) )
peersToSkip . Store ( p , time . Now ( ) )
}
}
ctx , cancel := context . WithCancel ( context . Background ( ) )
go fetcher . run ( peersToSkip )
defer cancel ( )
go fetcher . run ( ctx , peersToSkip )
rctx := context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
select {
select {
case request := <- requester . requestC :
case request := <- requester . requestC :
@ -115,20 +115,19 @@ func TestFetcherSingleRequest(t *testing.T) {
func TestFetcherCancelStopsFetcher ( t * testing . T ) {
func TestFetcherCancelStopsFetcher ( t * testing . T ) {
requester := newMockRequester ( )
requester := newMockRequester ( )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
ctx , cancel := context . WithCancel ( context . Background ( ) )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
// we start the fetcher, and then we immediately cancel the context
// we start the fetcher, and then we immediately cancel the context
go fetcher . run ( ctx , peersToSkip )
go fetcher . run ( peersToSkip )
cancel ( )
cancel ( )
rctx , rcancel := context . WithTimeout ( ctx , 100 * time . Millisecond )
defer rcancel ( )
// we call Request with an active context
// we call Request with an active context
fetcher . Request ( rctx , 0 )
fetcher . Request ( 0 )
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
select {
select {
@ -140,23 +139,23 @@ func TestFetcherCancelStopsFetcher(t *testing.T) {
// TestFetchCancelStopsRequest tests that calling a Request function with a cancelled context does not initiate a request
// TestFetchCancelStopsRequest tests that calling a Request function with a cancelled context does not initiate a request
func TestFetcherCancelStopsRequest ( t * testing . T ) {
func TestFetcherCancelStopsRequest ( t * testing . T ) {
t . Skip ( "since context is now per fetcher, this test is likely redundant" )
requester := newMockRequester ( 100 * time . Millisecond )
requester := newMockRequester ( 100 * time . Millisecond )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
defer cancel ( )
// we start the fetcher with an active context
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
go fetcher . run ( ctx , peersToSkip )
rctx , rcancel := context . WithCancel ( context . Background ( ) )
peersToSkip := & sync . Map { }
rcancel ( )
// we start the fetcher with an active context
go fetcher . run ( peersToSkip )
// we call Request with a cancelled context
// we call Request with a cancelled context
fetcher . Request ( rctx , 0 )
fetcher . Request ( 0 )
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
// fetcher should not initiate request, we can only check by waiting a bit and making sure no request is happening
select {
select {
@ -166,8 +165,7 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
}
}
// if there is another Request with active context, there should be a request, because the fetcher itself is not cancelled
// if there is another Request with active context, there should be a request, because the fetcher itself is not cancelled
rctx = context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
select {
select {
case <- requester . requestC :
case <- requester . requestC :
@ -182,19 +180,19 @@ func TestFetcherCancelStopsRequest(t *testing.T) {
func TestFetcherOfferUsesSource ( t * testing . T ) {
func TestFetcherOfferUsesSource ( t * testing . T ) {
requester := newMockRequester ( 100 * time . Millisecond )
requester := newMockRequester ( 100 * time . Millisecond )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
defer cancel ( )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
// start the fetcher
// start the fetcher
go fetcher . run ( ctx , peersToSkip )
go fetcher . run ( peersToSkip )
rctx := context . Background ( )
// call the Offer function with the source peer
// call the Offer function with the source peer
fetcher . Offer ( rctx , & sourcePeerID )
fetcher . Offer ( & sourcePeerID )
// fetcher should not initiate request
// fetcher should not initiate request
select {
select {
@ -204,8 +202,7 @@ func TestFetcherOfferUsesSource(t *testing.T) {
}
}
// call Request after the Offer
// call Request after the Offer
rctx = context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
// there should be exactly 1 request coming from fetcher
// there should be exactly 1 request coming from fetcher
var request * Request
var request * Request
@ -234,19 +231,19 @@ func TestFetcherOfferUsesSource(t *testing.T) {
func TestFetcherOfferAfterRequestUsesSourceFromContext ( t * testing . T ) {
func TestFetcherOfferAfterRequestUsesSourceFromContext ( t * testing . T ) {
requester := newMockRequester ( 100 * time . Millisecond )
requester := newMockRequester ( 100 * time . Millisecond )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
defer cancel ( )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
peersToSkip := & sync . Map { }
// start the fetcher
// start the fetcher
go fetcher . run ( ctx , peersToSkip )
go fetcher . run ( peersToSkip )
// call Request first
// call Request first
rctx := context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
// there should be a request coming from fetcher
// there should be a request coming from fetcher
var request * Request
var request * Request
@ -260,7 +257,7 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
}
}
// after the Request call Offer
// after the Request call Offer
fetcher . Offer ( context . Background ( ) , & sourcePeerID )
fetcher . Offer ( & sourcePeerID )
// there should be a request coming from fetcher
// there should be a request coming from fetcher
select {
select {
@ -283,21 +280,21 @@ func TestFetcherOfferAfterRequestUsesSourceFromContext(t *testing.T) {
func TestFetcherRetryOnTimeout ( t * testing . T ) {
func TestFetcherRetryOnTimeout ( t * testing . T ) {
requester := newMockRequester ( )
requester := newMockRequester ( )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
// set searchTimeOut to low value so the test is quicker
// set searchTimeOut to low value so the test is quicker
fetcher . searchTimeout = 250 * time . Millisecond
fetcher . searchTimeout = 250 * time . Millisecond
peersToSkip := & sync . Map { }
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
// start the fetcher
// start the fetcher
go fetcher . run ( ctx , peersToSkip )
go fetcher . run ( peersToSkip )
// call the fetch function with an active context
// call the fetch function with an active context
rctx := context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
// after 100ms the first request should be initiated
// after 100ms the first request should be initiated
time . Sleep ( 100 * time . Millisecond )
time . Sleep ( 100 * time . Millisecond )
@ -339,7 +336,7 @@ func TestFetcherFactory(t *testing.T) {
fetcher := fetcherFactory . New ( context . Background ( ) , addr , peersToSkip )
fetcher := fetcherFactory . New ( context . Background ( ) , addr , peersToSkip )
fetcher . Request ( context . Background ( ) , 0 )
fetcher . Request ( 0 )
// check if the created fetchFunction really starts a fetcher and initiates a request
// check if the created fetchFunction really starts a fetcher and initiates a request
select {
select {
@ -353,7 +350,11 @@ func TestFetcherFactory(t *testing.T) {
func TestFetcherRequestQuitRetriesRequest ( t * testing . T ) {
func TestFetcherRequestQuitRetriesRequest ( t * testing . T ) {
requester := newMockRequester ( )
requester := newMockRequester ( )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
// make sure the searchTimeout is long so it is sure the request is not
// make sure the searchTimeout is long so it is sure the request is not
// retried because of timeout
// retried because of timeout
@ -361,13 +362,9 @@ func TestFetcherRequestQuitRetriesRequest(t *testing.T) {
peersToSkip := & sync . Map { }
peersToSkip := & sync . Map { }
ctx , cancel := context . WithCancel ( context . Background ( ) )
go fetcher . run ( peersToSkip )
defer cancel ( )
go fetcher . run ( ctx , peersToSkip )
rctx := context . Background ( )
fetcher . Request ( 0 )
fetcher . Request ( rctx , 0 )
select {
select {
case <- requester . requestC :
case <- requester . requestC :
@ -460,17 +457,15 @@ func TestRequestSkipPeerPermanent(t *testing.T) {
func TestFetcherMaxHopCount ( t * testing . T ) {
func TestFetcherMaxHopCount ( t * testing . T ) {
requester := newMockRequester ( )
requester := newMockRequester ( )
addr := make ( [ ] byte , 32 )
addr := make ( [ ] byte , 32 )
fetcher := NewFetcher ( addr , requester . doRequest , true )
ctx , cancel := context . WithCancel ( context . Background ( ) )
ctx , cancel := context . WithCancel ( context . Background ( ) )
defer cancel ( )
defer cancel ( )
peersToSkip := & sync . Map { }
fetcher := NewFetcher ( ctx , addr , requester . doRequest , true )
go fetcher . run ( ctx , peersToSkip )
peersToSkip := & sync . Map { }
rctx := context . Background ( )
go fetcher . run ( peersToSkip )
fetcher . Request ( rctx , maxHopCount )
// if hopCount is already at max no request should be initiated
// if hopCount is already at max no request should be initiated
select {
select {