-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.go
222 lines (193 loc) · 5.69 KB
/
testing.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package bbrpc
import (
"fmt"
"reflect"
"runtime/debug"
"strconv"
"strings"
"testing"
"time"
)
func tShouldNil(t *testing.T, v interface{}, args ...interface{}) {
if v != nil {
debug.PrintStack()
t.Fatalf("[test assert] should nil, but got: %v, %v", v, args)
}
}
func tShouldTrue(t *testing.T, b bool, args ...interface{}) {
if !b {
debug.PrintStack()
t.Fatalf("[test assert] should true, args: %v", args)
}
}
func tShouldNotZero(t *testing.T, v interface{}, args ...interface{}) {
if reflect.ValueOf(v).IsZero() {
debug.PrintStack()
t.Fatalf("[test assert] should not [zero value], %v", args)
}
}
func tShouldNotContains(t *testing.T, v, containV string) {
if strings.Contains(v, containV) {
debug.PrintStack()
t.Fatalf("[test assert] [%s] should not contains [%s]", v, containV)
}
}
// ClusterNode .
type ClusterNode struct {
IsMiner bool
MinerAddress string
Client *Client
RPCPort int
}
//TesttoolRunClusterWith2nodes 运行2个节点,返回的第一个节点为矿工节点,发生错误则终止测试,矿工节点的日志会打印出来
func TesttoolRunClusterWith2nodes(t *testing.T) (func(), []ClusterNode) {
killMiner, minerClient, minerAddress := TesttoolRunServerAndBeginMint(t)
runBBOptions := DefaultDebugBBArgs()
runBBOptions["port"] = Pstring(strconv.Itoa(TDefaultPort2))
runBBOptions["rpcport"] = Pstring(strconv.Itoa(TDefaultRPCPort2))
ipHost := "127.0.0.1:" + strconv.Itoa(TDefaultPort)
runBBOptions["addnode"] = &ipHost
killPeer, err := RunBigBangServer(&RunBigBangOptions{
NewTmpDir: true,
Args: runBBOptions,
TmpDirTag: "peer",
NotPrint2stdout: true,
})
tShouldNil(t, err)
conn := DefaultDebugConnConfig()
conn.Host = "127.0.0.1:" + strconv.Itoa(TDefaultRPCPort2)
peerClient, err := NewClient(conn)
tShouldNil(t, err, "failed to new rpc client")
time.Sleep(time.Second)
{ //验证2个节点确实组成了网络
peers, err := minerClient.Listpeer()
tShouldNil(t, err)
tShouldTrue(t, len(peers) == 1)
peers, err = peerClient.Listpeer()
tShouldNil(t, err)
tShouldTrue(t, len(peers) == 1)
}
return func() {
killMiner()
killPeer()
minerClient.Shutdown()
peerClient.Shutdown()
},
[]ClusterNode{
{IsMiner: true, MinerAddress: minerAddress, Client: minerClient, RPCPort: TDefaultRPCPort},
{Client: peerClient, RPCPort: TDefaultRPCPort2},
}
}
//TesttoolRunServerAndBeginMint (测试中)启动bigbang,使用预置的私钥开始挖矿, opts 作为可选参数只使用第一个值(如果有)
// 返回:killBigbang(), Client, 挖矿模版地址
func TesttoolRunServerAndBeginMint(t *testing.T, opts ...RunBigBangOptions) (func(), *Client, string) {
runBBOptions := DefaultDebugBBArgs()
runBBOptions["cryptonightaddress"] = &TCryptonightAddr.Address
runBBOptions["cryptonightkey"] = &TCryptonightKey.Privkey
opt := RunBigBangOptions{
NewTmpDir: true,
Args: runBBOptions,
}
if len(opts) > 0 {
opt = opts[0]
if len(opt.Args) == 0 {
opt.Args = runBBOptions
} else {
for k, v := range runBBOptions { //补充没有的参数
if _, ok := opt.Args[k]; !ok {
opt.Args[k] = v
}
}
}
}
if !testing.Verbose() {
opt.NotPrint2stdout = true
}
killBigBangServer, err := RunBigBangServer(&opt)
tShouldNil(t, err, "failed to run bigbang server")
client, err := NewClient(DefaultDebugConnConfig())
tShouldNil(t, err, "failed to new rpc client")
{
_, _ = client.Importprivkey(TCryptonightAddr.Privkey, _tPassphrase, nil)
_, _ = client.Importprivkey(TCryptonightKey.Privkey, _tPassphrase, nil) //这个无需导入,配置已有,导入反而报错
_, _ = client.Unlockkey(TCryptonightAddr.Pubkey, _tPassphrase, nil)
_, _ = client.Unlockkey(TCryptonightKey.Pubkey, _tPassphrase, nil)
}
//开始挖矿
templateAddress, err := client.Addnewtemplate(AddnewtemplateParamMint{
Mint: TCryptonightKey.Pubkey,
Spent: TCryptonightAddr.Address,
})
tShouldNil(t, err)
return func() {
killBigBangServer()
client.Shutdown()
}, client, *templateAddress
}
// 启动bigbang-server,创建一个client,调用testFn(client)
func testClientMethod(t *testing.T, testFn func(*Client)) {
// killBigBangServer, client, _ := TesttoolRunServerAndBeginMint(t)
// defer killBigBangServer()
// testFn(client)
info := MustDockerRunDevCore(t, "bbccore:0.4")
testFn(info.Client)
}
// 启动bigbang-server,创建一个client,调用testFn(client)
func runClientTest(t *testing.T, testFn func(*Client, string)) {
killBigBangServer, client, minerAddr := TesttoolRunServerAndBeginMint(t)
defer killBigBangServer()
testFn(client, minerAddr)
}
// Wait4nBlocks 每次休眠1s,直到出了n个块
func Wait4nBlocks(n int, client *Client) error {
count, err := client.Getblockcount(nil)
if err != nil {
return err
}
fmt.Printf("等待 %d 个块 ", n)
prevDiff := 0
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
<-ticker.C
currentCount, err := client.Getblockcount(nil)
if err != nil {
return err
}
diff := *currentCount - *count
if diff >= n {
fmt.Printf("[达到(%d)]\n", diff)
return nil
}
fmt.Print(".")
if prevDiff != diff {
fmt.Print(diff)
prevDiff = diff
}
}
}
// Wait4balanceReach 每次休眠1s,等待地址的余额达到
func Wait4balanceReach(addr string, balance float64, client *Client, msg ...string) error {
fmt.Printf("%v等待地址 %s 余额达到%v ", msg, addr, balance)
prevBal := 0.0
for {
bal, err := client.Getbalance(nil, &addr)
if err != nil {
return err
}
f := 0.0
if len(bal) > 0 {
f = bal[0].Avail - bal[0].Unconfirmed
}
fmt.Printf(".")
if f != prevBal {
prevBal = f
fmt.Printf("%v", f)
}
if f >= balance {
fmt.Printf("[达到]\n")
return nil
}
time.Sleep(time.Second)
}
}