mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-14 13:51:21 +00:00
Client embeds EventSwitch, client.HTTP properly un/subscribes events over websocket
This commit is contained in:
@ -4,7 +4,6 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
merktest "github.com/tendermint/merkleeyes/testutil"
|
merktest "github.com/tendermint/merkleeyes/testutil"
|
||||||
"github.com/tendermint/tendermint/rpc/client"
|
"github.com/tendermint/tendermint/rpc/client"
|
||||||
@ -14,25 +13,19 @@ import (
|
|||||||
func TestHeaderEvents(t *testing.T) {
|
func TestHeaderEvents(t *testing.T) {
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
for i, c := range GetClients() {
|
for i, c := range GetClients() {
|
||||||
// test if this client implements event switch as well.
|
|
||||||
evsw, ok := c.(types.EventSwitch)
|
|
||||||
if !assert.True(t, ok, "%d: %v", i, c) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// start for this test it if it wasn't already running
|
// start for this test it if it wasn't already running
|
||||||
if !evsw.IsRunning() {
|
if !c.IsRunning() {
|
||||||
// if so, then we start it, listen, and stop it.
|
// if so, then we start it, listen, and stop it.
|
||||||
st, err := evsw.Start()
|
st, err := c.Start()
|
||||||
require.Nil(err, "%d: %+v", i, err)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
require.True(st, "%d", i)
|
require.True(st, "%d", i)
|
||||||
defer evsw.Stop()
|
defer c.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
evtTyp := types.EventStringNewBlockHeader()
|
evtTyp := types.EventStringNewBlockHeader()
|
||||||
evt, err := client.WaitForOneEvent(evsw, evtTyp, 1*time.Second)
|
evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
|
||||||
require.Nil(err, "%d: %+v", i, err)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
_, ok = evt.(types.EventDataNewBlockHeader)
|
_, ok := evt.(types.EventDataNewBlockHeader)
|
||||||
require.True(ok, "%d: %#v", i, evt)
|
require.True(ok, "%d: %#v", i, evt)
|
||||||
// TODO: more checks...
|
// TODO: more checks...
|
||||||
}
|
}
|
||||||
@ -41,19 +34,13 @@ func TestHeaderEvents(t *testing.T) {
|
|||||||
func TestTxEvents(t *testing.T) {
|
func TestTxEvents(t *testing.T) {
|
||||||
require := require.New(t)
|
require := require.New(t)
|
||||||
for i, c := range GetClients() {
|
for i, c := range GetClients() {
|
||||||
// test if this client implements event switch as well.
|
|
||||||
evsw, ok := c.(types.EventSwitch)
|
|
||||||
if !assert.True(t, ok, "%d: %v", i, c) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// start for this test it if it wasn't already running
|
// start for this test it if it wasn't already running
|
||||||
if !evsw.IsRunning() {
|
if !c.IsRunning() {
|
||||||
// if so, then we start it, listen, and stop it.
|
// if so, then we start it, listen, and stop it.
|
||||||
st, err := evsw.Start()
|
st, err := c.Start()
|
||||||
require.Nil(err, "%d: %+v", i, err)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
require.True(st, "%d", i)
|
require.True(st, "%d", i)
|
||||||
defer evsw.Stop()
|
defer c.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make the tx
|
// make the tx
|
||||||
@ -66,7 +53,7 @@ func TestTxEvents(t *testing.T) {
|
|||||||
require.True(txres.Code.IsOK())
|
require.True(txres.Code.IsOK())
|
||||||
|
|
||||||
// and wait for confirmation
|
// and wait for confirmation
|
||||||
evt, err := client.WaitForOneEvent(evsw, evtTyp, 1*time.Second)
|
evt, err := client.WaitForOneEvent(c, evtTyp, 1*time.Second)
|
||||||
require.Nil(err, "%d: %+v", i, err)
|
require.Nil(err, "%d: %+v", i, err)
|
||||||
// and make sure it has the proper info
|
// and make sure it has the proper info
|
||||||
txe, ok := evt.(types.EventDataTx)
|
txe, ok := evt.(types.EventDataTx)
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
cmn "github.com/tendermint/go-common"
|
cmn "github.com/tendermint/go-common"
|
||||||
events "github.com/tendermint/go-events"
|
events "github.com/tendermint/go-events"
|
||||||
|
"github.com/tendermint/tendermint/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Waiter is informed of current height, decided whether to quit early
|
// Waiter is informed of current height, decided whether to quit early
|
||||||
@ -55,8 +56,8 @@ func WaitForHeight(c StatusClient, h int, waiter Waiter) error {
|
|||||||
// when the timeout duration has expired.
|
// when the timeout duration has expired.
|
||||||
//
|
//
|
||||||
// This handles subscribing and unsubscribing under the hood
|
// This handles subscribing and unsubscribing under the hood
|
||||||
func WaitForOneEvent(evsw events.EventSwitch,
|
func WaitForOneEvent(evsw types.EventSwitch,
|
||||||
evtTyp string, timeout time.Duration) (events.EventData, error) {
|
evtTyp string, timeout time.Duration) (types.TMEventData, error) {
|
||||||
listener := cmn.RandStr(12)
|
listener := cmn.RandStr(12)
|
||||||
|
|
||||||
evts, quit := make(chan events.EventData, 10), make(chan bool, 1)
|
evts, quit := make(chan events.EventData, 10), make(chan bool, 1)
|
||||||
@ -71,14 +72,18 @@ func WaitForOneEvent(evsw events.EventSwitch,
|
|||||||
evts <- data
|
evts <- data
|
||||||
})
|
})
|
||||||
// make sure to unregister after the test is over
|
// make sure to unregister after the test is over
|
||||||
// TODO: don't require both!
|
// TODO: why doesn't the other call work???
|
||||||
defer evsw.RemoveListenerForEvent(listener, evtTyp)
|
// defer evsw.RemoveListenerForEvent(listener, evtTyp)
|
||||||
defer evsw.RemoveListener(listener)
|
defer evsw.RemoveListener(listener)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-quit:
|
case <-quit:
|
||||||
return nil, errors.New("timed out waiting for event")
|
return nil, errors.New("timed out waiting for event")
|
||||||
case evt := <-evts:
|
case evt := <-evts:
|
||||||
return evt, nil
|
tmevt, ok := evt.(types.TMEventData)
|
||||||
|
if ok {
|
||||||
|
return tmevt, nil
|
||||||
|
}
|
||||||
|
return nil, errors.Errorf("Got unexpected event type: %#v", evt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,8 +172,17 @@ type WSEvents struct {
|
|||||||
remote string
|
remote string
|
||||||
endpoint string
|
endpoint string
|
||||||
ws *rpcclient.WSClient
|
ws *rpcclient.WSClient
|
||||||
quit chan bool
|
|
||||||
done chan bool
|
// used for signaling the goroutine that feeds ws -> EventSwitch
|
||||||
|
quit chan bool
|
||||||
|
done chan bool
|
||||||
|
|
||||||
|
// used to maintain counts of actively listened events
|
||||||
|
// so we can properly subscribe/unsubscribe
|
||||||
|
// FIXME: thread-safety???
|
||||||
|
// FIXME: reuse code from go-events???
|
||||||
|
evtCount map[string]int // count how many time each event is subscribed
|
||||||
|
listeners map[string][]string // keep track of which events each listener is listening to
|
||||||
}
|
}
|
||||||
|
|
||||||
func newWSEvents(remote, endpoint string) *WSEvents {
|
func newWSEvents(remote, endpoint string) *WSEvents {
|
||||||
@ -183,6 +192,8 @@ func newWSEvents(remote, endpoint string) *WSEvents {
|
|||||||
remote: remote,
|
remote: remote,
|
||||||
quit: make(chan bool, 1),
|
quit: make(chan bool, 1),
|
||||||
done: make(chan bool, 1),
|
done: make(chan bool, 1),
|
||||||
|
evtCount: map[string]int{},
|
||||||
|
listeners: map[string][]string{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -222,16 +233,57 @@ func (w *WSEvents) Stop() bool {
|
|||||||
|
|
||||||
/** TODO: more intelligent subscriptions! **/
|
/** TODO: more intelligent subscriptions! **/
|
||||||
func (w *WSEvents) AddListenerForEvent(listenerID, event string, cb events.EventCallback) {
|
func (w *WSEvents) AddListenerForEvent(listenerID, event string, cb events.EventCallback) {
|
||||||
w.subscribe(event)
|
// no one listening -> subscribe
|
||||||
|
if w.evtCount[event] == 0 {
|
||||||
|
w.subscribe(event)
|
||||||
|
}
|
||||||
|
// if this listener was already listening to this event, return early
|
||||||
|
for _, s := range w.listeners[listenerID] {
|
||||||
|
if event == s {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// otherwise, add this event to this listener
|
||||||
|
w.evtCount[event] += 1
|
||||||
|
w.listeners[listenerID] = append(w.listeners[listenerID], event)
|
||||||
w.EventSwitch.AddListenerForEvent(listenerID, event, cb)
|
w.EventSwitch.AddListenerForEvent(listenerID, event, cb)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSEvents) RemoveListenerForEvent(event string, listenerID string) {
|
func (w *WSEvents) RemoveListenerForEvent(event string, listenerID string) {
|
||||||
w.unsubscribe(event)
|
// if this listener is listening already, splice it out
|
||||||
|
found := false
|
||||||
|
l := w.listeners[listenerID]
|
||||||
|
for i, s := range l {
|
||||||
|
if event == s {
|
||||||
|
found = true
|
||||||
|
w.listeners[listenerID] = append(l[:i], l[i+1:]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if the listener wasn't already listening to the event, exit early
|
||||||
|
if !found {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// now we can update the subscriptions
|
||||||
|
w.evtCount[event] -= 1
|
||||||
|
if w.evtCount[event] == 0 {
|
||||||
|
w.unsubscribe(event)
|
||||||
|
}
|
||||||
w.EventSwitch.RemoveListenerForEvent(event, listenerID)
|
w.EventSwitch.RemoveListenerForEvent(event, listenerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSEvents) RemoveListener(listenerID string) {
|
func (w *WSEvents) RemoveListener(listenerID string) {
|
||||||
|
// remove all counts for this listener
|
||||||
|
for _, s := range w.listeners[listenerID] {
|
||||||
|
w.evtCount[s] -= 1
|
||||||
|
if w.evtCount[s] == 0 {
|
||||||
|
w.unsubscribe(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
w.listeners[listenerID] = nil
|
||||||
|
|
||||||
|
// then let the switch do it's magic
|
||||||
w.EventSwitch.RemoveListener(listenerID)
|
w.EventSwitch.RemoveListener(listenerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -274,18 +326,24 @@ func (w *WSEvents) parseEvent(data []byte) (err error) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
// ignore silently (eg. subscribe, unsubscribe and maybe other events)
|
// ignore silently (eg. subscribe, unsubscribe and maybe other events)
|
||||||
return nil
|
return nil
|
||||||
// or report loudly???
|
|
||||||
// return errors.Errorf("unknown message: %#v", *result)
|
|
||||||
}
|
}
|
||||||
// looks good! let's fire this baby!
|
// looks good! let's fire this baby!
|
||||||
w.EventSwitch.FireEvent(event.Name, event.Data)
|
w.EventSwitch.FireEvent(event.Name, event.Data)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSEvents) subscribe(event string) error {
|
// no way of exposing these failures, so we panic.
|
||||||
return errors.Wrap(w.ws.Subscribe(event), "Subscribe")
|
// is this right? or silently ignore???
|
||||||
|
func (w *WSEvents) subscribe(event string) {
|
||||||
|
err := w.ws.Subscribe(event)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WSEvents) unsubscribe(event string) error {
|
func (w *WSEvents) unsubscribe(event string) {
|
||||||
return errors.Wrap(w.ws.Unsubscribe(event), "Unsubscribe")
|
err := w.ws.Unsubscribe(event)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,10 @@ type Client interface {
|
|||||||
SignClient
|
SignClient
|
||||||
HistoryClient
|
HistoryClient
|
||||||
StatusClient
|
StatusClient
|
||||||
|
|
||||||
|
// this Client is reactive, you can subscribe to any TMEventData
|
||||||
|
// type, given the proper string. see tendermint/types/events.go
|
||||||
|
types.EventSwitch
|
||||||
}
|
}
|
||||||
|
|
||||||
// NetworkClient is general info about the network state. May not
|
// NetworkClient is general info about the network state. May not
|
||||||
|
@ -32,6 +32,8 @@ type Client struct {
|
|||||||
client.SignClient
|
client.SignClient
|
||||||
client.HistoryClient
|
client.HistoryClient
|
||||||
client.StatusClient
|
client.StatusClient
|
||||||
|
// create a mock with types.NewEventSwitch()
|
||||||
|
types.EventSwitch
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c Client) _assertIsClient() client.Client {
|
func (c Client) _assertIsClient() client.Client {
|
||||||
|
Reference in New Issue
Block a user