mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-25 18:51:39 +00:00
Clean up event switch add helper function
This commit is contained in:
@ -4,6 +4,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
cmn "github.com/tendermint/go-common"
|
||||
events "github.com/tendermint/go-events"
|
||||
)
|
||||
|
||||
// Waiter is informed of current height, decided whether to quit early
|
||||
@ -47,3 +49,36 @@ func WaitForHeight(c StatusClient, h int, waiter Waiter) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// WaitForOneEvent subscribes to a websocket event for the given
|
||||
// event time and returns upon receiving it one time, or
|
||||
// when the timeout duration has expired.
|
||||
//
|
||||
// This handles subscribing and unsubscribing under the hood
|
||||
func WaitForOneEvent(evsw events.EventSwitch,
|
||||
evtTyp string, timeout time.Duration) (events.EventData, error) {
|
||||
listener := cmn.RandStr(12)
|
||||
|
||||
evts, quit := make(chan events.EventData, 10), make(chan bool, 1)
|
||||
// start timeout count-down
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
quit <- true
|
||||
}()
|
||||
|
||||
// register for the next event of this type
|
||||
evsw.AddListenerForEvent(listener, evtTyp, func(data events.EventData) {
|
||||
evts <- data
|
||||
})
|
||||
// make sure to unregister after the test is over
|
||||
// TODO: don't require both!
|
||||
defer evsw.RemoveListenerForEvent(listener, evtTyp)
|
||||
defer evsw.RemoveListener(listener)
|
||||
|
||||
select {
|
||||
case <-quit:
|
||||
return nil, errors.New("timed out waiting for event")
|
||||
case evt := <-evts:
|
||||
return evt, nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user