-
Notifications
You must be signed in to change notification settings - Fork 375
/
test_test.go
98 lines (84 loc) · 1.93 KB
/
test_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
package test
import (
"encoding/json"
"fmt"
"log"
"math/rand"
"runtime"
"testing"
"time"
"berty.tech/core/network/mock"
)
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
//
// json print helpers
//
func jsonPrint(i interface{}) {
out, _ := json.Marshal(i)
fmt.Println(string(out))
}
func jsonPrintIndent(i interface{}) {
out, _ := json.MarshalIndent(i, "", " ")
fmt.Println(string(out))
}
//
// duration / bench helpers
//
var durations map[string][]time.Time
func printDuration(key string) {
if durations == nil {
durations = make(map[string][]time.Time)
}
_, fn, line, _ := runtime.Caller(1)
now := time.Now()
if _, found := durations[key]; found {
log.Printf("\n[duration] %s: %s:%d diff=%s total=%s\n", key, fn, line, now.Sub(durations[key][1]), now.Sub(durations[key][0]))
durations[key][1] = now
} else {
log.Printf("\n[duration] %s: %s:%d init\n", key, fn, line)
durations[key] = []time.Time{now, now}
}
}
//
// appmock helpers
//
func nodeChansLens(apps ...*AppMock) []int {
time.Sleep(10 * time.Millisecond) // we let a few time to the queue to be filled
out := []int{}
for _, app := range apps {
app.node.AsyncWait()
out = append(out, len(app.networkDriver.(*mock.Enqueuer).Queue()))
out = append(out, len(app.eventStream))
if len(app.eventStream) > 99 {
log.Println("!! QUEUE SHOULD NOT BE AS MUCH FILLED !!")
log.Println("--- queue")
for len(app.networkDriver.(*mock.Enqueuer).Queue()) > 0 {
event := <-app.networkDriver.(*mock.Enqueuer).Queue()
jsonPrint(event)
}
log.Println("--- eventStream")
for len(app.eventStream) > 0 {
event := <-app.eventStream
jsonPrint(event)
}
}
}
return out
}
//
// fail fast
//
// see e2e_test.go for examples
//
var lastTestSucceed = true
func shouldIContinue(t *testing.T) {
if !lastTestSucceed {
t.Skip("a previous test failed, stopping here")
}
lastTestSucceed = false
}
func everythingWentFine() {
lastTestSucceed = true
}