mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
Reworked WaitForHeight to avoid race conditions
This commit is contained in:
parent
f7f7cf576a
commit
d92a5b1074
@ -6,26 +6,44 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Waiter is informed of current height, decided whether to quit early
|
||||||
|
type Waiter func(delta int) (abort error)
|
||||||
|
|
||||||
|
// DefaultWaitStrategy is the standard backoff algorithm,
|
||||||
|
// but you can plug in another one
|
||||||
|
func DefaultWaitStrategy(delta int) (abort error) {
|
||||||
|
if delta > 10 {
|
||||||
|
return errors.Errorf("Waiting for %d blocks... aborting", delta)
|
||||||
|
} else if delta > 0 {
|
||||||
|
// estimate of wait time....
|
||||||
|
// wait half a second for the next block (in progress)
|
||||||
|
// plus one second for every full block
|
||||||
|
delay := time.Duration(delta-1)*time.Second + 500*time.Millisecond
|
||||||
|
time.Sleep(delay)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// Wait for height will poll status at reasonable intervals until
|
// Wait for height will poll status at reasonable intervals until
|
||||||
// the block at the given height is available.
|
// the block at the given height is available.
|
||||||
func WaitForHeight(c StatusClient, h int) error {
|
//
|
||||||
wait := 1
|
// If waiter is nil, we use DefaultWaitStrategy, but you can also
|
||||||
for wait > 0 {
|
// provide your own implementation
|
||||||
|
func WaitForHeight(c StatusClient, h int, waiter Waiter) error {
|
||||||
|
if waiter == nil {
|
||||||
|
waiter = DefaultWaitStrategy
|
||||||
|
}
|
||||||
|
delta := 1
|
||||||
|
for delta > 0 {
|
||||||
s, err := c.Status()
|
s, err := c.Status()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
wait = h - s.LatestBlockHeight
|
delta = h - s.LatestBlockHeight
|
||||||
if wait > 10 {
|
// wait for the time, or abort early
|
||||||
return errors.Errorf("Waiting for %d block... aborting", wait)
|
if err := waiter(delta); err != nil {
|
||||||
} else if wait > 0 {
|
return err
|
||||||
// estimate of wait time....
|
|
||||||
// wait half a second for the next block (in progress)
|
|
||||||
// plus one second for every full block
|
|
||||||
delay := time.Duration(wait-1)*time.Second + 500*time.Millisecond
|
|
||||||
time.Sleep(delay)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// guess we waited long enough
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,6 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@ -25,7 +24,7 @@ func TestWaitForHeight(t *testing.T) {
|
|||||||
r := mock.NewStatusRecorder(m)
|
r := mock.NewStatusRecorder(m)
|
||||||
|
|
||||||
// connection failure always leads to error
|
// connection failure always leads to error
|
||||||
err := client.WaitForHeight(r, 8)
|
err := client.WaitForHeight(r, 8, nil)
|
||||||
require.NotNil(err)
|
require.NotNil(err)
|
||||||
require.Equal("bye", err.Error())
|
require.Equal("bye", err.Error())
|
||||||
// we called status once to check
|
// we called status once to check
|
||||||
@ -37,26 +36,28 @@ func TestWaitForHeight(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we will not wait for more than 10 blocks
|
// we will not wait for more than 10 blocks
|
||||||
err = client.WaitForHeight(r, 40)
|
err = client.WaitForHeight(r, 40, nil)
|
||||||
require.NotNil(err)
|
require.NotNil(err)
|
||||||
require.True(strings.Contains(err.Error(), "aborting"))
|
require.True(strings.Contains(err.Error(), "aborting"))
|
||||||
// we called status once more to check
|
// we called status once more to check
|
||||||
require.Equal(2, len(r.Calls))
|
require.Equal(2, len(r.Calls))
|
||||||
|
|
||||||
// waiting for the past returns immediately
|
// waiting for the past returns immediately
|
||||||
err = client.WaitForHeight(r, 5)
|
err = client.WaitForHeight(r, 5, nil)
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
// we called status once more to check
|
// we called status once more to check
|
||||||
require.Equal(3, len(r.Calls))
|
require.Equal(3, len(r.Calls))
|
||||||
|
|
||||||
// next tick in background while we wait
|
// since we can't update in a background goroutine (test --race)
|
||||||
go func() {
|
// we use the callback to update the status height
|
||||||
time.Sleep(500 * time.Millisecond)
|
myWaiter := func(delta int) error {
|
||||||
|
// update the height for the next call
|
||||||
m.Call.Response = &ctypes.ResultStatus{LatestBlockHeight: 15}
|
m.Call.Response = &ctypes.ResultStatus{LatestBlockHeight: 15}
|
||||||
}()
|
return client.DefaultWaitStrategy(delta)
|
||||||
|
}
|
||||||
|
|
||||||
// we wait for a few blocks
|
// we wait for a few blocks
|
||||||
err = client.WaitForHeight(r, 12)
|
err = client.WaitForHeight(r, 12, myWaiter)
|
||||||
require.Nil(err)
|
require.Nil(err)
|
||||||
// we called status once to check
|
// we called status once to check
|
||||||
require.Equal(5, len(r.Calls))
|
require.Equal(5, len(r.Calls))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user