-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.go
More file actions
65 lines (50 loc) · 1.49 KB
/
parallel.go
File metadata and controls
65 lines (50 loc) · 1.49 KB
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
package main
import (
"sync"
)
// RunSuiteFunc describes particular test suite execution. Passed here to deleniate parallelism from suite execution logic
type RunSuiteFunc func(requestConfig *RequestConfig, rewriteConfig *RewriteConfig, suite TestSuite) []TestResult
// RunParallel starts parallel routines to execute test suites received from loader channel
func RunParallel(runConfig *RunConfig) {
resultConsumer := make(chan []TestResult)
var wg sync.WaitGroup
wg.Add(runConfig.numRoutines)
for i := 0; i < runConfig.numRoutines; i++ {
go runSuites(&SuiteConfig{
requestConfig: runConfig.requestConfig,
rewriteConfig: runConfig.rewriteConfig,
loader: runConfig.loader,
resultConsumer: resultConsumer,
waitGroup: &wg,
runner: runConfig.runSuite,
})
}
// Start a goroutine to close out once all the output goroutines are
// done. This must start after the wg.Add call.
go func() {
wg.Wait()
close(resultConsumer)
}()
for {
results, more := <-resultConsumer
runConfig.reporter.Report(results)
if !more {
break
}
}
runConfig.reporter.Flush()
}
type SuiteConfig struct {
requestConfig *RequestConfig
rewriteConfig *RewriteConfig
loader <-chan TestSuite
resultConsumer chan []TestResult
waitGroup *sync.WaitGroup
runner RunSuiteFunc
}
func runSuites(cfg *SuiteConfig) {
for suite := range cfg.loader {
cfg.resultConsumer <- runSuite(cfg.requestConfig, cfg.rewriteConfig, suite)
}
cfg.waitGroup.Done()
}