Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 290a8dc

Browse files
committed
test: Fix flaky TestServer/Logging/Multiple
1 parent 8ae28a3 commit 290a8dc

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

cli/server_test.go

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,10 @@ func TestServer(t *testing.T) {
12791279
_ = os.Remove(fi3.Name())
12801280
}()
12811281

1282+
// NOTE(mafredri): This test might end up downloading Terraform
1283+
// which can take a long time and end up failing the test.
1284+
// This is why we wait extra long below for server to listen on
1285+
// HTTP.
12821286
root, _ := clitest.New(t,
12831287
"server",
12841288
"--verbose",
@@ -1289,26 +1293,38 @@ func TestServer(t *testing.T) {
12891293
"--log-json", fi2.Name(),
12901294
"--log-stackdriver", fi3.Name(),
12911295
)
1296+
// Attach pty so we get debug output from the command if this test
1297+
// fails.
1298+
pty := ptytest.New(t)
1299+
root.SetOut(pty.Output())
1300+
root.SetErr(pty.Output())
1301+
12921302
serverErr := make(chan error, 1)
12931303
go func() {
12941304
serverErr <- root.ExecuteContext(ctx)
12951305
}()
1306+
defer func() {
1307+
cancelFunc()
1308+
<-serverErr
1309+
}()
12961310

1297-
assert.Eventually(t, func() bool {
1311+
require.Eventually(t, func() bool {
1312+
line := pty.ReadLine()
1313+
return strings.HasPrefix(line, "Started HTTP listener at ")
1314+
}, testutil.WaitLong*2, testutil.IntervalMedium, "wait for server to listen on http")
1315+
1316+
require.Eventually(t, func() bool {
12981317
stat, err := os.Stat(fi1.Name())
12991318
return err == nil && stat.Size() > 0
1300-
}, testutil.WaitLong, testutil.IntervalMedium)
1301-
assert.Eventually(t, func() bool {
1319+
}, testutil.WaitShort, testutil.IntervalMedium, "log human size > 0")
1320+
require.Eventually(t, func() bool {
13021321
stat, err := os.Stat(fi2.Name())
13031322
return err == nil && stat.Size() > 0
1304-
}, testutil.WaitLong, testutil.IntervalMedium)
1305-
assert.Eventually(t, func() bool {
1323+
}, testutil.WaitShort, testutil.IntervalMedium, "log json size > 0")
1324+
require.Eventually(t, func() bool {
13061325
stat, err := os.Stat(fi3.Name())
13071326
return err == nil && stat.Size() > 0
1308-
}, testutil.WaitLong, testutil.IntervalMedium)
1309-
1310-
cancelFunc()
1311-
<-serverErr
1327+
}, testutil.WaitShort, testutil.IntervalMedium, "log stackdriver size > 0")
13121328
})
13131329
})
13141330
}

0 commit comments

Comments
 (0)