Skip to content

Commit

Permalink
test_: a flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Oct 19, 2024
1 parent c68293f commit 8113237
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions flaky-test/one_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 8113237

Please sign in to comment.