Skip to content

Commit 479907e

Browse files
authored
Merge pull request #22 from pierresouchay/properly_stop_test_early_when_something_is_wrong
When test went wrong, test was not stopping properly.
2 parents 9a3e4ec + 6fdc7f7 commit 479907e

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

haproxy/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,19 @@ func newHaConfig(baseDir string, sd *lib.Shutdown) (*haConfig, error) {
113113
DataplanePass: dataplanePass,
114114
})
115115
if err != nil {
116+
sd.Done()
116117
return nil, err
117118
}
118119

119120
spoeCfgFile, err := os.OpenFile(cfg.SPOE, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
120121
if err != nil {
122+
sd.Done()
121123
return nil, err
122124
}
123125
defer spoeCfgFile.Close()
124126
_, err = spoeCfgFile.WriteString(spoeConfTmpl)
125127
if err != nil {
128+
sd.Done()
126129
return nil, err
127130
}
128131

haproxy_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ import (
1414

1515
func TestSetup(t *testing.T) {
1616
sd := lib.NewShutdown()
17+
client := startAgent(t, sd)
1718
defer func() {
1819
sd.Shutdown("test end")
1920
sd.Wait()
2021
}()
2122

22-
client := startAgent(t, sd)
23-
2423
csd, _, upstreamPorts := startConnectService(t, sd, client, &api.AgentServiceRegistration{
2524
Name: "source",
2625
ID: "source-1",
@@ -50,9 +49,7 @@ func TestSetup(t *testing.T) {
5049
})
5150

5251
startServer(t, sd, servicePort, "hello connect")
53-
54-
wait(csd, tsd)
55-
52+
wait(sd, csd, tsd)
5653
res, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d", upstreamPorts["target"]))
5754
require.NoError(t, err)
5855
require.Equal(t, 200, res.StatusCode)

utils_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func startAgent(t *testing.T, sd *lib.Shutdown) *api.Client {
2727
go func() {
2828
defer sd.Done()
2929
<-sd.Stop
30-
3130
a.Shutdown()
3231
}()
3332

@@ -179,8 +178,14 @@ func testGetReq(t *testing.T, port int, expectedCode int, exptectedContent strin
179178
}
180179
}
181180

182-
func wait(c ...chan struct{}) {
181+
func wait(sd *lib.Shutdown, c ...chan struct{}) {
183182
for _, o := range c {
184-
<-o
183+
select {
184+
case <-sd.Stop:
185+
return
186+
case <-o:
187+
continue
188+
189+
}
185190
}
186191
}

0 commit comments

Comments
 (0)