From bc88d09a0825e6e88600a72bda24fa1003f35fff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=B6rg=20Thalheim?= <joerg@thalheim.io>
Date: Mon, 7 Apr 2025 13:07:30 +0200
Subject: [PATCH] tests/functional/repl: fix race condition

the sleep 1 is not enough in some circumstances. Switching to a fifo
helps.

(cherry picked from commit 1de951d31d2683c6f401cc96d918ff052342037f)
---
 tests/functional/repl.sh | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)

diff --git a/tests/functional/repl.sh b/tests/functional/repl.sh
index 706e0f5db..d2adeeecf 100755
--- a/tests/functional/repl.sh
+++ b/tests/functional/repl.sh
@@ -158,15 +158,32 @@ foo + baz
 # - Modify the flake
 # - Re-eval it
 # - Check that the result has changed
-replResult=$( (
-echo "changingThing"
-sleep 1 # Leave the repl the time to eval 'foo'
+mkfifo repl_fifo
+nix repl ./flake --experimental-features 'flakes' < repl_fifo > repl_output 2>&1 &
+repl_pid=$!
+exec 3>repl_fifo # Open fifo for writing
+echo "changingThing" >&3
+for i in $(seq 1 1000); do
+    if grep -q "beforeChange" repl_output; then
+        break
+    fi
+    cat repl_output
+    sleep 0.1
+done
+if [[ "$i" -eq 100 ]]; then
+    echo "Timed out waiting for beforeChange"
+    exit 1
+fi
+
 sed -i 's/beforeChange/afterChange/' flake/flake.nix
-echo ":reload"
-echo "changingThing"
-) | nix repl ./flake --experimental-features 'flakes')
-echo "$replResult" | grepQuiet -s beforeChange
-echo "$replResult" | grepQuiet -s afterChange
+
+# Send reload and second command
+echo ":reload" >&3
+echo "changingThing" >&3
+echo "exit" >&3
+exec 3>&- # Close fifo
+wait $repl_pid # Wait for process to finish
+grep -q "afterChange" repl_output
 
 # Test recursive printing and formatting
 # Normal output should print attributes in lexicographical order non-recursively