test(brainfstest): swap-race asserts fail when nothing was tested
AssertNoLeakFrom and the search Query race test passed vacuously when every symlink swap failed or every read errored. Both now count successful symlink swaps and reads that returned the inside note and fail if either is zero; the Query race reader no longer swallows Query's error. Verified by flipping: a swapper whose os.Symlink always fails turns notes, wiki and search race tests red; Query on a missing brain dir turns the search race test red. Closes #177 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -93,16 +93,20 @@ func TestQueryDoesNotFollowASymlinkedWingUnderAWingAndHallRoot(t *testing.T) {
|
||||
// bypass of readOpened hung Query after 5 iterations).
|
||||
// startSwap runs swapInto until the test ends and waits for it before
|
||||
// t.TempDir's cleanup (hyperguild#162).
|
||||
func startSwap(t *testing.T, path, outside string, fifo bool) {
|
||||
// It returns the count of symlinks or FIFOs actually renamed into path
|
||||
// (hyperguild#177: a swapper whose every swap fails tests nothing).
|
||||
func startSwap(t *testing.T, path, outside string, fifo bool) *atomic.Int64 {
|
||||
t.Helper()
|
||||
var stop atomic.Bool
|
||||
var swaps atomic.Int64
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); swapInto(path, outside, fifo, &stop) }()
|
||||
go func() { defer wg.Done(); swapInto(path, outside, fifo, &stop, &swaps) }()
|
||||
t.Cleanup(func() { stop.Store(true); wg.Wait() })
|
||||
return &swaps
|
||||
}
|
||||
|
||||
func swapInto(path, outside string, fifo bool, stop *atomic.Bool) {
|
||||
func swapInto(path, outside string, fifo bool, stop *atomic.Bool, swaps *atomic.Int64) {
|
||||
tmp := path + ".swap"
|
||||
// Writers stay open until the swapper exits, so a reader parked on a
|
||||
// FIFO stays parked (closing one would hand the reader EOF and hide the
|
||||
@@ -115,33 +119,46 @@ func swapInto(path, outside string, fifo bool, stop *atomic.Bool) {
|
||||
}()
|
||||
for i := 0; !stop.Load(); i++ {
|
||||
_ = os.Remove(tmp)
|
||||
var made error
|
||||
switch {
|
||||
case i%2 == 0:
|
||||
_ = os.WriteFile(tmp, []byte("# r\n\nzqinside "+"zqracenote"), 0o644)
|
||||
case fifo:
|
||||
if err := syscall.Mkfifo(tmp, 0o644); err == nil {
|
||||
if made = syscall.Mkfifo(tmp, 0o644); made == nil {
|
||||
if w, err := os.OpenFile(tmp, os.O_RDWR, 0); err == nil {
|
||||
writers = append(writers, w)
|
||||
}
|
||||
}
|
||||
default:
|
||||
_ = os.Symlink(outside, tmp)
|
||||
made = os.Symlink(outside, tmp)
|
||||
}
|
||||
if err := os.Rename(tmp, path); err == nil && made == nil && i%2 == 1 {
|
||||
swaps.Add(1)
|
||||
}
|
||||
_ = os.Rename(tmp, path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQueryLeaksNothingUnderASymlinkSwapRace(t *testing.T) {
|
||||
brain, facts, outside := searchBrain(t)
|
||||
startSwap(t, filepath.Join(facts, "r.md"), outside, false)
|
||||
leaks, iters := 0, 0
|
||||
swaps := startSwap(t, filepath.Join(facts, "r.md"), outside, false)
|
||||
leaks, iters, insideReads := 0, 0, 0
|
||||
for deadline := time.Now().Add(1500 * time.Millisecond); time.Now().Before(deadline); iters++ {
|
||||
rs, _ := Query(brain, QueryOptions{Query: sentinel157, Limit: 10})
|
||||
rs, err := Query(brain, QueryOptions{Query: sentinel157, Limit: 10})
|
||||
require.NoError(t, err)
|
||||
leaks += leaked(rs)
|
||||
// Positive control (hyperguild#177): a Query that always errors
|
||||
// returns nothing and so never leaks.
|
||||
rs, err = Query(brain, QueryOptions{Query: "zqracenote", Limit: 10})
|
||||
require.NoError(t, err)
|
||||
if len(rs) > 0 {
|
||||
insideReads++
|
||||
}
|
||||
}
|
||||
if leaks > 0 {
|
||||
t.Fatalf("outside file leaked %d times in %d iterations", leaks, iters)
|
||||
}
|
||||
require.Positive(t, swaps.Load(), "no symlink was swapped in: the race did not run")
|
||||
require.Positive(t, insideReads, "no Query returned the inside note")
|
||||
}
|
||||
|
||||
func TestQueryDoesNotHangUnderAFIFOSwapRace(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user