Skip to content

Commit 62cd989

Browse files
authored
Merge pull request #502 from rhansen/race
tests: fix / prevent race conditions in tests
2 parents a1cd2fa + ab9ed54 commit 62cd989

File tree

4 files changed

+15
-14
lines changed

4 files changed

+15
-14
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ jobs:
3838
run: make check-gofmt
3939

4040
- name: Run tests
41-
run: go test -v ./internal/...
41+
run: go test -race -v ./internal/...

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ check-gofmt:
5656
fi
5757

5858
test:
59-
go test ./internal/...
59+
go test -race ./internal/...

internal/generator/generator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ func (g *generator) generateFromEvents() {
190190
}
191191

192192
g.wg.Add(1)
193+
watcher := make(chan *docker.APIEvents, 100)
194+
watchers = append(watchers, watcher)
193195

194-
go func(cfg config.Config, watcher chan *docker.APIEvents) {
196+
go func(cfg config.Config) {
195197
defer g.wg.Done()
196-
watchers = append(watchers, watcher)
197-
198198
debouncedChan := newDebounceChannel(watcher, cfg.Wait)
199199
for range debouncedChan {
200200
containers, err := g.getContainers()
@@ -210,7 +210,7 @@ func (g *generator) generateFromEvents() {
210210
g.runNotifyCmd(cfg)
211211
g.sendSignalToContainer(cfg)
212212
}
213-
}(cfg, make(chan *docker.APIEvents, 100))
213+
}(cfg)
214214
}
215215

216216
// maintains docker client connection and passes events to watchers

internal/generator/generator_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"os"
1111
"strings"
12+
"sync/atomic"
1213
"testing"
1314
"time"
1415

@@ -22,7 +23,7 @@ import (
2223
func TestGenerateFromEvents(t *testing.T) {
2324
log.SetOutput(ioutil.Discard)
2425
containerID := "8dfafdbc3a40"
25-
counter := 0
26+
var counter atomic.Int32
2627

2728
eventsResponse := `
2829
{"status":"start","id":"8dfafdbc3a40","from":"base:latest","time":1374067924}
@@ -38,7 +39,7 @@ func TestGenerateFromEvents(t *testing.T) {
3839
for rsc.Scan() {
3940
w.Write([]byte(rsc.Text()))
4041
w.(http.Flusher).Flush()
41-
time.Sleep(15 * time.Millisecond)
42+
time.Sleep(150 * time.Millisecond)
4243
}
4344
time.Sleep(500 * time.Millisecond)
4445
}))
@@ -67,7 +68,7 @@ func TestGenerateFromEvents(t *testing.T) {
6768
json.NewEncoder(w).Encode(result)
6869
}))
6970
server.CustomHandler(fmt.Sprintf("/containers/%s/json", containerID), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
70-
counter++
71+
counter := counter.Add(1)
7172
container := docker.Container{
7273
Name: "docker-gen-test",
7374
ID: containerID,
@@ -165,13 +166,13 @@ func TestGenerateFromEvents(t *testing.T) {
165166
Template: tmplFile.Name(),
166167
Dest: destFiles[2].Name(),
167168
Watch: true,
168-
Wait: &config.Wait{Min: 20 * time.Millisecond, Max: 25 * time.Millisecond},
169+
Wait: &config.Wait{Min: 200 * time.Millisecond, Max: 250 * time.Millisecond},
169170
},
170171
{
171172
Template: tmplFile.Name(),
172173
Dest: destFiles[3].Name(),
173174
Watch: true,
174-
Wait: &config.Wait{Min: 25 * time.Millisecond, Max: 100 * time.Millisecond},
175+
Wait: &config.Wait{Min: 250 * time.Millisecond, Max: 1 * time.Second},
175176
},
176177
},
177178
},
@@ -188,12 +189,12 @@ func TestGenerateFromEvents(t *testing.T) {
188189

189190
// The counter is incremented in each output file in the following sequence:
190191
//
191-
// init 0ms 5ms 10ms 15ms 20ms 25ms 30ms 35ms 40ms 45ms 50ms 55ms
192+
// init 150ms 200ms 250ms 300ms 350ms 400ms 450ms 500ms 550ms 600ms 650ms 700ms
192193
// ├──────╫──────┼──────┼──────╫──────┼──────┼──────╫──────┼──────┼──────┼──────┼──────┤
193194
// File0 ├─ 1 ║ ║ ║
194195
// File1 ├─ 1 ╟─ 2 ╟─ 3 ╟─ 5
195-
// File2 ├─ 1 ╟───── max (25ms) ──║───────────> 4 ╟─────── min (20ms) ──────> 6
196-
// File3 └─ 1 ╟──────────────────> ╟──────────────────> ╟─────────── min (25ms) ─────────> 7
196+
// File2 ├─ 1 ╟───── max (250ms) ──║───────────> 4 ╟─────── min (200ms) ─────> 6
197+
// File3 └─ 1 ╟──────────────────> ╟──────────────────> ╟─────────── min (250ms) ────────> 7
197198
// ┌───╨───┐ ┌───╨──┐ ┌───╨───┐
198199
// │ start │ │ stop │ │ start │
199200
// └───────┘ └──────┘ └───────┘

0 commit comments

Comments
 (0)