Skip to content

Commit 3fba953

Browse files
committed
Add race tests
1 parent fdc9708 commit 3fba953

File tree

2 files changed

+146
-3
lines changed

2 files changed

+146
-3
lines changed

tools/flakeguard/runner/flaky_test_package/example_tests_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package flakytestpackage
22

33
import (
44
"os"
5+
"sync"
56
"testing"
67
)
78

@@ -49,3 +50,32 @@ func TestPanic(t *testing.T) {
4950
t.Parallel()
5051
panic("This test intentionally panics")
5152
}
53+
54+
func TestRace(t *testing.T) {
55+
t.Parallel()
56+
t.Logf("This test should trigger a failure if run with the -race flag, but otherwise pass")
57+
58+
var sharedCounter int
59+
var wg sync.WaitGroup
60+
61+
// Define a worker function that accesses sharedCounter without synchronization
62+
worker := func() {
63+
defer wg.Done()
64+
for i := 0; i < 1000; i++ {
65+
sharedCounter++
66+
}
67+
}
68+
69+
// Start multiple goroutines to introduce a race condition
70+
const numGoroutines = 10
71+
wg.Add(numGoroutines)
72+
for i := 0; i < numGoroutines; i++ {
73+
go worker()
74+
}
75+
76+
// Wait for all goroutines to complete
77+
wg.Wait()
78+
79+
// Log the result
80+
t.Logf("Final value of sharedCounter: %d", sharedCounter)
81+
}

tools/flakeguard/runner/runner_test.go

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ func TestRunDefault(t *testing.T) {
6767
PassRatio: 0,
6868
},
6969
},
70+
"TestRace": {
71+
TestResult: &reports.TestResult{
72+
TestName: "TestPass",
73+
Panicked: false,
74+
Skipped: false,
75+
PassRatio: 1,
76+
Successes: defaultRuns,
77+
},
78+
},
7079
}
7180

7281
testResults, err := defaultTestRunner.RunTests()
@@ -161,6 +170,15 @@ func TestRunWithPanics(t *testing.T) {
161170
Skipped: false,
162171
},
163172
},
173+
"TestRace": {
174+
TestResult: &reports.TestResult{
175+
TestName: "TestPass",
176+
Panicked: false,
177+
Skipped: false,
178+
PassRatio: 1,
179+
Successes: defaultRuns,
180+
},
181+
},
164182
}
165183

166184
testResults, err := panicTestRunner.RunTests()
@@ -218,9 +236,6 @@ func cleanup(t *testing.T, testResults []reports.TestResult, runner Runner) {
218236
if err != nil {
219237
t.Logf("error writing test results: %v", err)
220238
}
221-
if len(runner.RawOutputs()) == 0 {
222-
t.Logf("WARN: no raw output to write")
223-
}
224239
for packageName, rawOutput := range runner.RawOutputs() {
225240
saniPackageName := filepath.Base(packageName)
226241
rawOutputFileName := fmt.Sprintf("debug_raw_output_%s_%s.json", t.Name(), saniPackageName)
@@ -233,3 +248,101 @@ func cleanup(t *testing.T, testResults []reports.TestResult, runner Runner) {
233248
}
234249
})
235250
}
251+
252+
func TestRunWithRace(t *testing.T) {
253+
raceTestRunner := Runner{
254+
ProjectPath: "./",
255+
Verbose: true,
256+
RunCount: defaultRuns,
257+
UseRace: true,
258+
SkipTests: []string{"TestPanic"},
259+
FailFast: false,
260+
SelectedTestPackages: []string{"./flaky_test_package"},
261+
CollectRawOutput: true,
262+
}
263+
264+
expectedResults := map[string]*expectedTestResult{
265+
"TestFlaky": {
266+
TestResult: &reports.TestResult{
267+
TestName: "TestFlaky",
268+
Panicked: false,
269+
Skipped: false,
270+
},
271+
},
272+
"TestFail": {
273+
TestResult: &reports.TestResult{
274+
TestName: "TestFail",
275+
Panicked: false,
276+
Skipped: false,
277+
PassRatio: 0,
278+
Failures: defaultRuns,
279+
},
280+
},
281+
"TestPass": {
282+
TestResult: &reports.TestResult{
283+
TestName: "TestPass",
284+
Panicked: false,
285+
Skipped: false,
286+
PassRatio: 1,
287+
Successes: defaultRuns,
288+
},
289+
},
290+
"TestSkipped": {
291+
TestResult: &reports.TestResult{
292+
TestName: "TestSkipped",
293+
Panicked: false,
294+
Skipped: true,
295+
PassRatio: 0,
296+
},
297+
},
298+
"TestRace": {
299+
TestResult: &reports.TestResult{
300+
TestName: "TestPass",
301+
Panicked: false,
302+
Skipped: false,
303+
PassRatio: 0,
304+
Failures: defaultRuns,
305+
},
306+
},
307+
}
308+
309+
testResults, err := raceTestRunner.RunTests()
310+
require.NoError(t, err)
311+
cleanup(t, testResults, raceTestRunner)
312+
313+
for _, result := range testResults {
314+
t.Run(fmt.Sprintf("checking results of %s", result.TestName), func(t *testing.T) {
315+
expected, ok := expectedResults[result.TestName]
316+
// Sanity checks
317+
require.True(t, ok, "unexpected test result: %s", result.TestName)
318+
require.False(t, expected.seen, "test '%s' was seen multiple times", result.TestName)
319+
expected.seen = true
320+
321+
assert.Equal(t, defaultRuns, result.Runs, "test '%s' had an unexpected number of runs", result.TestName)
322+
assert.Len(t, result.Durations, result.Runs, "test '%s' has a mismatch of runs and duration counts", result.TestName, defaultRuns)
323+
resultCounts := result.Successes + result.Failures + result.Panics + result.Skips
324+
assert.Equal(t, result.Runs, resultCounts,
325+
"test '%s' doesn't match Runs count with results counts\nRuns: %d\nSuccesses: %d\nFailures: %d\nPanics: %d\nSkips: %d\nTotal: %d",
326+
result.TestName, result.Runs, result.Successes, result.Failures, result.Panics, result.Skips, resultCounts,
327+
)
328+
assert.Equal(t, expected.TestResult.Panicked, result.Panicked, "test '%s' had an unexpected panic result", result.TestName)
329+
assert.Equal(t, expected.TestResult.Skipped, result.Skipped, "test '%s' had an unexpected skipped result", result.TestName)
330+
331+
if result.TestName == "TestFlaky" {
332+
assert.Greater(t, result.Successes, 0, "flaky test '%s' should have passed some", result.TestName)
333+
assert.Greater(t, result.Failures, 0, "flaky test '%s' should have failed some", result.TestName)
334+
assert.Greater(t, result.PassRatio, float64(0), "flaky test '%s' should have a flaky pass ratio", result.TestName)
335+
assert.Less(t, result.PassRatio, float64(1), "flaky test '%s' should have a flaky pass ratio", result.TestName)
336+
} else {
337+
assert.Equal(t, expected.TestResult.PassRatio, result.PassRatio, "test '%s' had an unexpected pass ratio", result.TestName)
338+
assert.Equal(t, expected.TestResult.Successes, result.Successes, "test '%s' had an unexpected number of successes", result.TestName)
339+
assert.Equal(t, expected.TestResult.Failures, result.Failures, "test '%s' had an unexpected number of failures", result.TestName)
340+
}
341+
})
342+
}
343+
344+
for _, expected := range expectedResults {
345+
assert.True(t, expected.seen, "expected test '%s' not found in test runs", expected.TestResult.TestName)
346+
}
347+
348+
}

0 commit comments

Comments
 (0)