tendermint/tests/test_counter.go

127 lines
3.2 KiB
Go
Raw Normal View History

2016-02-14 14:59:53 -08:00
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
}
2016-02-21 23:55:45 -08:00
func startClient() *tmspcli.Client {
2016-02-14 14:59:53 -08:00
// Start client
2016-02-21 23:55:45 -08:00
client, err := tmspcli.NewClient("tcp://127.0.0.1:46658")
2016-02-14 14:59:53 -08:00
if err != nil {
panic("connecting to counter_app: " + err.Error())
}
return client
}
2016-02-21 23:55:45 -08:00
func setOption(client *tmspcli.Client, key, value string) {
2016-02-14 14:59:53 -08:00
log, err := client.SetOptionSync(key, value)
if err != nil {
panic(Fmt("setting %v=%v: %v\nlog: %v", key, value, err, log))
}
}
2016-02-21 23:55:45 -08:00
func commit(client *tmspcli.Client, hashExp []byte) {
2016-02-14 14:59:53 -08:00
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))
}
}
2016-02-21 23:55:45 -08:00
func appendTx(client *tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
2016-02-14 14:59:53 -08:00
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))
}
}
2016-02-21 23:55:45 -08:00
func checkTx(client *tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
2016-02-14 14:59:53 -08:00
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))
}
}