From 8113237ffcb391b0b3c881755b640ca188fc7633 Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Sat, 19 Oct 2024 12:36:25 +0300 Subject: [PATCH] test_: a flaky test --- flaky-test/one_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 flaky-test/one_test.go diff --git a/flaky-test/one_test.go b/flaky-test/one_test.go new file mode 100644 index 0000000000..8866c832c5 --- /dev/null +++ b/flaky-test/one_test.go @@ -0,0 +1,37 @@ +package flaky_test + +import ( + "os" + "testing" +) + +const stateFile = "test_state.txt" + +func TestFlaky(t *testing.T) { + if !checkState() { + // First run: fail the test + t.Errorf("Test failed on first run!") + } else { + // Second run: pass the test + t.Log("Test passed on second run!") + resetState() + } +} + +func checkState() bool { + // Check if the state file exists + if _, err := os.Stat(stateFile); !os.IsNotExist(err) { + // If file exists, return true (pass on second run) + return true + } + + // If file doesn't exist, create it and return false (fail on first run) + f, _ := os.Create(stateFile) + defer f.Close() + return false +} + +func resetState() { + // Remove the state file to reset the test state + os.Remove(stateFile) +}