From f85c8896d9877f034e6c40d11887637150940841 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Fri, 9 Mar 2018 16:23:52 +0400 Subject: [PATCH] test pex_reactor's dialPeer --- p2p/pex/pex_reactor.go | 11 +++++++++++ p2p/pex/pex_reactor_test.go | 25 ++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/p2p/pex/pex_reactor.go b/p2p/pex/pex_reactor.go index 4d85259fc..73df67dca 100644 --- a/p2p/pex/pex_reactor.go +++ b/p2p/pex/pex_reactor.go @@ -432,6 +432,17 @@ func (r *PEXReactor) dialSeeds() { r.Switch.Logger.Error("Couldn't connect to any seeds") } +// AttemptsToDial returns the number of attempts to dial specific address. It +// returns 0 if never attempted or successfully connected. +func (r *PEXReactor) AttemptsToDial(addr *p2p.NetAddress) int { + attempts, attempted := r.attemptsToDial.Load(addr.DialString()) + if attempted { + return attempts.(int) + } else { + return 0 + } +} + //---------------------------------------------------------- // Explores the network searching for more peers. (continuous) diff --git a/p2p/pex/pex_reactor_test.go b/p2p/pex/pex_reactor_test.go index 6b610f009..41431d34c 100644 --- a/p2p/pex/pex_reactor_test.go +++ b/p2p/pex/pex_reactor_test.go @@ -13,12 +13,11 @@ import ( crypto "github.com/tendermint/go-crypto" wire "github.com/tendermint/go-wire" - cmn "github.com/tendermint/tmlibs/common" - "github.com/tendermint/tmlibs/log" - cfg "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p/conn" + cmn "github.com/tendermint/tmlibs/common" + "github.com/tendermint/tmlibs/log" ) var ( @@ -269,6 +268,26 @@ func TestPEXReactorCrawlStatus(t *testing.T) { // TODO: test } +func TestPEXReactorDialPeer(t *testing.T) { + pexR, book := createReactor(&PEXReactorConfig{}) + defer teardownReactor(book) + + _ = createSwitchAndAddReactors(pexR) + + peer := newMockPeer() + addr := peer.NodeInfo().NetAddress() + + assert.Equal(t, 0, pexR.AttemptsToDial(addr)) + + // 1st unsuccessful attempt + pexR.dialPeer(addr) + + // 2nd unsuccessful attempt + pexR.dialPeer(addr) + + assert.Equal(t, 2, pexR.AttemptsToDial(addr)) +} + type mockPeer struct { *cmn.BaseService pubKey crypto.PubKey