From 41ec2f7038b714448ab9ef35c0bda7ec4b1c667c Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Mon, 20 May 2024 02:34:17 +0900
Subject: [PATCH 0656/1160] test: sync journal before starting test

Follow-up for c22a112883a46e302dae587b809c459647363ceb.

Hopefully fixes #32712.

(cherry picked from commit bb84142513be74a953e13d614a9d893a3d45b06d)
---
 test/units/testsuite-60.sh | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/test/units/testsuite-60.sh b/test/units/testsuite-60.sh
index 73bef06ade..6f4cba8319 100755
--- a/test/units/testsuite-60.sh
+++ b/test/units/testsuite-60.sh
@@ -203,7 +203,7 @@ EOF
 }
 
 test_issue_23796() {
-    local mount_path mount_mytmpfs
+    local mount_path mount_mytmpfs since
 
     mount_path="$(command -v mount 2>/dev/null)"
     mount_mytmpfs="${mount_path/\/bin/\/sbin}.mytmpfs"
@@ -225,6 +225,9 @@ EOF
     # shellcheck disable=SC2064
     trap "rm -f /run/systemd/system/tmp-hoge.mount '$mount_mytmpfs'" RETURN
 
+    journalctl --sync
+    since="$(date '+%H:%M:%S')"
+
     for _ in {1..10}; do
         systemctl --no-block start tmp-hoge.mount
         sleep ".$RANDOM"
@@ -233,7 +236,7 @@ EOF
         sleep 1
 
         if [[ "$(systemctl is-failed tmp-hoge.mount)" == "failed" ]] || \
-           journalctl -u tmp-hoge.mount -q --grep "but there is no mount"; then
+           journalctl --since="$since" -u tmp-hoge.mount -q --grep "but there is no mount"; then
                 exit 1
         fi
 
@@ -249,6 +252,8 @@ NUM_DIRS=20
 # make sure we can handle mounts at very long paths such that mount unit name must be hashed to fall within our unit name limit
 LONGPATH="$(printf "/$(printf "x%0.s" {1..255})%0.s" {1..7})"
 LONGMNT="$(systemd-escape --suffix=mount --path "$LONGPATH")"
+
+journalctl --sync
 TS="$(date '+%H:%M:%S')"
 
 mkdir -p "$LONGPATH"
@@ -271,6 +276,9 @@ for ((i = 0; i < NUM_DIRS; i++)); do
     mkdir "/tmp/meow${i}"
 done
 
+# The following loop may produce many journal entries.
+# Let's process all pending entries before testing.
+journalctl --sync
 TS="$(date '+%H:%M:%S')"
 
 for ((i = 0; i < NUM_DIRS; i++)); do
@@ -286,7 +294,9 @@ done
 
 # Figure out if we have entered the rate limit state.
 # If the infra is slow we might not enter the rate limit state; in that case skip the exit check.
+journalctl --sync
 if timeout 2m bash -c "until journalctl -u init.scope --since=$TS | grep -q '(mount-monitor-dispatch) entered rate limit'; do journalctl --sync; sleep 1; done"; then
+    journalctl --sync
     timeout 2m bash -c "until journalctl -u init.scope --since=$TS | grep -q '(mount-monitor-dispatch) left rate limit'; do journalctl --sync; sleep 1; done"
 fi
 
-- 
2.33.0