OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / abci / example / block_aware / block_aware_test.go
1 package main
2
3 import (
4         "strconv"
5         "strings"
6         "testing"
7
8         abcicli "github.com/tendermint/abci/client"
9         "github.com/tendermint/abci/server"
10         "github.com/tendermint/abci/types"
11         "github.com/tendermint/tmlibs/log"
12 )
13
14 func TestChainAware(t *testing.T) {
15         app := NewChainAwareApplication()
16
17         // Start the listener
18         srv, err := server.NewServer("unix://test.sock", "socket", app)
19         if err != nil {
20                 t.Fatal(err)
21         }
22         srv.SetLogger(log.TestingLogger().With("module", "abci-server"))
23         if _, err := srv.Start(); err != nil {
24                 t.Fatal(err.Error())
25         }
26         defer srv.Stop()
27
28         // Connect to the socket
29         client := abcicli.NewSocketClient("unix://test.sock", false)
30         client.SetLogger(log.TestingLogger().With("module", "abci-client"))
31         if _, err := client.Start(); err != nil {
32                 t.Fatalf("Error starting socket client: %v", err.Error())
33         }
34         defer client.Stop()
35
36         n := uint64(5)
37         hash := []byte("fake block hash")
38         header := &types.Header{}
39         for i := uint64(0); i < n; i++ {
40                 client.BeginBlockSync(types.RequestBeginBlock{hash, header})
41                 client.EndBlockSync(i)
42                 client.CommitSync()
43         }
44
45         r := app.Query(types.RequestQuery{})
46         spl := strings.Split(string(r.Value), ",")
47         if len(spl) != 2 {
48                 t.Fatal("expected %d,%d ; got %s", n, n, string(r.Value))
49         }
50         beginCount, _ := strconv.Atoi(spl[0])
51         endCount, _ := strconv.Atoi(spl[1])
52         if uint64(beginCount) != n {
53                 t.Fatalf("expected beginCount of %d, got %d", n, beginCount)
54         } else if uint64(endCount) != n {
55                 t.Fatalf("expected endCount of %d, got %d", n, endCount)
56         }
57 }