les: adapt to mclock change

This commit is contained in:
Felix Lange 2018-07-05 20:39:26 +02:00 committed by Zsolt Felfoldi
parent 0320df2ea8
commit 0740091aaa
2 changed files with 39 additions and 44 deletions

View file

@ -44,24 +44,19 @@ func TestFreeClientPoolL100C300(t *testing.T) {
const testFreeClientPoolTicks = 500000 const testFreeClientPoolTicks = 500000
func testFreeClientPool(t *testing.T, connLimit, clientCount int) { func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
quit := make(chan struct{}) var (
clock := mclock.NewSimulatedClock(time.Second, 10) quit = make(chan struct{})
defer clock.Stop() clock mclock.Simulated
var wg sync.WaitGroup wg sync.WaitGroup
db := ethdb.NewMemDatabase() db = ethdb.NewMemDatabase()
pool = newFreeClientPool(db, connLimit, 10000, quit, &wg, &clock)
pool := newFreeClientPool(db, connLimit, 10000, quit, &wg, clock) connected = make([]bool, clientCount)
connTicks = make([]int, clientCount)
connected := make([]bool, clientCount) disconnCh = make(chan int)
connTicks := make([]int, clientCount) )
disconnCh := make(chan int)
tickCh := clock.After(time.Second)
tickCounter := 0
peerId := func(i int) string { peerId := func(i int) string {
return fmt.Sprintf("test peer #%d", i) return fmt.Sprintf("test peer #%d", i)
} }
disconnFn := func(i int) func() { disconnFn := func(i int) func() {
return func() { return func() {
disconnCh <- i disconnCh <- i
@ -73,21 +68,20 @@ func testFreeClientPool(t *testing.T, connLimit, clientCount int) {
if pool.connect(peerId(i), disconnFn(i)) { if pool.connect(peerId(i), disconnFn(i)) {
connected[i] = true connected[i] = true
} else { } else {
t.Errorf("Test peer #%d rejected", i) t.Fatalf("Test peer #%d rejected", i)
} }
} }
// since all accepted peers are new and should not be kicked out, the next one should be rejected // since all accepted peers are new and should not be kicked out, the next one should be rejected
if pool.connect(peerId(connLimit), disconnFn(connLimit)) { if pool.connect(peerId(connLimit), disconnFn(connLimit)) {
connected[connLimit] = true connected[connLimit] = true
t.Errorf("Peer accepted over connected limit") t.Fatalf("Peer accepted over connected limit")
} }
// 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
loop: tickCounter := 0
for { for ; tickCounter < testFreeClientPoolTicks; tickCounter++ {
select { clock.Run(1 * time.Second)
case <-clock.PingChannel():
case <-tickCh:
i := rand.Intn(clientCount) i := rand.Intn(clientCount)
if connected[i] { if connected[i] {
pool.disconnect(peerId(i)) pool.disconnect(peerId(i))
@ -99,17 +93,18 @@ loop:
connTicks[i] -= tickCounter connTicks[i] -= tickCounter
} }
} }
tickCounter++ pollDisconnects:
if tickCounter >= testFreeClientPoolTicks { for {
break loop select {
}
tickCh = clock.After(time.Second)
case i := <-disconnCh: case i := <-disconnCh:
pool.disconnect(peerId(i)) pool.disconnect(peerId(i))
if connected[i] { if connected[i] {
connTicks[i] += tickCounter connTicks[i] += tickCounter
connected[i] = false connected[i] = false
} }
default:
break pollDisconnects
}
} }
} }
@ -137,7 +132,7 @@ loop:
wg.Wait() wg.Wait()
quit2 := make(chan struct{}) quit2 := make(chan struct{})
var wg2 sync.WaitGroup var wg2 sync.WaitGroup
pool = newFreeClientPool(db, connLimit, 10000, quit2, &wg2, clock) pool = newFreeClientPool(db, connLimit, 10000, quit2, &wg2, &clock)
// try connecting all known peers (connLimit should be filled up) // try connecting all known peers (connLimit should be filled up)
for i := 0; i < clientCount; i++ { for i := 0; i < clientCount; i++ {

View file

@ -228,7 +228,7 @@ func (pm *ProtocolManager) Start(maxPeers int) {
if pm.lightSync { if pm.lightSync {
go pm.syncer() go pm.syncer()
} else { } else {
pm.clientPool = newFreeClientPool(pm.chainDb, maxPeers, 10000, pm.quitSync, pm.wg, mclock.MonotonicClock{}) pm.clientPool = newFreeClientPool(pm.chainDb, maxPeers, 10000, pm.quitSync, pm.wg, mclock.System{})
go func() { go func() {
for range pm.newPeerCh { for range pm.newPeerCh {
} }