/
test.go
114 lines (100 loc) · 2.52 KB
/
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
package vdetesting
import (
"errors"
"log"
"net"
"sync"
)
//TestServer is the server side part of a test
//it should receive data and log all the statistic we need
type TestServer interface {
StartServer()
AddStat(s Stat)
statManager()
IFace() *net.Interface
Name() string
Address() net.Addr
}
//Test is a generic test it need a client method and a server method
// and it test one single aspect and save the results to a single logfile
type Test interface {
StartClient()
TestServer
}
//Stat let you gather statistic regarding any kind of test
type Stat interface {
Start()
Stop()
SetWaitGroup(wg *sync.WaitGroup) error
}
//TestRunner manage the sequence of tests to run and start and stop them
type TestRunner struct {
tests []Test
}
//NewTestRunner return a new shiny test runner
func NewTestRunner() TestRunner {
runner := TestRunner{tests: make([]Test, 0, 20)}
return runner
}
//AddTest add a test to the quee
func (runner *TestRunner) AddTest(test Test) {
runner.tests = append(runner.tests, test)
}
//StartServer start in sequence all the quequed server side Tests
func (runner *TestRunner) StartServer() {
for _, test := range runner.tests {
test.StartServer()
}
}
//StartClient start in sequence all the quequed server side Tests
func (runner *TestRunner) StartClient() {
for _, test := range runner.tests {
test.StartClient()
}
}
//StatManager is a struct that should be added to everytest
//it manage all the Stats inside them
type StatManager struct {
stats []Stat
wg *sync.WaitGroup
started bool
}
//NewStatManager Create a NewStatManager, should be used inside tests
func NewStatManager() StatManager {
var wg sync.WaitGroup
manager := StatManager{stats: make([]Stat, 0, 20),
wg: &wg, started: false}
return manager
}
//Add new statistic fetcher to the manager
func (manager *StatManager) Add(s Stat) error {
if manager.started {
err := errors.New("Statistics already Started can't add new ones")
return err
}
s.SetWaitGroup(manager.wg)
manager.stats = append(manager.stats, s)
return nil
}
//Start start all the statistics
func (manager *StatManager) Start() error {
if manager.started {
err := errors.New("Statistics already Started")
return err
}
for _, stat := range manager.stats {
stat.Start()
}
manager.started = true
return nil
}
//Stop stop all the statistics and wait for them to finish
func (manager *StatManager) Stop() error {
for _, stat := range manager.stats {
stat.Stop()
}
log.Print("waiting for stats to stop")
manager.wg.Wait()
manager.started = false
return nil
}