[PATCH stalld 07/36] tests/helpers: Replace sleep with poll in start_stalld_with_log()

Wander Lairson Costa <[email protected]>
Newsgroups org.kernel.vger.linux-rt-users
Message-ID <[email protected]>
start_stalld_with_log() uses a hardcoded sleep 1 after launching
stalld, which is both fragile and wasteful. On systems where BPF
backend initialization takes longer than one second, it is
insufficient. On fast systems, it delays every test unnecessarily.

Replace the fixed sleep with a polling loop that checks whether
stalld has started writing to the log file. A brief initial sleep
covers the fast path, then 1-second polling with a 15-second
timeout handles slow startup such as BPF initialization. On
timeout, the process is killed with SIGTERM and escalated to
SIGKILL if it does not exit within one second.

Signed-off-by: Wander Lairson Costa <[email protected]>
---
 tests/helpers/test_helpers.sh | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/tests/helpers/test_helpers.sh b/tests/helpers/test_helpers.sh
index 3fa8d07..f6d6ce2 100755
--- a/tests/helpers/test_helpers.sh
+++ b/tests/helpers/test_helpers.sh
@@ -948,11 +948,37 @@ start_stalld_with_log() {
 		echo "Using backend: ${STALLD_TEST_BACKEND}"
 	fi
 
-	# Start stalld with output redirected
-	${TEST_ROOT}/../stalld ${stalld_args} > "${log_file}" 2>&1 &
+	# Start stalld with line-buffered output so tail -f can detect
+	# readiness immediately instead of waiting for the buffer to fill.
+	stdbuf -oL ${TEST_ROOT}/../stalld ${stalld_args} > "${log_file}" 2>&1 &
 	STALLD_PID=$!
 	CLEANUP_PIDS+=("${STALLD_PID}")
+
+	# Poll for stalld to start writing to the log file rather than
+	# using a fixed sleep. Brief initial sleep covers the fast path,
+	# then 1-second polling for slow systems (e.g. BPF init).
+	sleep 0.01
+	local timeout=15
+	local elapsed=0
+	while [ $elapsed -lt $timeout ]; do
+		if ! kill -0 ${STALLD_PID} 2>/dev/null; then
+			echo -e "${RED}ERROR: stalld exited during startup${NC}"
+			return 1
+		fi
+		if [ -s "${log_file}" ]; then
+			return 0
+		fi
+		sleep 1
+		elapsed=$((elapsed + 1))
+	done
+
+	echo -e "${RED}ERROR: stalld did not produce output within ${timeout}s${NC}"
+	kill ${STALLD_PID} 2>/dev/null
 	sleep 1
+	if kill -0 ${STALLD_PID} 2>/dev/null; then
+		kill -9 ${STALLD_PID} 2>/dev/null
+	fi
+	return 1
 }
 
 # Wait for scheduling policy to change to expected value
-- 
2.53.0
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.