Skip to content

Commit ad204f8

Browse files
committed
fix: Using testing/synctest to make TestWriteUnitLevelSummary more reliable
1 parent 9bc14f7 commit ad204f8

File tree

1 file changed

+144
-48
lines changed

1 file changed

+144
-48
lines changed

internal/report/report_test.go

Lines changed: 144 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"regexp"
1010
"strings"
1111
"testing"
12+
"testing/synctest"
1213
"time"
1314

1415
"github.com/gruntwork-io/terragrunt/internal/report"
@@ -1175,19 +1176,35 @@ func TestWriteUnitLevelSummary(t *testing.T) {
11751176
{
11761177
name: "multiple runs sorted by duration",
11771178
setup: func(r *report.Report) {
1178-
// Add runs with different durations
1179-
longRun := newRun(t, filepath.Join(tmp, "long-run"))
1180-
r.AddRun(longRun)
1179+
// Use syntest.Test so that we can artificially manipulate the clock for duration testing.
1180+
synctest.Test(t, func(t *testing.T) {
1181+
t.Helper()
11811182

1182-
mediumRun := newRun(t, filepath.Join(tmp, "medium-run"))
1183-
r.AddRun(mediumRun)
1183+
longRun := newRun(t, filepath.Join(tmp, "long-run"))
1184+
r.AddRun(longRun)
11841185

1185-
shortRun := newRun(t, filepath.Join(tmp, "short-run"))
1186-
r.AddRun(shortRun)
1186+
time.Sleep(1 * time.Second)
11871187

1188-
r.EndRun(shortRun.Path)
1189-
r.EndRun(mediumRun.Path)
1190-
r.EndRun(longRun.Path)
1188+
mediumRun := newRun(t, filepath.Join(tmp, "medium-run"))
1189+
r.AddRun(mediumRun)
1190+
1191+
time.Sleep(1 * time.Second)
1192+
1193+
shortRun := newRun(t, filepath.Join(tmp, "short-run"))
1194+
r.AddRun(shortRun)
1195+
1196+
time.Sleep(1 * time.Second)
1197+
1198+
r.EndRun(shortRun.Path)
1199+
1200+
time.Sleep(1 * time.Second)
1201+
1202+
r.EndRun(mediumRun.Path)
1203+
1204+
time.Sleep(1 * time.Second)
1205+
1206+
r.EndRun(longRun.Path)
1207+
})
11911208
},
11921209
expected: `
11931210
❯❯ Run Summary 3 units x
@@ -1201,21 +1218,50 @@ func TestWriteUnitLevelSummary(t *testing.T) {
12011218
{
12021219
name: "mixed results grouped by category",
12031220
setup: func(r *report.Report) {
1204-
// Add runs with different results
1205-
successRun1 := newRun(t, filepath.Join(tmp, "success-1"))
1206-
successRun2 := newRun(t, filepath.Join(tmp, "success-2"))
1207-
failRun := newRun(t, filepath.Join(tmp, "fail-run"))
1208-
excludedRun := newRun(t, filepath.Join(tmp, "excluded-run"))
1209-
1210-
r.AddRun(successRun1)
1211-
r.AddRun(successRun2)
1212-
r.AddRun(failRun)
1213-
r.AddRun(excludedRun)
1221+
// Use syntest.Test so that we can artificially manipulate the clock for duration testing.
1222+
synctest.Test(t, func(t *testing.T) {
1223+
t.Helper()
1224+
1225+
successRun1 := newRun(t, filepath.Join(tmp, "success-1"))
1226+
1227+
time.Sleep(1 * time.Second)
1228+
1229+
successRun2 := newRun(t, filepath.Join(tmp, "success-2"))
1230+
1231+
time.Sleep(1 * time.Second)
1232+
1233+
failRun := newRun(t, filepath.Join(tmp, "fail-run"))
1234+
1235+
time.Sleep(1 * time.Second)
12141236

1215-
r.EndRun(successRun1.Path)
1216-
r.EndRun(successRun2.Path)
1217-
r.EndRun(failRun.Path, report.WithResult(report.ResultFailed))
1218-
r.EndRun(excludedRun.Path, report.WithResult(report.ResultExcluded))
1237+
excludedRun := newRun(t, filepath.Join(tmp, "excluded-run"))
1238+
1239+
r.AddRun(successRun1)
1240+
1241+
time.Sleep(1 * time.Second)
1242+
1243+
r.AddRun(successRun2)
1244+
1245+
time.Sleep(1 * time.Second)
1246+
1247+
r.AddRun(failRun)
1248+
1249+
time.Sleep(1 * time.Second)
1250+
1251+
r.AddRun(excludedRun)
1252+
1253+
time.Sleep(1 * time.Second)
1254+
r.EndRun(successRun1.Path)
1255+
1256+
time.Sleep(1 * time.Second)
1257+
r.EndRun(successRun2.Path)
1258+
1259+
time.Sleep(1 * time.Second)
1260+
r.EndRun(failRun.Path, report.WithResult(report.ResultFailed))
1261+
1262+
time.Sleep(1 * time.Second)
1263+
r.EndRun(excludedRun.Path, report.WithResult(report.ResultExcluded))
1264+
})
12191265
},
12201266
expected: `
12211267
❯❯ Run Summary 4 units x
@@ -1232,18 +1278,42 @@ func TestWriteUnitLevelSummary(t *testing.T) {
12321278
{
12331279
name: "very short unit names",
12341280
setup: func(r *report.Report) {
1235-
// Add runs with very short names
1236-
a := newRun(t, filepath.Join(tmp, "a"))
1237-
b := newRun(t, filepath.Join(tmp, "b"))
1238-
c := newRun(t, filepath.Join(tmp, "c"))
1239-
1240-
r.AddRun(a)
1241-
r.AddRun(b)
1242-
r.AddRun(c)
1243-
1244-
r.EndRun(a.Path)
1245-
r.EndRun(b.Path)
1246-
r.EndRun(c.Path)
1281+
// Use syntest.Test so that we can artificially manipulate the clock for duration testing.
1282+
synctest.Test(t, func(t *testing.T) {
1283+
t.Helper()
1284+
1285+
a := newRun(t, filepath.Join(tmp, "a"))
1286+
1287+
time.Sleep(1 * time.Second)
1288+
1289+
b := newRun(t, filepath.Join(tmp, "b"))
1290+
1291+
time.Sleep(1 * time.Second)
1292+
1293+
c := newRun(t, filepath.Join(tmp, "c"))
1294+
1295+
r.AddRun(a)
1296+
1297+
time.Sleep(1 * time.Second)
1298+
1299+
r.AddRun(b)
1300+
1301+
time.Sleep(1 * time.Second)
1302+
1303+
r.AddRun(c)
1304+
1305+
time.Sleep(1 * time.Second)
1306+
1307+
r.EndRun(a.Path)
1308+
1309+
time.Sleep(1 * time.Second)
1310+
1311+
r.EndRun(b.Path)
1312+
1313+
time.Sleep(1 * time.Second)
1314+
1315+
r.EndRun(c.Path)
1316+
})
12471317
},
12481318
expected: `
12491319
❯❯ Run Summary 3 units x
@@ -1257,18 +1327,44 @@ func TestWriteUnitLevelSummary(t *testing.T) {
12571327
{
12581328
name: "very long unit names",
12591329
setup: func(r *report.Report) {
1260-
// Add runs with very long names
1261-
longName1 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-1"))
1262-
longName2 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-2"))
1263-
longName3 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-3"))
1264-
1265-
r.AddRun(longName1)
1266-
r.AddRun(longName2)
1267-
r.AddRun(longName3)
1268-
1269-
r.EndRun(longName1.Path)
1270-
r.EndRun(longName2.Path)
1271-
r.EndRun(longName3.Path)
1330+
// Use syntest.Test so that we can artificially manipulate the clock for duration testing.
1331+
synctest.Test(t, func(t *testing.T) {
1332+
t.Helper()
1333+
1334+
longName1 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-1"))
1335+
1336+
time.Sleep(1 * time.Second)
1337+
1338+
longName2 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-2"))
1339+
1340+
time.Sleep(1 * time.Second)
1341+
1342+
longName3 := newRun(t, filepath.Join(tmp, "this-is-a-very-long-name-3"))
1343+
1344+
time.Sleep(1 * time.Second)
1345+
1346+
r.AddRun(longName1)
1347+
1348+
time.Sleep(1 * time.Second)
1349+
1350+
r.AddRun(longName2)
1351+
1352+
time.Sleep(1 * time.Second)
1353+
1354+
r.AddRun(longName3)
1355+
1356+
time.Sleep(1 * time.Second)
1357+
1358+
r.EndRun(longName1.Path)
1359+
1360+
time.Sleep(1 * time.Second)
1361+
1362+
r.EndRun(longName2.Path)
1363+
1364+
time.Sleep(1 * time.Second)
1365+
1366+
r.EndRun(longName3.Path)
1367+
})
12721368
},
12731369
expected: `
12741370
❯❯ Run Summary 3 units x

0 commit comments

Comments
 (0)