@@ -403,7 +403,7 @@ func (dl *downloadTester) newSlowPeer(id string, version int, hashes []common.Ha
403403 dl .lock .Lock ()
404404 defer dl .lock .Unlock ()
405405
406- var err = dl .downloader .RegisterPeer (id , version , & downloadTesterPeer {dl , id , delay })
406+ var err = dl .downloader .RegisterPeer (id , version , & downloadTesterPeer {dl : dl , id : id , delay : delay })
407407 if err == nil {
408408 // Assign the owned hashes, headers and blocks to the peer (deep copy)
409409 dl .peerHashes [id ] = make ([]common.Hash , len (hashes ))
@@ -465,6 +465,24 @@ type downloadTesterPeer struct {
465465 dl * downloadTester
466466 id string
467467 delay time.Duration
468+ lock sync.RWMutex
469+ }
470+
471+ // setDelay is a thread safe setter for the network delay value.
472+ func (dlp * downloadTesterPeer ) setDelay (delay time.Duration ) {
473+ dlp .lock .Lock ()
474+ defer dlp .lock .Unlock ()
475+
476+ dlp .delay = delay
477+ }
478+
479+ // waitDelay is a thread safe way to sleep for the configured time.
480+ func (dlp * downloadTesterPeer ) waitDelay () {
481+ dlp .lock .RLock ()
482+ delay := dlp .delay
483+ dlp .lock .RUnlock ()
484+
485+ time .Sleep (delay )
468486}
469487
470488// Head constructs a function to retrieve a peer's current head hash
@@ -499,7 +517,7 @@ func (dlp *downloadTesterPeer) RequestHeadersByHash(origin common.Hash, amount i
499517// origin; associated with a particular peer in the download tester. The returned
500518// function can be used to retrieve batches of headers from the particular peer.
501519func (dlp * downloadTesterPeer ) RequestHeadersByNumber (origin uint64 , amount int , skip int , reverse bool ) error {
502- time . Sleep ( dlp .delay )
520+ dlp .waitDelay ( )
503521
504522 dlp .dl .lock .RLock ()
505523 defer dlp .dl .lock .RUnlock ()
@@ -525,7 +543,7 @@ func (dlp *downloadTesterPeer) RequestHeadersByNumber(origin uint64, amount int,
525543// peer in the download tester. The returned function can be used to retrieve
526544// batches of block bodies from the particularly requested peer.
527545func (dlp * downloadTesterPeer ) RequestBodies (hashes []common.Hash ) error {
528- time . Sleep ( dlp .delay )
546+ dlp .waitDelay ( )
529547
530548 dlp .dl .lock .RLock ()
531549 defer dlp .dl .lock .RUnlock ()
@@ -550,7 +568,7 @@ func (dlp *downloadTesterPeer) RequestBodies(hashes []common.Hash) error {
550568// peer in the download tester. The returned function can be used to retrieve
551569// batches of block receipts from the particularly requested peer.
552570func (dlp * downloadTesterPeer ) RequestReceipts (hashes []common.Hash ) error {
553- time . Sleep ( dlp .delay )
571+ dlp .waitDelay ( )
554572
555573 dlp .dl .lock .RLock ()
556574 defer dlp .dl .lock .RUnlock ()
@@ -572,7 +590,7 @@ func (dlp *downloadTesterPeer) RequestReceipts(hashes []common.Hash) error {
572590// peer in the download tester. The returned function can be used to retrieve
573591// batches of node state data from the particularly requested peer.
574592func (dlp * downloadTesterPeer ) RequestNodeData (hashes []common.Hash ) error {
575- time . Sleep ( dlp .delay )
593+ dlp .waitDelay ( )
576594
577595 dlp .dl .lock .RLock ()
578596 defer dlp .dl .lock .RUnlock ()
@@ -1746,7 +1764,7 @@ func testFastCriticalRestarts(t *testing.T, protocol int, progress bool) {
17461764 for i := 0 ; i < fsPivotInterval ; i ++ {
17471765 tester .peerMissingStates ["peer" ][headers [hashes [fsMinFullBlocks + i ]].Root ] = true
17481766 }
1749- (tester .downloader .peers .peers ["peer" ].peer ).(* downloadTesterPeer ).delay = 500 * time .Millisecond // Enough to reach the critical section
1767+ (tester .downloader .peers .peers ["peer" ].peer ).(* downloadTesterPeer ).setDelay ( 500 * time .Millisecond ) // Enough to reach the critical section
17501768
17511769 // Synchronise with the peer a few times and make sure they fail until the retry limit
17521770 for i := 0 ; i < int (fsCriticalTrials )- 1 ; i ++ {
@@ -1765,7 +1783,7 @@ func testFastCriticalRestarts(t *testing.T, protocol int, progress bool) {
17651783 tester .lock .Lock ()
17661784 tester .peerHeaders ["peer" ][hashes [fsMinFullBlocks - 1 ]] = headers [hashes [fsMinFullBlocks - 1 ]]
17671785 tester .peerMissingStates ["peer" ] = map [common.Hash ]bool {tester .downloader .fsPivotLock .Root : true }
1768- (tester .downloader .peers .peers ["peer" ].peer ).(* downloadTesterPeer ).delay = 0
1786+ (tester .downloader .peers .peers ["peer" ].peer ).(* downloadTesterPeer ).setDelay ( 0 )
17691787 tester .lock .Unlock ()
17701788 }
17711789 }
0 commit comments