Skip to content

Commit c31e21d

Browse files
authored
Fail on any test timeout (#2929)
1 parent e10384f commit c31e21d

File tree

1 file changed

+101
-16
lines changed

1 file changed

+101
-16
lines changed

scripts/test_analysis/main.go

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ func (t *tester) runTests(passThruFlags []string) error {
5151
}
5252
log.Printf("Not all tests passed: %v", err)
5353

54+
timedOutPackages, err := t.findTimedoutTests(context.Background())
55+
if err != nil {
56+
return err
57+
}
58+
if len(timedOutPackages) > 0 {
59+
// Fail immediately if we find any timeouts. We'd have to run all tests
60+
// in the package, and this could take a long time.
61+
log.Printf("Found %d timed out packages. Failing", len(timedOutPackages))
62+
return errors.New("one or more tests timed out")
63+
}
64+
5465
failedTests, err := t.findFailedTests(context.Background())
5566
if err != nil {
5667
return err
@@ -129,6 +140,11 @@ type failedTest struct {
129140
Test string
130141
}
131142

143+
type timedOutPackage struct {
144+
Package string
145+
Outputs string
146+
}
147+
132148
func (t *tester) findFailedTests(ctx context.Context) ([]failedTest, error) {
133149
db, err := sql.Open("sqlite", t.Dir+dbPath)
134150
if err != nil {
@@ -151,6 +167,49 @@ func (t *tester) findFailedTests(ctx context.Context) ([]failedTest, error) {
151167
return out, nil
152168
}
153169

170+
func (t *tester) findTimedoutTests(ctx context.Context) ([]timedOutPackage, error) {
171+
db, err := sql.Open("sqlite", t.Dir+dbPath)
172+
if err != nil {
173+
return nil, err
174+
}
175+
defer db.Close()
176+
177+
rows, err := db.QueryContext(ctx, `WITH failed_packages AS (
178+
SELECT
179+
Package
180+
FROM
181+
test_results
182+
WHERE
183+
Action = 'fail'
184+
AND Elapsed > 300
185+
)
186+
SELECT
187+
test_results.Package, GROUP_CONCAT(Output, "") as Outputs
188+
FROM
189+
test_results
190+
INNER JOIN
191+
failed_packages
192+
ON
193+
test_results.Package = failed_packages.Package
194+
GROUP BY
195+
test_results.Package
196+
HAVING
197+
Outputs LIKE '%timed out%'
198+
ORDER BY Time;`)
199+
if err != nil {
200+
return nil, err
201+
}
202+
var out []timedOutPackage
203+
for rows.Next() {
204+
var pkg, outputs string
205+
if err := rows.Scan(&pkg, &outputs); err != nil {
206+
return nil, err
207+
}
208+
out = append(out, timedOutPackage{pkg, outputs})
209+
}
210+
return out, nil
211+
}
212+
154213
func filterOutFlags(flags []string, exclude *regexp.Regexp) []string {
155214
out := make([]string, 0, len(flags))
156215
for _, f := range flags {
@@ -170,20 +229,45 @@ func (t *tester) summarize() (string, error) {
170229
if err != nil {
171230
return "", err
172231
}
232+
timeouts, err := t.findTimedoutTests(ctx)
233+
if err != nil {
234+
return "", err
235+
}
236+
237+
testFailureCount := len(testFailures) + len(timeouts)
173238

174239
plural := "s"
175-
if len(testFailures) == 1 {
240+
if testFailureCount == 1 {
176241
plural = ""
177242
}
178-
out.WriteString(fmt.Sprintf("## %d Test Failure%s\n\n", len(testFailures), plural))
243+
out.WriteString(fmt.Sprintf("## %d Test Failure%s\n\n", testFailureCount, plural))
179244

180-
db, err := sql.Open("sqlite", t.Dir+dbPath)
181-
if err != nil {
182-
return "", err
245+
if len(timeouts) > 0 {
246+
out.WriteString("### Timed Out Tests\n\n")
247+
for _, timeout := range timeouts {
248+
_, err = out.WriteString(fmt.Sprintf(`<details>
249+
<summary>%s</summary>
250+
<pre>
251+
%s
252+
</pre>
253+
</details>`, timeout.Package, timeout.Outputs))
254+
if err != nil {
255+
return "", err
256+
}
257+
}
258+
out.WriteString("\n")
183259
}
184-
defer db.Close()
185260

186-
rows, err := db.QueryContext(ctx, `SELECT
261+
if len(testFailures) > 0 {
262+
out.WriteString("### Failed Tests\n\n")
263+
264+
db, err := sql.Open("sqlite", t.Dir+dbPath)
265+
if err != nil {
266+
return "", err
267+
}
268+
defer db.Close()
269+
270+
rows, err := db.QueryContext(ctx, `SELECT
187271
tr_output.Package,
188272
tr_output.Test,
189273
GROUP_CONCAT(tr_output.Output, "") AS Outputs
@@ -204,22 +288,23 @@ GROUP BY
204288
tr_output.Test
205289
ORDER BY
206290
MIN(tr_output.Time);`)
207-
if err != nil {
208-
return "", err
209-
}
210-
for rows.Next() {
211-
var pkg, test, outputs string
212-
if err := rows.Scan(&pkg, &test, &outputs); err != nil {
291+
if err != nil {
213292
return "", err
214293
}
215-
_, err = out.WriteString(fmt.Sprintf(`<details>
294+
for rows.Next() {
295+
var pkg, test, outputs string
296+
if err := rows.Scan(&pkg, &test, &outputs); err != nil {
297+
return "", err
298+
}
299+
_, err = out.WriteString(fmt.Sprintf(`<details>
216300
<summary>%s.%s</summary>
217301
<pre>
218302
%s
219303
</pre>
220304
</details>`, pkg, test, outputs))
221-
if err != nil {
222-
return "", err
305+
if err != nil {
306+
return "", err
307+
}
223308
}
224309
}
225310
return out.String(), nil

0 commit comments

Comments
 (0)