diff --git a/example/nil/nil_app.go b/example/nil/nil_app.go new file mode 100644 index 000000000..f417862be --- /dev/null +++ b/example/nil/nil_app.go @@ -0,0 +1,36 @@ +package nilapp + +import ( + "github.com/tendermint/tmsp/types" +) + +type NilApplication struct { +} + +func NewNilApplication() *NilApplication { + return &NilApplication{} +} + +func (app *NilApplication) Info() string { + return "nil" +} + +func (app *NilApplication) SetOption(key string, value string) (log string) { + return "" +} + +func (app *NilApplication) AppendTx(tx []byte) (code types.CodeType, result []byte, log string) { + return types.CodeType_OK, nil, "" +} + +func (app *NilApplication) CheckTx(tx []byte) (code types.CodeType, result []byte, log string) { + return types.CodeType_OK, nil, "" +} + +func (app *NilApplication) Commit() (hash []byte, log string) { + return []byte("nil"), "" +} + +func (app *NilApplication) Query(query []byte) (code types.CodeType, result []byte, log string) { + return types.CodeType_OK, nil, "" +} diff --git a/example/nil/nil_test.go b/example/nil/nil_test.go new file mode 100644 index 000000000..8ffa388e6 --- /dev/null +++ b/example/nil/nil_test.go @@ -0,0 +1,91 @@ +package nilapp + +import ( + "testing" + "time" + + . "github.com/tendermint/go-common" + "github.com/tendermint/tmsp/server" + "github.com/tendermint/tmsp/types" +) + +func TestStream(t *testing.T) { + + numAppendTxs := 200000 + + // Start the listener + _, err := server.NewServer("tcp://127.0.0.1:46658", NewNilApplication()) + if err != nil { + Exit(err.Error()) + } + + // Connect to the socket + conn, err := Connect("tcp://127.0.0.1:46658") + if err != nil { + Exit(err.Error()) + } + + // Read response data + done := make(chan struct{}) + go func() { + counter := 0 + for { + + var res = &types.Response{} + err := types.ReadMessage(conn, res) + if err != nil { + Exit(err.Error()) + } + + // Process response + switch res.Type { + case types.MessageType_AppendTx: + counter += 1 + if res.Code != types.CodeType_OK { + t.Error("AppendTx failed with ret_code", res.Code) + } + if counter > numAppendTxs { + t.Fatal("Too many AppendTx responses") + } + t.Log("response", counter) + if counter == numAppendTxs { + go func() { + time.Sleep(time.Second * 2) // Wait for a bit to allow counter overflow + close(done) + }() + } + case types.MessageType_Flush: + // ignore + default: + t.Error("Unexpected response type", res.Type) + } + } + }() + + // Write requests + for counter := 0; counter < numAppendTxs; counter++ { + // Send request + var req = types.RequestAppendTx([]byte("test")) + err := types.WriteMessage(req, conn) + if err != nil { + t.Fatal(err.Error()) + } + + // Sometimes send flush messages + if counter%123 == 0 { + t.Log("flush") + err := types.WriteMessage(types.RequestFlush(), conn) + if err != nil { + t.Fatal(err.Error()) + } + } + } + + // Send final flush message + err = types.WriteMessage(types.RequestFlush(), conn) + if err != nil { + t.Fatal(err.Error()) + } + + <-done +}