10 "google.golang.org/grpc"
12 "golang.org/x/net/context"
14 abcicli "github.com/tendermint/abci/client"
15 "github.com/tendermint/abci/example/dummy"
16 "github.com/tendermint/abci/server"
17 "github.com/tendermint/abci/types"
18 cmn "github.com/tendermint/tmlibs/common"
19 "github.com/tendermint/tmlibs/log"
22 func TestDummy(t *testing.T) {
23 fmt.Println("### Testing Dummy")
24 testStream(t, dummy.NewDummyApplication())
27 func TestBaseApp(t *testing.T) {
28 fmt.Println("### Testing BaseApp")
29 testStream(t, types.NewBaseApplication())
32 func TestGRPC(t *testing.T) {
33 fmt.Println("### Testing GRPC")
34 testGRPCSync(t, types.NewGRPCApplication(types.NewBaseApplication()))
37 func testStream(t *testing.T, app types.Application) {
38 numDeliverTxs := 200000
41 server := server.NewSocketServer("unix://test.sock", app)
42 server.SetLogger(log.TestingLogger().With("module", "abci-server"))
43 if _, err := server.Start(); err != nil {
44 t.Fatalf("Error starting socket server: %v", err.Error())
48 // Connect to the socket
49 client := abcicli.NewSocketClient("unix://test.sock", false)
50 client.SetLogger(log.TestingLogger().With("module", "abci-client"))
51 if _, err := client.Start(); err != nil {
52 t.Fatalf("Error starting socket client: %v", err.Error())
56 done := make(chan struct{})
58 client.SetResponseCallback(func(req *types.Request, res *types.Response) {
60 switch r := res.Value.(type) {
61 case *types.Response_DeliverTx:
63 if r.DeliverTx.Code != types.CodeType_OK {
64 t.Error("DeliverTx failed with ret_code", r.DeliverTx.Code)
66 if counter > numDeliverTxs {
67 t.Fatalf("Too many DeliverTx responses. Got %d, expected %d", counter, numDeliverTxs)
69 if counter == numDeliverTxs {
71 time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow
76 case *types.Response_Flush:
79 t.Error("Unexpected response type", reflect.TypeOf(res.Value))
84 for counter := 0; counter < numDeliverTxs; counter++ {
86 reqRes := client.DeliverTxAsync([]byte("test"))
90 // Sometimes send flush messages
97 // Send final flush message
103 //-------------------------
106 func dialerFunc(addr string, timeout time.Duration) (net.Conn, error) {
107 return cmn.Connect(addr)
110 func testGRPCSync(t *testing.T, app *types.GRPCApplication) {
111 numDeliverTxs := 2000
113 // Start the listener
114 server := server.NewGRPCServer("unix://test.sock", app)
115 server.SetLogger(log.TestingLogger().With("module", "abci-server"))
116 if _, err := server.Start(); err != nil {
117 t.Fatalf("Error starting GRPC server: %v", err.Error())
121 // Connect to the socket
122 conn, err := grpc.Dial("unix://test.sock", grpc.WithInsecure(), grpc.WithDialer(dialerFunc))
124 t.Fatalf("Error dialing GRPC server: %v", err.Error())
128 client := types.NewABCIApplicationClient(conn)
131 for counter := 0; counter < numDeliverTxs; counter++ {
133 response, err := client.DeliverTx(context.Background(), &types.RequestDeliverTx{[]byte("test")})
135 t.Fatalf("Error in GRPC DeliverTx: %v", err.Error())
138 if response.Code != types.CodeType_OK {
139 t.Error("DeliverTx failed with ret_code", response.Code)
141 if counter > numDeliverTxs {
142 t.Fatal("Too many DeliverTx responses")
144 t.Log("response", counter)
145 if counter == numDeliverTxs {
147 time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow