-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-suite.go
130 lines (111 loc) · 2.5 KB
/
multi-suite.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
package arp
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
type MultiTestSuite struct {
Suites map[string]*TestSuite
Verbose bool
}
type MultiSuiteResult struct {
Passed bool
Error error
TestResults SuiteResult
TestFile string
}
type MultiSuiteWorker struct {
TestTags []string
Suite *TestSuite
TestFile string
}
func NewMultiSuiteTest(testDir string, fixtures string) (*MultiTestSuite, error) {
multiSuite := &MultiTestSuite{
Suites: map[string]*TestSuite{},
Verbose: true,
}
err := multiSuite.LoadTests(testDir, fixtures)
return multiSuite, err
}
func (t *MultiTestSuite) LoadTests(testDir string, fixtures string) error {
err := filepath.Walk(testDir, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".yaml") {
suite, err := NewTestSuite(path, fixtures)
if err != nil {
return err
}
if len(suite.Tests) == 0 {
return nil
}
if suite != nil {
t.Suites[path] = suite
}
return nil
}
return nil
})
return err
}
func (t *MultiTestSuite) ExecuteTests(threads int, testTags []string) (bool, []MultiSuiteResult, time.Duration, error) {
startTime := time.Now()
if t.Verbose {
fmt.Printf("Executing tests across %v threads...\n\n", threads)
}
var results []MultiSuiteResult
aggregateStatus := true
wg := sync.WaitGroup{}
testCount := len(t.Suites)
workerResults := make(chan MultiSuiteResult, threads)
workerMessages := make(chan MultiSuiteWorker, testCount)
wg.Add(threads)
for i := 0; i < threads; i++ {
go func() {
for {
m, ok := <-workerMessages
if !ok {
wg.Done()
return
}
if t.Verbose {
fmt.Printf("> In Progress: %v\n", m.TestFile)
}
status, result, err := m.Suite.ExecuteTests(m.TestTags)
r := MultiSuiteResult{
Passed: status,
Error: err,
TestFile: m.TestFile,
TestResults: result,
}
workerResults <- r
}
}()
}
for k := range t.Suites {
msg := MultiSuiteWorker{
TestTags: testTags,
Suite: t.Suites[k],
TestFile: k,
}
workerMessages <- msg
}
close(workerMessages)
defer close(workerResults)
for i := 0; i < testCount; i++ {
d := <-workerResults
results = append(results, d)
aggregateStatus = aggregateStatus && d.Passed
if t.Verbose {
statusStr := "Pass"
if !d.Passed {
statusStr = "Fail"
}
fmt.Printf("< Done: [%v] %v\n", statusStr, d.TestFile)
}
}
wg.Wait()
duration := time.Since(startTime)
return aggregateStatus, results, duration, nil
}