Skip to content

Commit 7e8c072

Browse files
authored
Fix test race condition. (#58)
Successfully passed with `go test ./... -race -count=100`
1 parent 430f129 commit 7e8c072

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

caller.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ var (
3838
Value: "scamper",
3939
}
4040

41+
// Variables to aid in testing of main()
4142
ctx, cancel = context.WithCancel(context.Background())
43+
logFatal = log.Fatal
4244
)
4345

4446
func init() {
@@ -56,6 +58,7 @@ func main() {
5658
rtx.Must(os.MkdirAll(*outputPath, 0557), "Could not create data directory")
5759

5860
defer cancel()
61+
wg := sync.WaitGroup{}
5962

6063
promSrv := prometheusx.MustServeMetrics()
6164
defer promSrv.Shutdown(ctx)
@@ -71,9 +74,11 @@ func main() {
7174
ControlSocket: *scamperCtrlSocket,
7275
ScamperTimeout: *scamperTimeout,
7376
}
77+
wg.Add(1)
7478
go func() {
7579
daemon.MustStart(ctx)
7680
cancel()
81+
wg.Done()
7782
}()
7883
trace = daemon
7984
case "paris-traceroute":
@@ -84,7 +89,6 @@ func main() {
8489
}
8590
}
8691

87-
wg := sync.WaitGroup{}
8892
cache := ipcache.New(ctx, trace, *ipcache.IPCacheTimeout, *ipcache.IPCacheUpdatePeriod)
8993

9094
if *poll {
@@ -101,8 +105,7 @@ func main() {
101105
}
102106
wg.Done()
103107
}(cache)
104-
}
105-
if *eventsocket.Filename != "" {
108+
} else if *eventsocket.Filename != "" {
106109
wg.Add(1)
107110
go func() {
108111
connCreator, err := connection.NewCreator()
@@ -111,6 +114,8 @@ func main() {
111114
eventsocket.MustRun(ctx, *eventsocket.Filename, connListener)
112115
wg.Done()
113116
}()
117+
} else {
118+
logFatal("--poll was false but --tcpinfo.eventsocket was set to \"\". This is a nonsensical configuration.")
114119
}
115120
wg.Wait()
116121
}

caller_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package main
33
import (
44
"context"
55
"io/ioutil"
6+
"log"
7+
"os"
68
"testing"
79
"time"
810

@@ -21,13 +23,15 @@ func TestMetrics(t *testing.T) {
2123
func TestMain(t *testing.T) {
2224
dir, err := ioutil.TempDir("", "TestMain")
2325
rtx.Must(err, "Could not create temp dir")
26+
defer os.RemoveAll(dir)
2427

2528
// Verify that main doesn't crash, and that it does exit when the context is canceled.
2629
// TODO: verify more in this test.
2730
*prometheusx.ListenAddress = ":0"
2831
*scamperCtrlSocket = dir + "/scamper.sock"
2932
*waitTime = time.Nanosecond // Run through the loop a few times.
3033
*outputPath = dir
34+
*poll = true
3135
tracerType.Value = "scamper"
3236
ctx, cancel = context.WithCancel(context.Background())
3337
go func() {
@@ -40,13 +44,15 @@ func TestMain(t *testing.T) {
4044
func TestMainWithConnectionListener(t *testing.T) {
4145
dir, err := ioutil.TempDir("", "TestMainWithConnectionListener")
4246
rtx.Must(err, "Could not create temp dir")
47+
defer os.RemoveAll(dir)
4348
srv := eventsocket.New(dir + "/events.sock")
4449
rtx.Must(srv.Listen(), "Could not start the empty server")
4550

4651
*prometheusx.ListenAddress = ":0"
4752
*scamperCtrlSocket = dir + "/scamper.sock"
4853
*eventsocket.Filename = dir + "/events.sock"
4954
*outputPath = dir
55+
*poll = false
5056
tracerType.Value = "paris-traceroute"
5157

5258
ctx, cancel = context.WithCancel(context.Background())
@@ -57,3 +63,25 @@ func TestMainWithConnectionListener(t *testing.T) {
5763
}()
5864
main()
5965
}
66+
67+
func TestMainWithBadArgs(t *testing.T) {
68+
tracerType.Value = "paris-traceroute"
69+
*eventsocket.Filename = ""
70+
*outputPath = "/tmp/"
71+
*poll = false
72+
73+
logFatal = func(_ ...interface{}) {
74+
panic("testpanic")
75+
}
76+
defer func() {
77+
logFatal = log.Fatal
78+
}()
79+
defer func() {
80+
r := recover()
81+
if r != "testpanic" {
82+
t.Error("Should have had a panic called testpanic, not", r)
83+
}
84+
}()
85+
86+
main()
87+
}

0 commit comments

Comments
 (0)