Browse Source

tmsp batch and some tests

pull/1780/head
Ethan Buchman 9 years ago
parent
commit
3e721456f5
4 changed files with 150 additions and 3 deletions
  1. +22
    -0
      cmd/counter/main.go
  2. +27
    -2
      cmd/tmsp/cli.go
  3. +1
    -1
      example/counter.go
  4. +100
    -0
      test.sh

+ 22
- 0
cmd/counter/main.go View File

@ -0,0 +1,22 @@
package main
import (
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/example"
"github.com/tendermint/tmsp/server"
)
func main() {
// Start the listener
_, err := server.StartListener("tcp://127.0.0.1:8080", example.NewCounterApplication())
if err != nil {
Exit(err.Error())
}
// Wait forever
TrapSignal(func() {
// Cleanup
})
}

+ 27
- 2
cmd/tmsp/cli.go View File

@ -4,6 +4,7 @@ import (
"bufio"
"encoding/hex"
"fmt"
"io"
"net"
"os"
"strings"
@ -30,6 +31,13 @@ func main() {
},
}
app.Commands = []cli.Command{
{
Name: "batch",
Usage: "Run a batch of tmsp commands against an application",
Action: func(c *cli.Context) {
cmdBatch(app, c)
},
},
{
Name: "console",
Usage: "Start an interactive tmsp console for multiple commands",
@ -98,6 +106,23 @@ func before(c *cli.Context) error {
//--------------------------------------------------------------------------------
func cmdBatch(app *cli.App, c *cli.Context) {
bufReader := bufio.NewReader(os.Stdin)
for {
line, more, err := bufReader.ReadLine()
if more {
Exit("input line is too long")
} else if err == io.EOF {
break
} else if err != nil {
Exit(err.Error())
}
args := []string{"tmsp"}
args = append(args, strings.Split(string(line), " ")...)
app.Run(args)
}
}
func cmdConsole(app *cli.App, c *cli.Context) {
for {
fmt.Printf("> ")
@ -134,7 +159,7 @@ func cmdSetOption(c *cli.Context) {
if err != nil {
Exit(err.Error())
}
fmt.Printf("Set option %s = %s\n", args[0], args[1])
fmt.Printf("%s=%s\n", args[0], args[1])
}
// Append a new tx to application
@ -166,7 +191,7 @@ func cmdGetHash(c *cli.Context) {
if err != nil {
Exit(err.Error())
}
fmt.Println("Got hash:", Fmt("%X", res.(types.ResponseGetHash).Hash))
fmt.Printf("%X\n", res.(types.ResponseGetHash).Hash)
}
// Commit the application state


+ 1
- 1
example/counter.go View File

@ -69,7 +69,7 @@ func (appC *CounterAppContext) AppendTx(tx []byte) ([]types.Event, types.RetCode
func (appC *CounterAppContext) GetHash() ([]byte, types.RetCode) {
hash := make([]byte, 32)
binary.PutVarint(hash, int64(appC.hashCount))
binary.PutVarint(hash, int64(appC.txCount))
appC.hashCount += 1
return hash, 0
}


+ 100
- 0
test.sh View File

@ -0,0 +1,100 @@
#! /bin/bash
# Make sure the tmsp cli can connect to the dummy
echo "Dummy test ..."
dummy &> /dev/null &
PID=`echo $!`
sleep 1
RESULT_HASH=`tmsp get_hash`
if [[ "$RESULT_HASH" != "" ]]; then
echo "Expected nothing but got: $RESULT_HASH"
exit 1
fi
echo "... Pass!"
echo ""
# Add a tx, get hash, commit, get hash
# hashes should be non-empty and identical
echo "Dummy batch test ..."
OUTPUT=`(tmsp batch) <<STDIN
append_tx abc
get_hash
commit
get_hash
STDIN`
HASH1=`echo "$OUTPUT" | tail -n 3 | head -n 1`
HASH2=`echo "$OUTPUT" | tail -n 1`
if [[ "$HASH1" == "" ]]; then
echo "Expected non empty hash!"
exit 1
fi
if [[ "$HASH1" != "$HASH2" ]]; then
echo "Expected hashes before and after commit to match: $HASH1, $HASH2"
exit 1
fi
echo "... Pass!"
echo ""
# Start a new connection and ensure the hash is the same
echo "New connection test ..."
RESULT_HASH=`tmsp get_hash`
if [[ "$HASH1" != "$RESULT_HASH" ]]; then
echo "Expected hash to persist as $HASH1 for new connection. Got $RESULT_HASH"
exit 1
fi
echo "... Pass!"
echo ""
kill $PID
sleep 1
# test the counter app
echo "Counter test ..."
counter &> /dev/null &
PID=`echo $!`
sleep 1
OUTPUT=`(tmsp batch) <<STDIN
set_option serial on
get_hash
append_tx abc
STDIN`
# why can't we pick up the non-zero exit code here?
# echo $?
HASH1=`echo "$OUTPUT" | tail -n +2 | head -n 1`
if [[ "${HASH1:0:2}" != "00" ]]; then
echo "Expected opening hash to lead with 00. Got $HASH1"
exit 1
fi
OUTPUT=`(tmsp batch) <<STDIN
set_option serial on
append_tx 0x00
get_hash
append_tx 0x02
get_hash
STDIN`
HASH1=`echo "$OUTPUT" | tail -n +3 | head -n 1`
HASH2=`echo "$OUTPUT" | tail -n +5 | head -n 1`
if [[ "${HASH1:0:2}" != "02" ]]; then
echo "Expected hash to lead with 02. Got $HASH1"
exit 1
fi
if [[ "${HASH2:0:2}" != "04" ]]; then
echo "Expected hash to lead with 04. Got $HASH2"
exit 1
fi
echo "... Pass!"
echo ""
kill $PID

Loading…
Cancel
Save