Skip to content

Commit 6fafc81

Browse files
ptarjanclaude
authored andcommitted
t7527: fix flaky fsmonitor event tests with retry logic
The fsmonitor event tests (edit, create, delete, rename, etc.) were flaky because there can be a race between the daemon writing events to the trace file and the test's grep commands checking for them. Add a retry_grep() helper function (similar to retry_until_success in lib-git-p4.sh) that retries grep with a timeout, and use it in all event-checking tests to wait for one expected event before checking the rest. Signed-off-by: Paul Tarjan <[email protected]>
1 parent 68cb7f9 commit 6fafc81

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

t/t7527-builtin-fsmonitor.sh

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,8 @@ move_directory() {
408408
# ensure we are getting the OS notifications and do not try to confirm what
409409
# is reported by `git status`.
410410
#
411-
# We run a simple query after modifying the filesystem just to introduce
412-
# a bit of a delay so that the trace logging from the daemon has time to
413-
# get flushed to disk.
411+
# We use retry_grep to handle races between the daemon writing events
412+
# to the trace file and our check.
414413
#
415414
# We `reset` and `clean` at the bottom of each test (and before stopping the
416415
# daemon) because these commands might implicitly restart the daemon.
@@ -422,16 +421,32 @@ clean_up_repo_and_stop_daemon () {
422421
rm -f .git/trace
423422
}
424423

424+
# Retry a grep up to RETRY_TIMEOUT times until it succeeds.
425+
#
426+
RETRY_TIMEOUT=5
427+
428+
retry_grep () {
429+
nr_tries_left=$RETRY_TIMEOUT
430+
until grep "$1" "$2" 2>/dev/null
431+
do
432+
if test $nr_tries_left -eq 0
433+
then
434+
grep "$1" "$2"
435+
return
436+
fi
437+
nr_tries_left=$(($nr_tries_left - 1))
438+
sleep 1
439+
done
440+
}
441+
425442
test_expect_success 'edit some files' '
426443
test_when_finished clean_up_repo_and_stop_daemon &&
427444
428445
start_daemon --tf "$PWD/.git/trace" &&
429446
430447
edit_files &&
431448
432-
test-tool fsmonitor-client query --token 0 &&
433-
434-
grep "^event: dir1/modified$" .git/trace &&
449+
retry_grep "^event: dir1/modified$" .git/trace &&
435450
grep "^event: dir2/modified$" .git/trace &&
436451
grep "^event: modified$" .git/trace &&
437452
grep "^event: dir1/untracked$" .git/trace
@@ -444,9 +459,7 @@ test_expect_success 'create some files' '
444459
445460
create_files &&
446461
447-
test-tool fsmonitor-client query --token 0 &&
448-
449-
grep "^event: dir1/new$" .git/trace &&
462+
retry_grep "^event: dir1/new$" .git/trace &&
450463
grep "^event: dir2/new$" .git/trace &&
451464
grep "^event: new$" .git/trace
452465
'
@@ -458,9 +471,7 @@ test_expect_success 'delete some files' '
458471
459472
delete_files &&
460473
461-
test-tool fsmonitor-client query --token 0 &&
462-
463-
grep "^event: dir1/delete$" .git/trace &&
474+
retry_grep "^event: dir1/delete$" .git/trace &&
464475
grep "^event: dir2/delete$" .git/trace &&
465476
grep "^event: delete$" .git/trace
466477
'
@@ -472,9 +483,7 @@ test_expect_success 'rename some files' '
472483
473484
rename_files &&
474485
475-
test-tool fsmonitor-client query --token 0 &&
476-
477-
grep "^event: dir1/rename$" .git/trace &&
486+
retry_grep "^event: dir1/rename$" .git/trace &&
478487
grep "^event: dir2/rename$" .git/trace &&
479488
grep "^event: rename$" .git/trace &&
480489
grep "^event: dir1/renamed$" .git/trace &&
@@ -489,9 +498,7 @@ test_expect_success 'rename directory' '
489498
490499
mv dirtorename dirrenamed &&
491500
492-
test-tool fsmonitor-client query --token 0 &&
493-
494-
grep "^event: dirtorename/*$" .git/trace &&
501+
retry_grep "^event: dirtorename/*$" .git/trace &&
495502
grep "^event: dirrenamed/*$" .git/trace
496503
'
497504

@@ -502,9 +509,7 @@ test_expect_success 'file changes to directory' '
502509
503510
file_to_directory &&
504511
505-
test-tool fsmonitor-client query --token 0 &&
506-
507-
grep "^event: delete$" .git/trace &&
512+
retry_grep "^event: delete$" .git/trace &&
508513
grep "^event: delete/new$" .git/trace
509514
'
510515

@@ -515,9 +520,7 @@ test_expect_success 'directory changes to a file' '
515520
516521
directory_to_file &&
517522
518-
test-tool fsmonitor-client query --token 0 &&
519-
520-
grep "^event: dir1$" .git/trace
523+
retry_grep "^event: dir1$" .git/trace
521524
'
522525

523526
# The next few test cases exercise the token-resync code. When filesystem

0 commit comments

Comments
 (0)