OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / tests / test_app / app.go
1 package main
2
3 import (
4         "bytes"
5         "fmt"
6         "os"
7         "time"
8
9         abcicli "github.com/tendermint/abci/client"
10         "github.com/tendermint/abci/types"
11         "github.com/tendermint/tmlibs/log"
12         "github.com/tendermint/tmlibs/process"
13 )
14
15 func startApp(abciApp string) *process.Process {
16         // Start the app
17         //outBuf := NewBufferCloser(nil)
18         proc, err := process.StartProcess("abci_app",
19                 "",
20                 "bash",
21                 []string{"-c", abciApp},
22                 nil,
23                 os.Stdout,
24         )
25         if err != nil {
26                 panic("running abci_app: " + err.Error())
27         }
28
29         // TODO a better way to handle this?
30         time.Sleep(time.Second)
31
32         return proc
33 }
34
35 func startClient(abciType string) abcicli.Client {
36         // Start client
37         client, err := abcicli.NewClient("tcp://127.0.0.1:46658", abciType, true)
38         if err != nil {
39                 panic(err.Error())
40         }
41         logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
42         client.SetLogger(logger.With("module", "abcicli"))
43         if _, err := client.Start(); err != nil {
44                 panic("connecting to abci_app: " + err.Error())
45         }
46
47         return client
48 }
49
50 func setOption(client abcicli.Client, key, value string) {
51         res := client.SetOptionSync(key, value)
52         _, _, log := res.Code, res.Data, res.Log
53         if res.IsErr() {
54                 panic(fmt.Sprintf("setting %v=%v: \nlog: %v", key, value, log))
55         }
56 }
57
58 func commit(client abcicli.Client, hashExp []byte) {
59         res := client.CommitSync()
60         _, data, _ := res.Code, res.Data, res.Log
61         if res.IsErr() {
62                 panic(fmt.Sprintf("committing err %v\n", res))
63         }
64         if !bytes.Equal(res.Data, hashExp) {
65                 panic(fmt.Sprintf("Commit hash was unexpected. Got %X expected %X",
66                         data, hashExp))
67         }
68 }
69
70 func deliverTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
71         res := client.DeliverTxSync(txBytes)
72         code, data, log := res.Code, res.Data, res.Log
73         if code != codeExp {
74                 panic(fmt.Sprintf("DeliverTx response code was unexpected. Got %v expected %v. Log: %v",
75                         code, codeExp, log))
76         }
77         if !bytes.Equal(data, dataExp) {
78                 panic(fmt.Sprintf("DeliverTx response data was unexpected. Got %X expected %X",
79                         data, dataExp))
80         }
81 }
82
83 /*func checkTx(client abcicli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
84         res := client.CheckTxSync(txBytes)
85         code, data, log := res.Code, res.Data, res.Log
86         if res.IsErr() {
87                 panic(fmt.Sprintf("checking tx %X: %v\nlog: %v", txBytes, log))
88         }
89         if code != codeExp {
90                 panic(fmt.Sprintf("CheckTx response code was unexpected. Got %v expected %v. Log: %v",
91                         code, codeExp, log))
92         }
93         if !bytes.Equal(data, dataExp) {
94                 panic(fmt.Sprintf("CheckTx response data was unexpected. Got %X expected %X",
95                         data, dataExp))
96         }
97 }*/