mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-06 16:17:11 +00:00
Added counter test written in Golang
This commit is contained in:
+3
-12
@@ -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
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
@@ -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 ""
|
||||
@@ -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 ""
|
||||
Reference in New Issue
Block a user