container, k8s: ticker refactoring and bug fixes

This commit is contained in:
Alan Chen 2017-09-12 14:12:26 +08:00
parent 653ee76f85
commit 7f1b6f0c89
2 changed files with 21 additions and 17 deletions

View File

@ -404,6 +404,7 @@ func (eth *ethereum) ConsensusMonitor(errCh chan<- error, quit chan struct{}) {
defer sub.Unsubscribe()
timer := time.NewTimer(10 * time.Second)
defer timer.Stop()
blockNumber := uint64(0)
for {
select {
@ -446,6 +447,7 @@ func (eth *ethereum) WaitForProposed(expectedAddress common.Address, timeout tim
defer sub.Unsubscribe()
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case err := <-sub.Err():
@ -468,6 +470,7 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
defer client.Close()
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for _ = range ticker.C {
infos, err := client.AdminPeers(context.Background())
if err != nil {
@ -476,7 +479,6 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
if len(infos) < expectedPeercount {
continue
} else {
ticker.Stop()
break
}
}
@ -502,6 +504,7 @@ func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error
timeout := time.After(t)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
@ -518,7 +521,6 @@ func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error
}
// Check if new blocks are getting generated
if new(big.Int).Sub(n, first).Int64() >= int64(num) {
ticker.Stop()
return nil
}
}
@ -533,13 +535,13 @@ func (eth *ethereum) WaitForBlockHeight(num int) error {
defer client.Close()
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for _ = range ticker.C {
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
}
if n.Int64() >= int64(num) {
ticker.Stop()
break
}
}
@ -556,12 +558,13 @@ func (eth *ethereum) WaitForNoBlocks(num int, duration time.Duration) error {
}
timeout := time.After(duration)
tick := time.Tick(time.Millisecond * 500)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
return nil
case <-tick:
case <-ticker.C:
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
@ -593,13 +596,13 @@ func (eth *ethereum) WaitForBalances(addrs []common.Address, duration ...time.Du
waitBalance := func(addr common.Address) error {
timeout := time.After(t)
tick := time.Tick(time.Millisecond * 500)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
return ErrTimeout
case <-tick:
case <-ticker.C:
n, err := client.BalanceAt(context.Background(), addr, nil)
if err != nil {
return err

View File

@ -126,6 +126,7 @@ func (eth *ethereum) WaitForProposed(expectedAddress common.Address, timeout tim
defer sub.Unsubscribe()
timer := time.NewTimer(timeout)
defer timer.Stop()
for {
select {
case err := <-sub.Err():
@ -148,6 +149,7 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
defer client.Close()
ticker := time.NewTicker(time.Second * 1)
defer ticker.Stop()
for _ = range ticker.C {
infos, err := client.AdminPeers(context.Background())
if err != nil {
@ -156,7 +158,6 @@ func (eth *ethereum) WaitForPeersConnected(expectedPeercount int) error {
if len(infos) < expectedPeercount {
continue
} else {
ticker.Stop()
break
}
}
@ -182,10 +183,10 @@ func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error
timeout := time.After(t)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
ticker.Stop()
return ErrNoBlock
case <-ticker.C:
n, err := client.BlockNumber(context.Background())
@ -198,7 +199,6 @@ func (eth *ethereum) WaitForBlocks(num int, waitingTime ...time.Duration) error
}
// Check if new blocks are getting generated
if new(big.Int).Sub(n, first).Int64() >= int64(num) {
ticker.Stop()
return nil
}
}
@ -213,13 +213,13 @@ func (eth *ethereum) WaitForBlockHeight(num int) error {
defer client.Close()
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for _ = range ticker.C {
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
}
if n.Int64() >= int64(num) {
ticker.Stop()
break
}
}
@ -236,12 +236,13 @@ func (eth *ethereum) WaitForNoBlocks(num int, duration time.Duration) error {
}
timeout := time.After(duration)
tick := time.Tick(time.Millisecond * 500)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
return nil
case <-tick:
case <-ticker.C:
n, err := client.BlockNumber(context.Background())
if err != nil {
return err
@ -273,13 +274,13 @@ func (eth *ethereum) WaitForBalances(addrs []common.Address, duration ...time.Du
waitBalance := func(addr common.Address) error {
timeout := time.After(t)
tick := time.Tick(time.Millisecond * 500)
ticker := time.NewTicker(time.Millisecond * 500)
defer ticker.Stop()
for {
select {
case <-timeout:
return container.ErrTimeout
case <-tick:
case <-ticker.C:
n, err := client.BalanceAt(context.Background(), addr, nil)
if err != nil {
return err