subscribe to new blocks and make sure height increments by 1
(t *testing.T)
| 54 | |
| 55 | // subscribe to new blocks and make sure height increments by 1 |
| 56 | func TestBlockEvents(t *testing.T) { |
| 57 | for _, c := range GetClients() { |
| 58 | c := c |
| 59 | t.Run(reflect.TypeOf(c).String(), func(t *testing.T) { |
| 60 | |
| 61 | // start for this test it if it wasn't already running |
| 62 | if !c.IsRunning() { |
| 63 | // if so, then we start it, listen, and stop it. |
| 64 | err := c.Start() |
| 65 | require.Nil(t, err) |
| 66 | t.Cleanup(func() { |
| 67 | if err := c.Stop(); err != nil { |
| 68 | t.Error(err) |
| 69 | } |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | const subscriber = "TestBlockEvents" |
| 74 | |
| 75 | eventCh, err := c.Subscribe(context.Background(), subscriber, types.QueryForEvent(types.EventNewBlock).String()) |
| 76 | require.NoError(t, err) |
| 77 | t.Cleanup(func() { |
| 78 | if err := c.UnsubscribeAll(context.Background(), subscriber); err != nil { |
| 79 | t.Error(err) |
| 80 | } |
| 81 | }) |
| 82 | |
| 83 | var firstBlockHeight int64 |
| 84 | for i := int64(0); i < 3; i++ { |
| 85 | event := <-eventCh |
| 86 | blockEvent, ok := event.Data.(types.EventDataNewBlock) |
| 87 | require.True(t, ok) |
| 88 | |
| 89 | block := blockEvent.Block |
| 90 | |
| 91 | if firstBlockHeight == 0 { |
| 92 | firstBlockHeight = block.Header.Height |
| 93 | } |
| 94 | |
| 95 | require.Equal(t, firstBlockHeight+i, block.Header.Height) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { testTxEventsSent(t, "async") } |
| 102 | func TestTxEventsSentWithBroadcastTxSync(t *testing.T) { testTxEventsSent(t, "sync") } |
nothing calls this directly
no test coverage detected