Browse Source

test pex_reactor's dialPeer

pull/1292/head
Anton Kaliaev 7 years ago
parent
commit
f85c8896d9
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 33 additions and 3 deletions
  1. +11
    -0
      p2p/pex/pex_reactor.go
  2. +22
    -3
      p2p/pex/pex_reactor_test.go

+ 11
- 0
p2p/pex/pex_reactor.go View File

@ -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)


+ 22
- 3
p2p/pex/pex_reactor_test.go View File

@ -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


Loading…
Cancel
Save