-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
164 lines (143 loc) · 3.26 KB
/
main.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"fmt"
"math/rand"
"strconv"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
type jobInfo struct {
ID int
Status bool
}
type Counter struct {
C int
Done bool
}
var (
builders = 10000000
tasks = 1000000
testers = 10000000
)
func main() {
var wg sync.WaitGroup
var buildsCh = make(chan jobInfo)
var testersCh = make(chan jobInfo)
var deployCh = make(chan jobInfo)
var buildDone, testDone bool
var buildCounter, buildPassCounter, testCounter, testPassCounter, deployCounter int
var m sync.Mutex
for i := 0; i < builders; i++ {
go buildRunner(&buildsCh, &testersCh, &buildDone, &buildCounter, &buildPassCounter, &m)
}
for i := 0; i < testers; i++ {
go testRunner(&testersCh, &deployCh, &testDone, &testCounter, &testPassCounter, &m)
}
wg.Add(1)
go deployRunner(&deployCh, &wg, &deployCounter, &m)
for i := 0; i < tasks; i++ {
buildsCh <- jobInfo{ID: i, Status: false}
}
go func() {
wg.Add(1)
for {
if buildCounter == tasks {
close(buildsCh)
break
} else {
time.Sleep(time.Millisecond * 1)
}
}
wg.Done()
}()
go func() {
wg.Add(1)
for {
if buildDone {
if buildPassCounter == testCounter {
close(testersCh)
break
} else {
time.Sleep(time.Millisecond * 1)
}
} else {
time.Sleep(time.Millisecond * 1)
}
}
wg.Done()
}()
go func() {
wg.Add(1)
for {
if testDone {
if testPassCounter == deployCounter {
close(deployCh)
break
} else {
time.Sleep(time.Millisecond * 1)
}
} else {
time.Sleep(time.Millisecond * 1)
}
}
wg.Done()
}()
wg.Wait()
}
func buildRunner(buildChan, testerCh *chan jobInfo, buildDone *bool, buildCounter, buildPassCounter *int, m *sync.Mutex) {
for i := range *buildChan {
fmt.Println("...Running Build for job #" + strconv.Itoa(i.ID) + " ...")
time.Sleep(time.Second * 1)
status := rand.Float64() * 1
if status < 0.5 {
log.Info("Job #" + strconv.Itoa(i.ID) + ": BUILD FAILED!")
} else {
log.Info("Job #" + strconv.Itoa(i.ID) + ": BUILD PASSED!")
*testerCh <- jobInfo{ID: i.ID, Status: true}
m.Lock()
*buildPassCounter++
m.Unlock()
}
m.Lock()
*buildCounter++
m.Unlock()
}
*buildDone = true
}
func testRunner(testerChan, deployCh *chan jobInfo, testDone *bool, testCounter, testPassCounter *int, m *sync.Mutex) {
for i := range *testerChan {
fmt.Println("...Running Tests for job #" + strconv.Itoa(i.ID) + " ...")
time.Sleep(time.Second * 1)
status := rand.Float64() * 1
if status < 0.5 {
log.Info("Job #" + strconv.Itoa(i.ID) + ": TESTS FAILED!")
} else {
log.Info("Job #" + strconv.Itoa(i.ID) + ": TESTS PASSED!")
*deployCh <- jobInfo{ID: i.ID, Status: true}
m.Lock()
*testPassCounter++
m.Unlock()
}
m.Lock()
*testCounter++
m.Unlock()
}
*testDone = true
}
func deployRunner(deployChan *chan jobInfo, wg *sync.WaitGroup, deployCounter *int, m *sync.Mutex) {
for i := range *deployChan {
fmt.Println("...Running Deploy for job #..." + strconv.Itoa(i.ID))
time.Sleep(time.Millisecond * 1)
status := rand.Float64() * 1
if status < 0.5 {
log.Info("Job #" + strconv.Itoa(i.ID) + ": DEPLOY FAILED!")
} else {
log.Info("Job #" + strconv.Itoa(i.ID) + ": DEPLOY PASSED!")
}
m.Lock()
*deployCounter++
m.Unlock()
}
wg.Done()
}