From 32f83f9494b2fc908808f5246dd854a846c72deb Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Sat, 14 May 2016 03:09:47 -0400 Subject: [PATCH] fix up tmsp-cli, tests --- cmd/tmsp-cli/tmsp-cli.go | 110 ++++++-------------------- example/dummy/dummy_test.go | 19 ++--- example/nil/nil_test.go | 19 ++--- tests/benchmarks/parallel/parallel.go | 2 +- tests/benchmarks/simple/simple.go | 9 ++- types/messages.go | 2 +- 6 files changed, 53 insertions(+), 108 deletions(-) diff --git a/cmd/tmsp-cli/tmsp-cli.go b/cmd/tmsp-cli/tmsp-cli.go index 275bb23a0..9e746b572 100644 --- a/cmd/tmsp-cli/tmsp-cli.go +++ b/cmd/tmsp-cli/tmsp-cli.go @@ -5,18 +5,18 @@ import ( "errors" "fmt" "io" - "net" "os" "strings" "github.com/codegangsta/cli" . "github.com/tendermint/go-common" "github.com/tendermint/go-wire/expr" + "github.com/tendermint/tmsp/client" "github.com/tendermint/tmsp/types" ) -// connection is a global variable so it can be reused by the console -var conn net.Conn +// clientection is a global variable so it can be reused by the console +var client tmspcli.Client func main() { app := cli.NewApp() @@ -103,9 +103,9 @@ func main() { } func before(c *cli.Context) error { - if conn == nil { + if client == nil { var err error - conn, err = Connect(c.GlobalString("address")) + client, err = tmspcli.NewClient(c.GlobalString("address"), false) if err != nil { Exit(err.Error()) } @@ -148,7 +148,9 @@ func cmdConsole(app *cli.App, c *cli.Context) error { args := []string{"tmsp"} args = append(args, strings.Split(string(line), " ")...) - app.Run(args) + if err := app.Run(args); err != nil { + return err + } } return nil } @@ -159,21 +161,15 @@ func cmdEcho(c *cli.Context) error { if len(args) != 1 { return errors.New("Command echo takes 1 argument") } - res, err := makeRequest(conn, types.RequestEcho(args[0])) - if err != nil { - return err - } - printResponse(res, string(res.Data)) + res := client.EchoSync(args[0]) + printResponse(res, string(res.Data), false) return nil } // Get some info from the application func cmdInfo(c *cli.Context) error { - res, err := makeRequest(conn, types.RequestInfo()) - if err != nil { - return err - } - printResponse(res, string(res.Data)) + res := client.InfoSync() + printResponse(res, string(res.Data), false) return nil } @@ -183,11 +179,8 @@ func cmdSetOption(c *cli.Context) error { if len(args) != 2 { return errors.New("Command set_option takes 2 arguments (key, value)") } - res, err := makeRequest(conn, types.RequestSetOption(args[0], args[1])) - if err != nil { - return err - } - printResponse(res, Fmt("%s=%s", args[0], args[1])) + res := client.SetOptionSync(args[0], args[1]) + printResponse(res, Fmt("%s=%s", args[0], args[1]), false) return nil } @@ -203,11 +196,8 @@ func cmdAppendTx(c *cli.Context) error { return err } - res, err := makeRequest(conn, types.RequestAppendTx(txBytes)) - if err != nil { - return err - } - printResponse(res, string(res.Data)) + res := client.AppendTxSync(txBytes) + printResponse(res, string(res.Data), true) return nil } @@ -223,21 +213,15 @@ func cmdCheckTx(c *cli.Context) error { return err } - res, err := makeRequest(conn, types.RequestCheckTx(txBytes)) - if err != nil { - return err - } - printResponse(res, string(res.Data)) + res := client.CheckTxSync(txBytes) + printResponse(res, string(res.Data), true) return nil } // Get application Merkle root hash func cmdCommit(c *cli.Context) error { - res, err := makeRequest(conn, types.RequestCommit()) - if err != nil { - return err - } - printResponse(res, Fmt("%X", res.Data)) + res := client.CommitSync() + printResponse(res, Fmt("%X", res.Data), false) return nil } @@ -253,24 +237,20 @@ func cmdQuery(c *cli.Context) error { return err } - res, err := makeRequest(conn, types.RequestQuery(queryBytes)) - if err != nil { - return err - } - printResponse(res, string(res.Data)) + res := client.QuerySync(queryBytes) + printResponse(res, string(res.Data), true) return nil } //-------------------------------------------------------------------------------- -func printResponse(res *types.Response, s string) { - switch res.Type { - case types.MessageType_AppendTx, types.MessageType_CheckTx, types.MessageType_Query: +func printResponse(res types.Result, s string, printCode bool) { + if printCode { fmt.Printf("-> code: %s\n", res.Code.String()) } - if res.Error != "" { + /*if res.Error != "" { fmt.Printf("-> error: %s\n", res.Error) - } + }*/ if s != "" { fmt.Printf("-> data: {%s}\n", s) } @@ -279,41 +259,3 @@ func printResponse(res *types.Response, s string) { } } - -func responseString(res *types.Response) string { - return Fmt("type: %v\tdata: %v\tcode: %v", res.Type, res.Data, res.Code) -} - -func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) { - - // Write desired request - err := types.WriteMessage(req, conn) - if err != nil { - return nil, err - } - - // Write flush request - err = types.WriteMessage(types.RequestFlush(), conn) - if err != nil { - return nil, err - } - - // Read desired response - var res = &types.Response{} - err = types.ReadMessage(conn, res) - if err != nil { - return nil, err - } - - // Read flush response - var resFlush = &types.Response{} - err = types.ReadMessage(conn, resFlush) - if err != nil { - return nil, err - } - if resFlush.Type != types.MessageType_Flush { - return nil, errors.New(Fmt("Expected types.MessageType_Flush but got %v instead", resFlush.Type)) - } - - return res, nil -} diff --git a/example/dummy/dummy_test.go b/example/dummy/dummy_test.go index 22338b00c..bfde40012 100644 --- a/example/dummy/dummy_test.go +++ b/example/dummy/dummy_test.go @@ -1,6 +1,7 @@ package dummy import ( + "reflect" "testing" "time" @@ -39,11 +40,11 @@ func TestStream(t *testing.T) { } // Process response - switch res.Type { - case types.MessageType_AppendTx: + switch r := res.Responses.(type) { + case *types.Response_AppendTx: counter += 1 - if res.Code != types.CodeType_OK { - t.Error("AppendTx failed with ret_code", res.Code) + if r.AppendTx.Code != types.CodeType_OK { + t.Error("AppendTx failed with ret_code", r.AppendTx.Code) } if counter > numAppendTxs { t.Fatal("Too many AppendTx responses") @@ -55,10 +56,10 @@ func TestStream(t *testing.T) { close(done) }() } - case types.MessageType_Flush: + case *types.Response_Flush: // ignore default: - t.Error("Unexpected response type", res.Type) + t.Error("Unexpected response type", reflect.TypeOf(res.Responses)) } } }() @@ -66,7 +67,7 @@ func TestStream(t *testing.T) { // Write requests for counter := 0; counter < numAppendTxs; counter++ { // Send request - var req = types.RequestAppendTx([]byte("test")) + var req = types.ToRequestAppendTx([]byte("test")) err := types.WriteMessage(req, conn) if err != nil { t.Fatal(err.Error()) @@ -75,7 +76,7 @@ func TestStream(t *testing.T) { // Sometimes send flush messages if counter%123 == 0 { t.Log("flush") - err := types.WriteMessage(types.RequestFlush(), conn) + err := types.WriteMessage(types.ToRequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } @@ -83,7 +84,7 @@ func TestStream(t *testing.T) { } // Send final flush message - err = types.WriteMessage(types.RequestFlush(), conn) + err = types.WriteMessage(types.ToRequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } diff --git a/example/nil/nil_test.go b/example/nil/nil_test.go index 0f3f6adb4..953e86d99 100644 --- a/example/nil/nil_test.go +++ b/example/nil/nil_test.go @@ -1,6 +1,7 @@ package nilapp import ( + "reflect" "testing" "time" @@ -39,11 +40,11 @@ func TestStream(t *testing.T) { } // Process response - switch res.Type { - case types.MessageType_AppendTx: + switch r := res.Responses.(type) { + case *types.Response_AppendTx: counter += 1 - if res.Code != types.CodeType_OK { - t.Error("AppendTx failed with ret_code", res.Code) + if r.AppendTx.Code != types.CodeType_OK { + t.Error("AppendTx failed with ret_code", r.AppendTx.Code) } if counter > numAppendTxs { t.Fatal("Too many AppendTx responses") @@ -55,10 +56,10 @@ func TestStream(t *testing.T) { close(done) }() } - case types.MessageType_Flush: + case *types.Response_Flush: // ignore default: - t.Error("Unexpected response type", res.Type) + t.Error("Unexpected response type", reflect.TypeOf(res.Responses)) } } }() @@ -66,7 +67,7 @@ func TestStream(t *testing.T) { // Write requests for counter := 0; counter < numAppendTxs; counter++ { // Send request - var req = types.RequestAppendTx([]byte("test")) + var req = types.ToRequestAppendTx([]byte("test")) err := types.WriteMessage(req, conn) if err != nil { t.Fatal(err.Error()) @@ -75,7 +76,7 @@ func TestStream(t *testing.T) { // Sometimes send flush messages if counter%123 == 0 { t.Log("flush") - err := types.WriteMessage(types.RequestFlush(), conn) + err := types.WriteMessage(types.ToRequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } @@ -83,7 +84,7 @@ func TestStream(t *testing.T) { } // Send final flush message - err = types.WriteMessage(types.RequestFlush(), conn) + err = types.WriteMessage(types.ToRequestFlush(), conn) if err != nil { t.Fatal(err.Error()) } diff --git a/tests/benchmarks/parallel/parallel.go b/tests/benchmarks/parallel/parallel.go index 7cab2eb26..db57e19bf 100644 --- a/tests/benchmarks/parallel/parallel.go +++ b/tests/benchmarks/parallel/parallel.go @@ -36,7 +36,7 @@ func main() { counter := 0 for i := 0; ; i++ { var bufWriter = bufio.NewWriter(conn) - var req = types.RequestEcho("foobar") + var req = types.ToRequestEcho("foobar") err := types.WriteMessage(req, bufWriter) if err != nil { diff --git a/tests/benchmarks/simple/simple.go b/tests/benchmarks/simple/simple.go index 8fb02585e..61016ab87 100644 --- a/tests/benchmarks/simple/simple.go +++ b/tests/benchmarks/simple/simple.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "net" + "reflect" //"encoding/hex" . "github.com/tendermint/go-common" @@ -21,7 +22,7 @@ func main() { // Make a bunch of requests counter := 0 for i := 0; ; i++ { - req := types.RequestEcho("foobar") + req := types.ToRequestEcho("foobar") _, err := makeRequest(conn, req) if err != nil { Exit(err.Error()) @@ -41,7 +42,7 @@ func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) { if err != nil { return nil, err } - err = types.WriteMessage(types.RequestFlush(), bufWriter) + err = types.WriteMessage(types.ToRequestFlush(), bufWriter) if err != nil { return nil, err } @@ -61,8 +62,8 @@ func makeRequest(conn net.Conn, req *types.Request) (*types.Response, error) { if err != nil { return nil, err } - if resFlush.Type != types.MessageType_Flush { - return nil, errors.New(Fmt("Expected flush response but got something else: %v", resFlush.Type)) + if _, ok := resFlush.Responses.(*types.Response_Flush); !ok { + return nil, errors.New(Fmt("Expected flush response but got something else: %v", reflect.TypeOf(resFlush))) } return res, nil diff --git a/types/messages.go b/types/messages.go index 730e3d6f1..4249c1c0d 100644 --- a/types/messages.go +++ b/types/messages.go @@ -9,7 +9,7 @@ import ( func ToRequestEcho(message string) *Request { return &Request{ - Requests: &Request_Echo{&RequestEcho{}}, + Requests: &Request_Echo{&RequestEcho{message}}, } }