Browse Source

Added counter test written in Golang

pull/1780/head
Jae Kwon 8 years ago
parent
commit
90e38f08f4
14 changed files with 154 additions and 167 deletions
  1. +12
    -3
      client/client.go
  2. +2
    -2
      cmd/counter/main.go
  3. +2
    -2
      cmd/dummy/main.go
  4. +1
    -1
      example/counter/counter.go
  5. +1
    -1
      example/dummy/dummy.go
  6. +1
    -1
      example/dummy/dummy_test.go
  7. +1
    -1
      example/python/app.py
  8. +2
    -2
      example/python/tmsp/msg.py
  9. +1
    -1
      example/python3/app.py
  10. +2
    -2
      example/python3/tmsp/msg.py
  11. +3
    -12
      tests/test.sh
  12. +126
    -0
      tests/test_counter.go
  13. +0
    -78
      tests/test_counter.sh
  14. +0
    -61
      tests/test_dummy.sh

+ 12
- 3
client/client.go View File

@ -217,6 +217,11 @@ func (cli *TMSPClient) QueryAsync(query []byte) *ReqRes {
//----------------------------------------
func (cli *TMSPClient) FlushSync() error {
cli.queueRequest(types.RequestFlush()).Wait()
return cli.err
}
func (cli *TMSPClient) InfoSync() (info string, err error) {
reqres := cli.queueRequest(types.RequestInfo())
cli.FlushSync()
@ -226,9 +231,13 @@ func (cli *TMSPClient) InfoSync() (info string, err error) {
return string(reqres.Response.Data), nil
}
func (cli *TMSPClient) FlushSync() error {
cli.queueRequest(types.RequestFlush()).Wait()
return cli.err
func (cli *TMSPClient) SetOptionSync(key string, value string) (log string, err error) {
reqres := cli.queueRequest(types.RequestSetOption(key, value))
cli.FlushSync()
if cli.err != nil {
return "", cli.err
}
return reqres.Response.Log, nil
}
func (cli *TMSPClient) AppendTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) {


+ 2
- 2
cmd/counter/main.go View File

@ -4,7 +4,7 @@ import (
"flag"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/example/golang"
"github.com/tendermint/tmsp/example/counter"
"github.com/tendermint/tmsp/server"
)
@ -13,7 +13,7 @@ func main() {
addrPtr := flag.String("addr", "tcp://0.0.0.0:46658", "Listen address")
serialPtr := flag.Bool("serial", false, "Enforce incrementing (serial) txs")
flag.Parse()
app := example.NewCounterApplication(*serialPtr)
app := counter.NewCounterApplication(*serialPtr)
// Start the listener
_, err := server.StartListener(*addrPtr, app)


+ 2
- 2
cmd/dummy/main.go View File

@ -4,7 +4,7 @@ import (
"flag"
. "github.com/tendermint/go-common"
"github.com/tendermint/tmsp/example/golang"
"github.com/tendermint/tmsp/example/dummy"
"github.com/tendermint/tmsp/server"
)
@ -14,7 +14,7 @@ func main() {
flag.Parse()
// Start the listener
_, err := server.StartListener(*addrPtr, example.NewDummyApplication())
_, err := server.StartListener(*addrPtr, dummy.NewDummyApplication())
if err != nil {
Exit(err.Error())
}


example/golang/counter.go → example/counter/counter.go View File


example/golang/dummy.go → example/dummy/dummy.go View File


example/golang/dummy_test.go → example/dummy/dummy_test.go View File


+ 1
- 1
example/python/app.py View File

@ -47,7 +47,7 @@ class CounterApplication():
return 6
return 0
def get_hash(self):
def commit(self):
self.hashCount += 1
if self.txCount == 0:
return "", 0


+ 2
- 2
example/python/tmsp/msg.py View File

@ -8,7 +8,7 @@ message_types = {
0x04: "set_option",
0x21: "append_tx",
0x22: "check_tx",
0x23: "get_hash",
0x23: "commit",
0x24: "add_listener",
0x25: "rm_listener",
}
@ -38,7 +38,7 @@ class RequestDecoder():
def check_tx(self):
return decode_string(self.reader)
def get_hash(self):
def commit(self):
return
def add_listener(self):


+ 1
- 1
example/python3/app.py View File

@ -47,7 +47,7 @@ class CounterApplication():
return 6
return 0
def get_hash(self):
def commit(self):
self.hashCount += 1
if self.txCount == 0:
return "", 0


+ 2
- 2
example/python3/tmsp/msg.py View File

@ -8,7 +8,7 @@ message_types = {
0x04: "set_option",
0x21: "append_tx",
0x22: "check_tx",
0x23: "get_hash",
0x23: "commit",
0x24: "add_listener",
0x25: "rm_listener",
}
@ -38,7 +38,7 @@ class RequestDecoder():
def check_tx(self):
return decode_string(self.reader)
def get_hash(self):
def commit(self):
return
def add_listener(self):


+ 3
- 12
tests/test.sh View File

@ -2,17 +2,8 @@
ROOT=$GOPATH/src/github.com/tendermint/tmsp
cd $ROOT
# test golang dummy
bash tests/test_dummy.sh
# test golang counter
bash tests/test_counter.sh
# test js counter
cd example/js
COUNTER_APP="node app.js" bash $ROOT/tests/test_counter.sh
# test python counter
cd ../python
COUNTER_APP="python app.py" bash $ROOT/tests/test_counter.sh
COUNTER_APP="counter" go run $ROOT/tests/test_counter.go
# test nodejs counter
COUNTER_APP="node ../js-tmsp/example/app.js" go run $ROOT/tests/test_counter.go

+ 126
- 0
tests/test_counter.go View File

@ -0,0 +1,126 @@
package main
import (
"bytes"
"fmt"
"os"
"time"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-process"
"github.com/tendermint/tmsp/client"
"github.com/tendermint/tmsp/types"
)
func main() {
// Run tests
testBasic()
fmt.Println("Success!")
}
func testBasic() {
fmt.Println("Running basic tests")
appProc := startApp()
defer appProc.StopProcess(true)
client := startClient()
defer client.Stop()
setOption(client, "serial", "on")
commit(client, nil)
appendTx(client, []byte("abc"), types.CodeType_BadNonce, nil)
commit(client, nil)
appendTx(client, []byte{0x00}, types.CodeType_OK, nil)
commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 1})
appendTx(client, []byte{0x00}, types.CodeType_BadNonce, nil)
appendTx(client, []byte{0x01}, types.CodeType_OK, nil)
appendTx(client, []byte{0x00, 0x02}, types.CodeType_OK, nil)
appendTx(client, []byte{0x00, 0x03}, types.CodeType_OK, nil)
appendTx(client, []byte{0x00, 0x00, 0x04}, types.CodeType_OK, nil)
appendTx(client, []byte{0x00, 0x00, 0x06}, types.CodeType_BadNonce, nil)
commit(client, []byte{0, 0, 0, 0, 0, 0, 0, 5})
}
//----------------------------------------
func startApp() *process.Process {
counterApp := os.Getenv("COUNTER_APP")
if counterApp == "" {
panic("No COUNTER_APP specified")
}
// Start the app
//outBuf := NewBufferCloser(nil)
proc, err := process.StartProcess("counter_app",
"bash",
[]string{"-c", counterApp},
nil,
os.Stdout,
)
if err != nil {
panic("running counter_app: " + err.Error())
}
// TODO a better way to handle this?
time.Sleep(time.Second)
return proc
}
func startClient() *tmspcli.TMSPClient {
// Start client
client, err := tmspcli.NewTMSPClient("tcp://127.0.0.1:46658")
if err != nil {
panic("connecting to counter_app: " + err.Error())
}
return client
}
func setOption(client *tmspcli.TMSPClient, key, value string) {
log, err := client.SetOptionSync(key, value)
if err != nil {
panic(Fmt("setting %v=%v: %v\nlog: %v", key, value, err, log))
}
}
func commit(client *tmspcli.TMSPClient, hashExp []byte) {
hash, log, err := client.CommitSync()
if err != nil {
panic(Fmt("committing %v\nlog: %v", err, log))
}
if !bytes.Equal(hash, hashExp) {
panic(Fmt("Commit hash was unexpected. Got %X expected %X",
hash, hashExp))
}
}
func appendTx(client *tmspcli.TMSPClient, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
code, data, log, err := client.AppendTxSync(txBytes)
if err != nil {
panic(Fmt("appending tx %X: %v\nlog: %v", txBytes, err, log))
}
if code != codeExp {
panic(Fmt("AppendTx response code was unexpected. Got %v expected %v. Log: %v",
code, codeExp, log))
}
if !bytes.Equal(data, dataExp) {
panic(Fmt("AppendTx response data was unexpected. Got %X expected %X",
data, dataExp))
}
}
func checkTx(client *tmspcli.TMSPClient, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
code, data, log, err := client.CheckTxSync(txBytes)
if err != nil {
panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, err, log))
}
if code != codeExp {
panic(Fmt("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
code, codeExp, log))
}
if !bytes.Equal(data, dataExp) {
panic(Fmt("CheckTx response data was unexpected. Got %X expected %X",
data, dataExp))
}
}

+ 0
- 78
tests/test_counter.sh View File

@ -1,78 +0,0 @@
#! /bin/bash
function finish {
echo "Cleaning up..."
ps -p $PID > /dev/null
if [[ "$?" == "0" ]]; then
kill -9 $PID
fi
}
trap finish EXIT
# so we can test other languages
if [[ "$COUNTER_APP" == "" ]]; then
COUNTER_APP="counter"
fi
echo "Testing counter app for: $COUNTER_APP"
# run the counter app
$COUNTER_APP &> /dev/null &
PID=`echo $!`
if [[ "$?" != 0 ]]; then
echo "Error running tmsp app"
echo $OUTPUT
exit 1
fi
sleep 1
OUTPUT=`(tmsp-cli batch) <<STDIN
set_option serial on
get_hash
append_tx abc
STDIN`
if [[ "$?" != 0 ]]; then
echo "Error running tmsp batch command"
echo $OUTPUT
exit 1
fi
# why can't we pick up the non-zero exit code here?
# echo $?
HASH1=`echo "$OUTPUT" | tail -n +2 | head -n 1`
if [[ "${HASH1}" != "" ]]; then
echo "Expected opening hash to be empty. Got $HASH1"
exit 1
fi
OUTPUT=`(tmsp-cli batch) <<STDIN
set_option serial on
append_tx 0x00
get_hash
append_tx 0x01
get_hash
STDIN`
if [[ "$?" != 0 ]]; then
echo "Error running tmsp command"
echo $OUTPUT
exit 1
fi
HASH1=`echo "$OUTPUT" | tail -n +3 | head -n 1`
HASH2=`echo "$OUTPUT" | tail -n +5 | head -n 1`
if [[ "${HASH1: -2}" != "01" ]]; then
echo "Expected hash to lead with 01. Got $HASH1"
exit 1
fi
if [[ "${HASH2: -2}" != "02" ]]; then
echo "Expected hash to lead with 02. Got $HASH2"
exit 1
fi
echo "... Pass!"
echo ""

+ 0
- 61
tests/test_dummy.sh View File

@ -1,61 +0,0 @@
#! /bin/bash
function finish {
echo "Cleaning up..."
ps -p $PID > /dev/null
if [[ "$?" == "0" ]]; then
kill -9 $PID
fi
}
trap finish EXIT
# Make sure the tmsp cli can connect to the dummy
echo "Dummy test ..."
dummy &> /dev/null &
PID=`echo $!`
sleep 1
RESULT_HASH=`tmsp-cli get_hash`
if [[ "$RESULT_HASH" != "" ]]; then
echo "Expected nothing but got: $RESULT_HASH"
exit 1
fi
echo "... Pass!"
echo ""
# Add a tx, get hash, get hash
# hashes should be non-empty and identical
echo "Dummy batch test ..."
OUTPUT=`(tmsp-cli batch) <<STDIN
append_tx abc
get_hash
get_hash
STDIN`
HASH1=`echo "$OUTPUT" | tail -n 2 | head -n 1`
HASH2=`echo "$OUTPUT" | tail -n 1`
if [[ "$HASH1" == "" ]]; then
echo "Expected non empty hash!"
exit 1
fi
if [[ "$HASH1" == "EOF" ]]; then
echo "Expected hash not broken connection!"
exit 1
fi
if [[ "$HASH1" != "$HASH2" ]]; then
echo "Expected first and second hashes 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-cli 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 ""

Loading…
Cancel
Save