les: fix test by calling the disconnectFn callback directly

This commit is contained in:
Zsolt Felfoldi 2018-08-08 21:02:23 +02:00
parent 0740091aaa
commit 4a6959a839
3 changed files with 8 additions and 7 deletions

View file

@ -90,6 +90,8 @@ func newFreeClientPool(db ethdb.Database, connectedLimit, totalLimit int, quit c
// connect should be called after a successful handshake. If the connection was // connect should be called after a successful handshake. If the connection was
// rejected, there is no need to call disconnect. // rejected, there is no need to call disconnect.
//
// Note: the disconnectFn callback should not block.
func (f *freeClientPool) connect(address string, disconnectFn func()) bool { func (f *freeClientPool) connect(address string, disconnectFn func()) bool {
f.lock.Lock() f.lock.Lock()
defer f.lock.Unlock() defer f.lock.Unlock()
@ -121,7 +123,7 @@ func (f *freeClientPool) connect(address string, disconnectFn func()) bool {
i.connected = false i.connected = false
f.disconnPool.Push(i, -i.logUsage) f.disconnPool.Push(i, -i.logUsage)
log.Debug("Client kicked out", "address", i.address) log.Debug("Client kicked out", "address", i.address)
go i.disconnectFn() i.disconnectFn()
} else { } else {
// keep the old client and reject the new one // keep the old client and reject the new one
f.connPool.Push(i, i.linUsage) f.connPool.Push(i, i.linUsage)

View file

@ -52,7 +52,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
pool = newFreeClientPool(db, connLimit, 10000, quit, &wg, &clock) pool = newFreeClientPool(db, connLimit, 10000, quit, &wg, &clock)
connected = make([]bool, clientCount) connected = make([]bool, clientCount)
connTicks = make([]int, clientCount) connTicks = make([]int, clientCount)
disconnCh = make(chan int) disconnCh = make(chan int, clientCount)
) )
peerId := func(i int) string { peerId := func(i int) string {
return fmt.Sprintf("test peer #%d", i) return fmt.Sprintf("test peer #%d", i)
@ -78,8 +78,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
} }
// randomly connect and disconnect peers, expect to have a similar total connection time at the end // randomly connect and disconnect peers, expect to have a similar total connection time at the end
tickCounter := 0 for tickCounter := 0; tickCounter < testFreeClientPoolTicks; tickCounter++ {
for ; tickCounter < testFreeClientPoolTicks; tickCounter++ {
clock.Run(1 * time.Second) clock.Run(1 * time.Second)
i := rand.Intn(clientCount) i := rand.Intn(clientCount)
@ -115,7 +114,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
// check if the total connected time of peers are all in the expected range // check if the total connected time of peers are all in the expected range
for i, c := range connected { for i, c := range connected {
if c { if c {
connTicks[i] += tickCounter connTicks[i] += testFreeClientPoolTicks
} }
if connTicks[i] < expMin || connTicks[i] > expMax { if connTicks[i] < expMin || connTicks[i] > expMax {
t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax) t.Errorf("Total connected time of test node #%d (%d) outside expected range (%d to %d)", i, connTicks[i], expMin, expMax)
@ -124,7 +123,7 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
// a previously unknown peer should be accepted now // a previously unknown peer should be accepted now
if !pool.connect("newPeer", func() {}) { if !pool.connect("newPeer", func() {}) {
t.Errorf("Previously unknown peer rejected") t.Fatalf("Previously unknown peer rejected")
} }
// close and restart pool // close and restart pool

View file

@ -292,7 +292,7 @@ func (pm *ProtocolManager) handle(p *peer) error {
// test peer address is not a tcp address, don't use client pool if can not typecast // test peer address is not a tcp address, don't use client pool if can not typecast
if ok { if ok {
id := addr.IP.String() id := addr.IP.String()
if !pm.clientPool.connect(id, func() { pm.removePeer(p.id) }) { if !pm.clientPool.connect(id, func() { go pm.removePeer(p.id) }) {
return p2p.DiscTooManyPeers return p2p.DiscTooManyPeers
} }
defer pm.clientPool.disconnect(id) defer pm.clientPool.disconnect(id)