github.com/pokt-network/tendermint@v0.32.11-0.20230426215212-59310158d3e9/abci/example/example_test.go (about)

     1  package example
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"reflect"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"google.golang.org/grpc"
    13  
    14  	"golang.org/x/net/context"
    15  
    16  	"github.com/tendermint/tendermint/libs/log"
    17  	tmnet "github.com/tendermint/tendermint/libs/net"
    18  
    19  	abcicli "github.com/tendermint/tendermint/abci/client"
    20  	"github.com/tendermint/tendermint/abci/example/code"
    21  	"github.com/tendermint/tendermint/abci/example/kvstore"
    22  	abciserver "github.com/tendermint/tendermint/abci/server"
    23  	"github.com/tendermint/tendermint/abci/types"
    24  )
    25  
    26  func TestKVStore(t *testing.T) {
    27  	fmt.Println("### Testing KVStore")
    28  	testStream(t, kvstore.NewApplication())
    29  }
    30  
    31  func TestBaseApp(t *testing.T) {
    32  	fmt.Println("### Testing BaseApp")
    33  	testStream(t, types.NewBaseApplication())
    34  }
    35  
    36  func TestGRPC(t *testing.T) {
    37  	fmt.Println("### Testing GRPC")
    38  	testGRPCSync(t, types.NewGRPCApplication(types.NewBaseApplication()))
    39  }
    40  
    41  func testStream(t *testing.T, app types.Application) {
    42  	numDeliverTxs := 20000
    43  
    44  	// Start the listener
    45  	server := abciserver.NewSocketServer("unix://test.sock", app)
    46  	server.SetLogger(log.TestingLogger().With("module", "abci-server"))
    47  	if err := server.Start(); err != nil {
    48  		require.NoError(t, err, "Error starting socket server")
    49  	}
    50  	defer server.Stop()
    51  
    52  	// Connect to the socket
    53  	client := abcicli.NewSocketClient("unix://test.sock", false)
    54  	client.SetLogger(log.TestingLogger().With("module", "abci-client"))
    55  	if err := client.Start(); err != nil {
    56  		t.Fatalf("Error starting socket client: %v", err.Error())
    57  	}
    58  	defer client.Stop()
    59  
    60  	done := make(chan struct{})
    61  	counter := 0
    62  	client.SetResponseCallback(func(req *types.Request, res *types.Response) {
    63  		// Process response
    64  		switch r := res.Value.(type) {
    65  		case *types.Response_DeliverTx:
    66  			counter++
    67  			if r.DeliverTx.Code != code.CodeTypeOK {
    68  				t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
    69  			}
    70  			if counter > numDeliverTxs {
    71  				t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
    72  			}
    73  			if counter == numDeliverTxs {
    74  				go func() {
    75  					time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
    76  					close(done)
    77  				}()
    78  				return
    79  			}
    80  		case *types.Response_Flush:
    81  			// ignore
    82  		default:
    83  			t.Error("Unexpected response type", reflect.TypeOf(res.Value))
    84  		}
    85  	})
    86  
    87  	// Write requests
    88  	for counter := 0; counter < numDeliverTxs; counter++ {
    89  		// Send request
    90  		reqRes := client.DeliverTxAsync(types.RequestDeliverTx{Tx: []byte("test")})
    91  		_ = reqRes
    92  		// check err ?
    93  
    94  		// Sometimes send flush messages
    95  		if counter%123 == 0 {
    96  			client.FlushAsync()
    97  			// check err ?
    98  		}
    99  	}
   100  
   101  	// Send final flush message
   102  	client.FlushAsync()
   103  
   104  	<-done
   105  }
   106  
   107  //-------------------------
   108  // test grpc
   109  
   110  func dialerFunc(ctx context.Context, addr string) (net.Conn, error) {
   111  	return tmnet.Connect(addr)
   112  }
   113  
   114  func testGRPCSync(t *testing.T, app types.ABCIApplicationServer) {
   115  	numDeliverTxs := 2000
   116  
   117  	// Start the listener
   118  	server := abciserver.NewGRPCServer("unix://test.sock", app)
   119  	server.SetLogger(log.TestingLogger().With("module", "abci-server"))
   120  	if err := server.Start(); err != nil {
   121  		t.Fatalf("Error starting GRPC server: %v", err.Error())
   122  	}
   123  	defer server.Stop()
   124  
   125  	// Connect to the socket
   126  	conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithContextDialer(dialerFunc))
   127  	if err != nil {
   128  		t.Fatalf("Error dialing GRPC server: %v", err.Error())
   129  	}
   130  	defer conn.Close()
   131  
   132  	client := types.NewABCIApplicationClient(conn)
   133  
   134  	// Write requests
   135  	for counter := 0; counter < numDeliverTxs; counter++ {
   136  		// Send request
   137  		response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{Tx: []byte("test")})
   138  		if err != nil {
   139  			t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
   140  		}
   141  		counter++
   142  		if response.Code != code.CodeTypeOK {
   143  			t.Error("DeliverTx failed with ret_code", response.Code)
   144  		}
   145  		if counter > numDeliverTxs {
   146  			t.Fatal("Too many DeliverTx responses")
   147  		}
   148  		t.Log("response", counter)
   149  		if counter == numDeliverTxs {
   150  			go func() {
   151  				time.Sleep(time.Second * 1) // Wait for a bit to allow counter overflow
   152  			}()
   153  		}
   154  
   155  	}
   156  }