@@ -18,10 +18,13 @@ package main
1818
1919import (
2020 "database/sql"
21+ "os/exec"
2122 "path/filepath"
23+ "sync"
2224 "testing"
2325 "time"
2426
27+ "context"
2528 . "github.com/smartystreets/goconvey/convey"
2629 "gitlab.com/thunderdb/ThunderDB/client"
2730 "gitlab.com/thunderdb/ThunderDB/utils"
@@ -34,57 +37,116 @@ var (
3437 logDir = FJ (testWorkingDir , "./log/" )
3538)
3639
40+ var nodeCmds []* exec.Cmd
41+
3742var FJ = filepath .Join
3843
39- func _TestBuild (t * testing.T ) {
44+ func TestBuild (t * testing.T ) {
4045 log .SetLevel (log .DebugLevel )
4146 utils .Build ()
4247}
4348
4449func startNodes () {
50+ // wait for ports to be available
51+ var err error
52+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 15 )
53+ defer cancel ()
54+ err = utils .WaitForPorts (ctx , "127.0.0.1" , []int {
55+ 2122 ,
56+ 2121 ,
57+ 2120 ,
58+ 2144 ,
59+ 2145 ,
60+ 2146 ,
61+ }, time .Millisecond * 200 )
62+
63+ if err != nil {
64+ log .Fatalf ("wait for port ready timeout: %v" , err )
65+ }
66+
4567 // start 3bps
46- go utils .RunCommand (
68+ var cmd * exec.Cmd
69+ if cmd , err = utils .RunCommandNB (
4770 FJ (baseDir , "./bin/thunderdbd" ),
4871 []string {"-config" , FJ (testWorkingDir , "./node_0/config.yaml" )},
4972 "leader" , testWorkingDir , logDir , false ,
50- )
51- go utils .RunCommand (
73+ ); err == nil {
74+ nodeCmds = append (nodeCmds , cmd )
75+ } else {
76+ log .Errorf ("start node failed: %v" , err )
77+ }
78+ if cmd , err = utils .RunCommandNB (
5279 FJ (baseDir , "./bin/thunderdbd" ),
5380 []string {"-config" , FJ (testWorkingDir , "./node_1/config.yaml" )},
5481 "follower1" , testWorkingDir , logDir , false ,
55- )
56- go utils .RunCommand (
82+ ); err == nil {
83+ nodeCmds = append (nodeCmds , cmd )
84+ } else {
85+ log .Errorf ("start node failed: %v" , err )
86+ }
87+ if cmd , err = utils .RunCommandNB (
5788 FJ (baseDir , "./bin/thunderdbd" ),
5889 []string {"-config" , FJ (testWorkingDir , "./node_2/config.yaml" )},
5990 "follower2" , testWorkingDir , logDir , false ,
60- )
91+ ); err == nil {
92+ nodeCmds = append (nodeCmds , cmd )
93+ } else {
94+ log .Errorf ("start node failed: %v" , err )
95+ }
6196
6297 time .Sleep (time .Second * 3 )
6398
6499 // start 3miners
65- go utils .RunCommand (
100+ if cmd , err = utils .RunCommandNB (
66101 FJ (baseDir , "./bin/thunderminerd" ),
67102 []string {"-config" , FJ (testWorkingDir , "./node_miner_0/config.yaml" )},
68- "miner1" , testWorkingDir , logDir , false ,
69- )
70- go utils .RunCommand (
103+ "miner0" , testWorkingDir , logDir , false ,
104+ ); err == nil {
105+ nodeCmds = append (nodeCmds , cmd )
106+ } else {
107+ log .Errorf ("start node failed: %v" , err )
108+ }
109+ if cmd , err = utils .RunCommandNB (
71110 FJ (baseDir , "./bin/thunderminerd" ),
72111 []string {"-config" , FJ (testWorkingDir , "./node_miner_1/config.yaml" )},
73- "miner2" , testWorkingDir , logDir , false ,
74- )
75- go utils .RunCommand (
112+ "miner1" , testWorkingDir , logDir , false ,
113+ ); err == nil {
114+ nodeCmds = append (nodeCmds , cmd )
115+ } else {
116+ log .Errorf ("start node failed: %v" , err )
117+ }
118+ if cmd , err = utils .RunCommandNB (
76119 FJ (baseDir , "./bin/thunderminerd" ),
77120 []string {"-config" , FJ (testWorkingDir , "./node_miner_2/config.yaml" )},
78- "miner3" , testWorkingDir , logDir , false ,
79- )
121+ "miner2" , testWorkingDir , logDir , false ,
122+ ); err == nil {
123+ nodeCmds = append (nodeCmds , cmd )
124+ } else {
125+ log .Errorf ("start node failed: %v" , err )
126+ }
127+ }
128+
129+ func stopNodes () {
130+ var wg sync.WaitGroup
131+
132+ for _ , nodeCmd := range nodeCmds {
133+ wg .Add (1 )
134+ go func (thisCmd * exec.Cmd ) {
135+ defer wg .Done ()
136+ thisCmd .Process .Kill ()
137+ thisCmd .Wait ()
138+ }(nodeCmd )
139+ }
80140
141+ wg .Wait ()
81142}
82143
83- func _TestFullProcess (t * testing.T ) {
144+ func TestFullProcess (t * testing.T ) {
84145 log .SetLevel (log .DebugLevel )
85146
86147 Convey ("test full process" , t , func () {
87148 startNodes ()
149+ defer stopNodes ()
88150 time .Sleep (5 * time .Second )
89151
90152 var err error
0 commit comments