github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/rpc/client/event_test.go (about)

     1  package client_test
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	abci "github.com/tendermint/tendermint/abci/types"
    14  	tmrand "github.com/tendermint/tendermint/libs/rand"
    15  	"github.com/tendermint/tendermint/rpc/client"
    16  	ctypes "github.com/tendermint/tendermint/rpc/core/types"
    17  	"github.com/tendermint/tendermint/types"
    18  )
    19  
    20  var waitForEventTimeout = 5 * time.Second
    21  
    22  // MakeTxKV returns a text transaction, allong with expected key, value pair
    23  func MakeTxKV() ([]byte, []byte, []byte) {
    24  	k := []byte(tmrand.Str(8))
    25  	v := []byte(tmrand.Str(8))
    26  	return k, v, append(k, append([]byte("="), v...)...)
    27  }
    28  
    29  func TestHeaderEvents(t *testing.T) {
    30  	for i, c := range GetClients() {
    31  		i, c := i, c // capture params
    32  		t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
    33  			// start for this test it if it wasn't already running
    34  			if !c.IsRunning() {
    35  				// if so, then we start it, listen, and stop it.
    36  				err := c.Start()
    37  				require.Nil(t, err, "%d: %+v", i, err)
    38  				defer c.Stop()
    39  			}
    40  
    41  			evtTyp := types.EventNewBlockHeader
    42  			evt, err := client.WaitForOneEvent(c, evtTyp, waitForEventTimeout)
    43  			require.Nil(t, err, "%d: %+v", i, err)
    44  			_, ok := evt.(types.EventDataNewBlockHeader)
    45  			require.True(t, ok, "%d: %#v", i, evt)
    46  			// TODO: more checks...
    47  		})
    48  	}
    49  }
    50  
    51  func TestBlockEvents(t *testing.T) {
    52  	for i, c := range GetClients() {
    53  		i, c := i, c // capture params
    54  		t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
    55  
    56  			// start for this test it if it wasn't already running
    57  			if !c.IsRunning() {
    58  				// if so, then we start it, listen, and stop it.
    59  				err := c.Start()
    60  				require.Nil(t, err, "%d: %+v", i, err)
    61  				defer c.Stop()
    62  			}
    63  
    64  			// listen for a new block; ensure height increases by 1
    65  			var firstBlockHeight int64
    66  			for j := 0; j < 3; j++ {
    67  				evtTyp := types.EventNewBlock
    68  				evt, err := client.WaitForOneEvent(c, evtTyp, waitForEventTimeout)
    69  				require.Nil(t, err, "%d: %+v", j, err)
    70  				blockEvent, ok := evt.(types.EventDataNewBlock)
    71  				require.True(t, ok, "%d: %#v", j, evt)
    72  
    73  				block := blockEvent.Block
    74  				if j == 0 {
    75  					firstBlockHeight = block.Header.Height
    76  					continue
    77  				}
    78  
    79  				require.Equal(t, block.Header.Height, firstBlockHeight+int64(j))
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func TestTxEventsSentWithBroadcastTxAsync(t *testing.T) { testTxEventsSent(t, "async") }
    86  func TestTxEventsSentWithBroadcastTxSync(t *testing.T)  { testTxEventsSent(t, "sync") }
    87  
    88  func testTxEventsSent(t *testing.T, broadcastMethod string) {
    89  	for i, c := range GetClients() {
    90  		i, c := i, c // capture params
    91  		t.Run(reflect.TypeOf(c).String(), func(t *testing.T) {
    92  
    93  			// start for this test it if it wasn't already running
    94  			if !c.IsRunning() {
    95  				// if so, then we start it, listen, and stop it.
    96  				err := c.Start()
    97  				require.Nil(t, err, "%d: %+v", i, err)
    98  				defer c.Stop()
    99  			}
   100  
   101  			// make the tx
   102  			_, _, tx := MakeTxKV()
   103  			evtTyp := types.EventTx
   104  
   105  			// send
   106  			var (
   107  				txres *ctypes.ResultBroadcastTx
   108  				err   error
   109  			)
   110  			switch broadcastMethod {
   111  			case "async":
   112  				txres, err = c.BroadcastTxAsync(tx)
   113  			case "sync":
   114  				txres, err = c.BroadcastTxSync(tx)
   115  			default:
   116  				panic(fmt.Sprintf("Unknown broadcastMethod %s", broadcastMethod))
   117  			}
   118  
   119  			require.NoError(t, err)
   120  			require.Equal(t, txres.Code, abci.CodeTypeOK)
   121  
   122  			// and wait for confirmation
   123  			evt, err := client.WaitForOneEvent(c, evtTyp, waitForEventTimeout)
   124  			require.Nil(t, err, "%d: %+v", i, err)
   125  			// and make sure it has the proper info
   126  			txe, ok := evt.(types.EventDataTx)
   127  			require.True(t, ok, "%d: %#v", i, evt)
   128  			// make sure this is the proper tx
   129  			require.EqualValues(t, tx, txe.Tx)
   130  			require.True(t, txe.Result.IsOK())
   131  		})
   132  	}
   133  }
   134  
   135  // Test HTTPClient resubscribes upon disconnect && subscription error.
   136  // Test Local client resubscribes upon subscription error.
   137  func TestClientsResubscribe(t *testing.T) {
   138  	// TODO(melekes)
   139  }
   140  
   141  func TestHTTPReturnsErrorIfClientIsNotRunning(t *testing.T) {
   142  	c := getHTTPClient()
   143  
   144  	// on Subscribe
   145  	_, err := c.Subscribe(context.Background(), "TestHeaderEvents",
   146  		types.QueryForEvent(types.EventNewBlockHeader).String())
   147  	assert.Error(t, err)
   148  
   149  	// on Unsubscribe
   150  	err = c.Unsubscribe(context.Background(), "TestHeaderEvents",
   151  		types.QueryForEvent(types.EventNewBlockHeader).String())
   152  	assert.Error(t, err)
   153  
   154  	// on UnsubscribeAll
   155  	err = c.UnsubscribeAll(context.Background(), "TestHeaderEvents")
   156  	assert.Error(t, err)
   157  }