/
app_test.go
151 lines (122 loc) · 3.48 KB
/
app_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package app
import (
"fmt"
"github.com/spacemeshos/go-spacemesh/address"
"github.com/spacemeshos/go-spacemesh/api/config"
"github.com/spacemeshos/go-spacemesh/hare"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/mesh"
"github.com/spacemeshos/go-spacemesh/miner"
"github.com/spacemeshos/go-spacemesh/oracle"
"github.com/spacemeshos/go-spacemesh/p2p/service"
"github.com/stretchr/testify/suite"
"math/big"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/spacemeshos/go-spacemesh/filesystem"
)
type AppTestSuite struct {
suite.Suite
apps []*SpacemeshApp
dbs []string
}
func (app *AppTestSuite) SetupTest() {
app.apps = make([]*SpacemeshApp, 0, 0)
app.dbs = make([]string, 0, 0)
}
func (app *AppTestSuite) TearDownTest() {
for _, ap := range app.apps {
ap.stopServices()
}
for _, dbinst := range app.dbs {
err := os.RemoveAll(dbinst)
if err != nil {
panic(fmt.Sprintf("what happened : %v", err))
}
}
}
func TestApp(t *testing.T) {
t.Skip()
filesystem.SetupTestSpacemeshDataFolders(t, "app_test")
// remove all injected test flags for now
os.Args = []string{"/go-spacemesh", "--json-server=true"}
go Main()
<-EntryPointCreated
assert.NotNil(t, App)
<-App.NodeInitCallback
assert.NotNil(t, App.P2P)
assert.NotNil(t, App)
assert.Equal(t, App.Config.API.StartJSONServer, true)
// app should exit based on this signal
Cancel()
filesystem.DeleteSpacemeshDataFolders(t)
}
func (app *AppTestSuite) initMultipleInstances(t *testing.T, numOfInstances int, storeFormat string) {
net := service.NewSimulator()
runningName := 'a'
bo := oracle.NewLocalOracle(numOfInstances)
for i := 0; i < numOfInstances; i++ {
app.apps = append(app.apps, newSpacemeshApp())
store := storeFormat + string(runningName)
n := net.NewNode()
sgn := hare.NewMockSigning() //todo: shouldn't be any mock code here
pub := sgn.Verifier()
bo.Register(true, pub.String())
err := app.apps[i].initServices(pub.String(), n, store, sgn, bo, bo, uint32(numOfInstances))
assert.NoError(t, err)
app.apps[i].setupGenesis(config.DefaultGenesisConfig())
app.dbs = append(app.dbs, store)
runningName++
}
}
func (app *AppTestSuite) TestMultipleNodes() {
//EntryPointCreated <- true
addr := address.BytesToAddress([]byte{0x01})
dst := address.BytesToAddress([]byte{0x02})
tx := mesh.SerializableTransaction{}
tx.Amount = big.NewInt(10).Bytes()
tx.GasLimit = 1
tx.Origin = addr
tx.Recipient = &dst
tx.Price = big.NewInt(1).Bytes()
txbytes, _ := mesh.TransactionAsBytes(&tx)
path := "../tmp/test/state_" + time.Now().String()
app.initMultipleInstances(app.T(), 10, path)
for _, a := range app.apps {
a.startServices()
}
app.apps[0].P2P.Broadcast(miner.IncomingTxProtocol, txbytes)
timeout := time.After(2 * 60 * time.Second)
for {
select {
// Got a timeout! fail with a timeout error
case <-timeout:
app.T().Fatal("timed out ")
default:
for idx, ap := range app.apps {
if big.NewInt(10).Cmp(ap.state.GetBalance(dst)) == 0 {
ok := 0
for idx2, ap2 := range app.apps {
if idx != idx2 {
r1 := ap.state.IntermediateRoot(false).String()
r2 := ap2.state.IntermediateRoot(false).String()
if r1 == r2 {
log.Info("%d roots confirmed out of %d", ok, len(app.apps))
ok++
}
}
if ok == len(app.apps)-1 {
return
}
}
}
}
time.Sleep(1 * time.Millisecond)
}
}
}
func TestAppTestSuite(t *testing.T) {
suite.Run(t, new(AppTestSuite))
}