test pex_reactor's dialPeer

This commit is contained in:
Anton Kaliaev 2018-03-09 16:23:52 +04:00
parent f0d4f56327
commit f85c8896d9
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
2 changed files with 33 additions and 3 deletions

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)

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