func (cli *TMSPClient) AppendTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) { reqres := cli.queueRequest(types.RequestAppendTx(tx)) cli.FlushSync() if cli.err != nil { return types.CodeType_InternalError, nil, "", cli.err } res := reqres.Response return res.Code, res.Data, res.Log, nil }
func (app *localAppConn) AppendTxAsync(tx []byte) *tmspcli.ReqRes { app.mtx.Lock() code, result, log := app.Application.AppendTx(tx) app.mtx.Unlock() app.Callback( tmsp.RequestAppendTx(tx), tmsp.ResponseAppendTx(code, result, log), ) return nil // TODO maybe create a ReqRes }
// Append a new tx to application func cmdAppendTx(c *cli.Context) { args := c.Args() if len(args) != 1 { fmt.Println("append_tx takes 1 argument") return } txString := args[0] tx := []byte(txString) if len(txString) > 2 && strings.HasPrefix(txString, "0x") { var err error tx, err = hex.DecodeString(txString[2:]) if err != nil { fmt.Println(err.Error()) return } } res, err := makeRequest(conn, types.RequestAppendTx(tx)) if err != nil { fmt.Println(err.Error()) return } printResponse(res, string(res.Data)) }
func (cli *TMSPClient) AppendTxAsync(tx []byte) *ReqRes { return cli.queueRequest(types.RequestAppendTx(tx)) }
func TestStream(t *testing.T) { numAppendTxs := 200000 // Start the listener _, err := server.StartListener("tcp://127.0.0.1:46658", NewDummyApplication()) 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 }