Skip to content

Commit 84e0371

Browse files
authored
Merge pull request #9608 from keymanapp/chore/linux/fixbug
chore(linux): Check and restart background processes
2 parents cc82a3c + 4ecaad6 commit 84e0371

File tree

6 files changed

+110
-52
lines changed

6 files changed

+110
-52
lines changed

linux/ibus-keyman/src/test/meson.build

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ test_include_dirs = [
1919
]
2020

2121
env_file = '/tmp/env-src-test.txt'
22-
pid_file = '/tmp/ibus-keyman-src-test-pids'
22+
cleanup_file = '/tmp/ibus-keyman-src-test-pids'
23+
pid_file = '/tmp/ibus-keyman-src-test-pids.pids'
2324

2425
setup_src_test_tests = find_program('setup-tests.sh', dirs: [meson.current_source_dir()])
2526
teardown_tests = find_program('teardown-tests.sh', dirs: [meson.current_source_dir() / '../../tests/scripts'])
@@ -64,7 +65,7 @@ bcp47_util_tests = executable(
6465
test(
6566
'setup-src-test',
6667
setup_src_test_tests,
67-
args: ['--x11', env_file, pid_file],
68+
args: ['--x11', env_file, cleanup_file, pid_file],
6869
env: test_env,
6970
priority: -1,
7071
is_parallel: false,
@@ -74,7 +75,7 @@ test(
7475
test(
7576
'teardown-src-test',
7677
teardown_tests,
77-
args: [pid_file],
78+
args: [cleanup_file],
7879
priority: -9,
7980
is_parallel: false,
8081
protocol: 'exitcode'

linux/ibus-keyman/src/test/setup-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ set -eu
33

44
. "$(dirname "$0")/../../tests/scripts/test-helper.inc.sh"
55

6-
setup_display_server_only "$1" "$2" "$3"
6+
setup_display_server_only "$1" "$2" "$3" "$4"

linux/ibus-keyman/tests/meson.build

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ stop_test_server = executable(
5050
)
5151

5252
env_file = '/tmp/env.txt'
53-
pid_file = '/tmp/ibus-keyman-test-pids'
53+
cleanup_file = '/tmp/ibus-keyman-test-pids'
54+
pid_file = '/tmp/ibus-keyman-test-pids.pids'
5455

5556
setup_tests = find_program('setup-tests.sh', dirs: [meson.current_source_dir() / 'scripts'])
5657
teardown_tests = find_program('teardown-tests.sh', dirs: [meson.current_source_dir() / 'scripts'])
@@ -64,7 +65,7 @@ can_build_wayland = mutter.found()
6465
test(
6566
'setup-x11',
6667
setup_tests,
67-
args: ['--x11', env_file, pid_file],
68+
args: ['--x11', env_file, cleanup_file, pid_file],
6869
env: test_env,
6970
priority: -10,
7071
is_parallel: false,
@@ -74,7 +75,7 @@ test(
7475
test(
7576
'teardown-x11',
7677
teardown_tests,
77-
args: [pid_file],
78+
args: [cleanup_file],
7879
priority: -19,
7980
is_parallel: false,
8081
protocol: 'exitcode'
@@ -84,7 +85,7 @@ if can_build_wayland
8485
test(
8586
'setup-wayland',
8687
setup_tests,
87-
args: ['--wayland', env_file, pid_file],
88+
args: ['--wayland', env_file, cleanup_file, pid_file],
8889
env: test_env,
8990
priority: -20,
9091
is_parallel: false,
@@ -94,7 +95,7 @@ if can_build_wayland
9495
test(
9596
'teardown-wayland',
9697
teardown_tests,
97-
args: [pid_file],
98+
args: [cleanup_file],
9899
priority: -29,
99100
is_parallel: false,
100101
protocol: 'exitcode'
@@ -113,7 +114,7 @@ foreach kmx: kmxtest_files
113114
continue
114115
endif
115116
testname = filename[1].split('.kmx')[0]
116-
test_args = [ '--tap', '-k', '--env', env_file, '--', filename]
117+
test_args = [ '--tap', '-k', '--env', env_file, '--cleanup', cleanup_file, '--check', pid_file, '--', filename]
117118
test(
118119
'X11-' + testname + '__surrounding-text',
119120
run_test,

linux/ibus-keyman/tests/scripts/run-single-test.sh

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fi
2121

2222
function help() {
2323
echo "Usage:"
24-
echo " $0 [--env <envfile>] [-k] [--tap] [--surrounding-text] [--no-surrounding-text] [--wayland|--x11] [--] TEST"
24+
echo " $0 [--env <envfile>] [-k] [--tap] [--surrounding-text] [--no-surrounding-text] [--wayland|--x11] [--check <pidfile> --cleanup <file>] [--] TEST"
2525
echo
2626
echo "Arguments:"
2727
echo " --help, -h, -? Display this help"
@@ -34,6 +34,8 @@ function help() {
3434
echo " --wayland run tests with Wayland"
3535
echo " --x11 run tests with X11"
3636
echo " --env <envfile> Name of the file containing environment variables to use"
37+
echo " --check <pidfile> Name of the file containing pids to check are running"
38+
echo " --cleanup <file> Name of the file containing cleanup of processes"
3739
exit 0
3840
}
3941

@@ -65,12 +67,22 @@ while (( $# )); do
6567
--verbose|-v) ARG_VERBOSE=--verbose;;
6668
--debug) ARG_DEBUG=--debug-log;;
6769
--env) shift ; ARG_ENV=$1 ;;
70+
--check) shift; ARG_PIDS=$1 ;;
71+
--cleanup) shift; ARG_CLEANUP=$1 ;;
6872
--) shift ; TESTFILE=$1; break ;;
6973
*) echo "Error: Unexpected argument \"$1\". Exiting." ; exit 4 ;;
7074
esac
7175
shift || (echo "Error: The last argument is missing a value. Exiting."; false) || exit 5
7276
done
7377

78+
# shellcheck disable=SC2236
79+
if [ -n "${ARG_PIDS:-}" ] && [ ! -n "${ARG_CLEANUP:-}" ]; then
80+
echo "Error: '--check' also requires '--cleanup'. Exiting."
81+
exit 6
82+
fi
83+
84+
check_processes_running "$ARG_DISPLAY_SERVER" "$ARG_ENV" "$ARG_CLEANUP" "$ARG_PIDS" >&2
85+
7486
# shellcheck source=/dev/null
7587
. "$ARG_ENV"
7688

linux/ibus-keyman/tests/scripts/setup-tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ set -eu
55

66
exit_on_package_build
77

8-
setup "$1" "$2" "$3"
8+
setup "$1" "$2" "$3" "$4"

linux/ibus-keyman/tests/scripts/test-helper.inc.sh

Lines changed: 84 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ function _link_test_keyboards() {
9494
}
9595

9696
function _setup_init() {
97-
local ENV_FILE PID_FILE
97+
local ENV_FILE CLEANUP_FILE PID_FILE
9898
ENV_FILE=$1
99-
PID_FILE=$2
99+
CLEANUP_FILE=$2
100+
PID_FILE=$3
100101

101102
if [ -z "${TOP_SRCDIR:-}" ]; then
102103
TOP_SRCDIR=${G_TEST_SRCDIR:-$(realpath "$(dirname "$0")/..")}/..
@@ -107,14 +108,15 @@ function _setup_init() {
107108

108109
echo > "$ENV_FILE"
109110

110-
if [ -f "$PID_FILE" ]; then
111+
if [ -f "$CLEANUP_FILE" ]; then
111112
# kill previous instances
112-
"$(dirname "$0")"/teardown-tests.sh "$PID_FILE" || true
113+
"$(dirname "$0")"/teardown-tests.sh "$CLEANUP_FILE" || true
113114
fi
114115

116+
echo > "$CLEANUP_FILE"
115117
echo > "$PID_FILE"
116118
TEMP_DATA_DIR=$(mktemp --directory)
117-
echo "rm -rf ${TEMP_DATA_DIR} || true" >> "$PID_FILE"
119+
echo "rm -rf ${TEMP_DATA_DIR} || true" >> "$CLEANUP_FILE"
118120

119121
COMMON_ARCH_DIR=
120122
[ -d "${TOP_SRCDIR}"/../../core/build/arch ] && COMMON_ARCH_DIR=${TOP_SRCDIR}/../../core/build/arch
@@ -133,27 +135,28 @@ function _setup_init() {
133135
}
134136

135137
function _setup_test_dbus_server() {
136-
local ENV_FILE PID_FILE
138+
local ENV_FILE CLEANUP_FILE
137139
ENV_FILE=$1
138-
PID_FILE=$2
140+
CLEANUP_FILE=$2
139141

140142
# Start test dbus server. This will create `/tmp/km-test-server.env`.
141143
"${TOP_BINDIR}/tests/km-dbus-test-server" &> /dev/null &
142144
sleep 1
143145

144146
cat /tmp/km-test-server.env >> "$ENV_FILE"
145-
cat /tmp/km-test-server.env >> "$PID_FILE"
146-
echo "${TOP_BINDIR}/tests/stop-test-server" >> "$PID_FILE"
147+
cat /tmp/km-test-server.env >> "$CLEANUP_FILE"
148+
echo "${TOP_BINDIR}/tests/stop-test-server" >> "$CLEANUP_FILE"
147149

148150
source /tmp/km-test-server.env
149151
echo "# DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
150152
}
151153

152154
function _setup_display_server() {
153-
local DISPLAY_SERVER ENV_FILE PID_FILE
155+
local DISPLAY_SERVER ENV_FILE CLEANUP_FILE PID_FILE PID
154156
ENV_FILE=$1
155-
PID_FILE=$2
156-
DISPLAY_SERVER=$3
157+
CLEANUP_FILE=$2
158+
PID_FILE=$3
159+
DISPLAY_SERVER=$4
157160

158161
if [ "$DISPLAY_SERVER" == "--wayland" ]; then
159162
if ! can_run_wayland; then
@@ -166,7 +169,9 @@ function _setup_display_server() {
166169
TMPFILE=$(mktemp)
167170
# mutter-Message: 18:56:15.422: Using Wayland display name 'wayland-1'
168171
mutter --wayland --headless --no-x11 --virtual-monitor 1024x768 &> "$TMPFILE" &
169-
echo "kill -9 $! || true" >> "$PID_FILE"
172+
PID=$!
173+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
174+
echo "${PID}" >> "${PID_FILE}"
170175
sleep 1s
171176
export WAYLAND_DISPLAY
172177
WAYLAND_DISPLAY=$(grep "Using Wayland display" "$TMPFILE" | cut -d"'" -f2)
@@ -176,25 +181,30 @@ function _setup_display_server() {
176181
echo "Running on X11:"
177182
echo "Starting Xvfb..."
178183
Xvfb -screen 0 1024x768x24 :33 &> /dev/null &
179-
echo "kill -9 $! || true" >> "$PID_FILE"
184+
PID=$!
185+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
186+
echo "${PID}" >> "${PID_FILE}"
180187
sleep 1
181188
echo "Starting Xephyr..."
182189
DISPLAY=:33 Xephyr :32 -screen 1024x768 &> /dev/null &
183-
echo "kill -9 $! || true" >> "$PID_FILE"
190+
PID=$!
191+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
192+
echo "${PID}" >> "${PID_FILE}"
184193
sleep 1
185194
echo "Starting metacity"
186195
metacity --display=:32 &> /dev/null &
187-
echo "kill -9 $! || true" >> "$PID_FILE"
196+
PID=$!
197+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
198+
echo "${PID}" >> "${PID_FILE}"
188199

189200
export DISPLAY=:32
190201
echo "export DISPLAY=\"$DISPLAY\"" >> "$ENV_FILE"
191202
fi
192203
}
193204

194205
function _setup_schema_and_gsettings() {
195-
local ENV_FILE PID_FILE
206+
local ENV_FILE
196207
ENV_FILE=$1
197-
PID_FILE=$2
198208

199209
# Install schema to temporary directory. This removes the build dependency on the keyman package.
200210
SCHEMA_DIR=$TEMP_DATA_DIR/glib-2.0/schemas
@@ -219,33 +229,41 @@ function _setup_schema_and_gsettings() {
219229
}
220230

221231
function _setup_ibus() {
222-
local ENV_FILE PID_FILE
232+
local ENV_FILE CLEANUP_FILE PID_FILE PID
223233
ENV_FILE=$1
224-
PID_FILE=$2
234+
CLEANUP_FILE=$2
235+
PID_FILE=$3
225236

237+
echo "Starting ibus-daemon..."
226238
#shellcheck disable=SC2086
227239
ibus-daemon ${ARG_VERBOSE-} --daemonize --panel=disable --address=unix:abstract="${TEMP_DATA_DIR}/test-ibus" ${IBUS_CONFIG-} &> /tmp/ibus-daemon.log
228-
echo "kill -9 $! || true" >> "$PID_FILE"
240+
PID=$(pgrep -f "${TEMP_DATA_DIR}/test-ibus")
241+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
242+
echo "${PID}" >> "${PID_FILE}"
229243
sleep 1s
230244

231245
IBUS_ADDRESS=$(ibus address)
232246
export IBUS_ADDRESS
233247

234248
echo "export IBUS_ADDRESS=\"$IBUS_ADDRESS\"" >> "$ENV_FILE"
235249

250+
echo "Starting ibus-engine-keyman..."
236251
#shellcheck disable=SC2086
237252
"${TOP_BINDIR}/src/ibus-engine-keyman" --testing ${ARG_VERBOSE-} &> /tmp/ibus-engine-keyman.log &
238-
echo "kill -9 $! || true" >> "$PID_FILE"
253+
PID=$!
254+
echo "kill -9 ${PID} || true" >> "$CLEANUP_FILE"
255+
echo "${PID}" >> "${PID_FILE}"
239256
sleep 1s
240-
241257
}
258+
242259
function setup() {
243-
local DISPLAY_SERVER ENV_FILE PID_FILE TESTBASEDIR TESTDIR
260+
local DISPLAY_SERVER ENV_FILE CLEANUP_FILE PID_FILE TESTBASEDIR TESTDIR
244261
DISPLAY_SERVER=$1
245262
ENV_FILE=$2
246-
PID_FILE=$3
263+
CLEANUP_FILE=$3
264+
PID_FILE=$4
247265

248-
_setup_init "${ENV_FILE}" "${PID_FILE}"
266+
_setup_init "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}"
249267

250268
TESTBASEDIR=${XDG_DATA_HOME:-$HOME/.local/share}/keyman
251269
TESTDIR=${TESTBASEDIR}/test_kmx
@@ -254,32 +272,33 @@ function setup() {
254272

255273
_generate_kmpjson "$TESTDIR"
256274

257-
_setup_test_dbus_server "${ENV_FILE}" "${PID_FILE}"
258-
_setup_display_server "${ENV_FILE}" "${PID_FILE}" "${DISPLAY_SERVER}"
259-
_setup_schema_and_gsettings "${ENV_FILE}" "${PID_FILE}"
260-
_setup_ibus "${ENV_FILE}" "${PID_FILE}"
275+
_setup_test_dbus_server "${ENV_FILE}" "${CLEANUP_FILE}"
276+
_setup_display_server "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}" "${DISPLAY_SERVER}"
277+
_setup_schema_and_gsettings "${ENV_FILE}"
278+
_setup_ibus "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}"
261279
}
262280

263281
function setup_display_server_only() {
264-
local DISPLAY_SERVER ENV_FILE PID_FILE TESTBASEDIR TESTDIR
282+
local DISPLAY_SERVER ENV_FILE CLEANUP_FILE PID_FILE TESTBASEDIR TESTDIR
265283
DISPLAY_SERVER=$1
266284
ENV_FILE=$2
267-
PID_FILE=$3
285+
CLEANUP_FILE=$3
286+
PID_FILE=$4
268287

269-
_setup_init "${ENV_FILE}" "${PID_FILE}"
270-
_setup_display_server "${ENV_FILE}" "${PID_FILE}" "${DISPLAY_SERVER}"
271-
_setup_schema_and_gsettings "${ENV_FILE}" "${PID_FILE}"
288+
_setup_init "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}"
289+
_setup_display_server "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}" "${DISPLAY_SERVER}"
290+
_setup_schema_and_gsettings "${ENV_FILE}"
272291
}
273292

274293
function cleanup() {
275-
local PID_FILE
276-
PID_FILE=$1
294+
local CLEANUP_FILE
295+
CLEANUP_FILE=$1
277296

278-
if [ -f "$PID_FILE" ]; then
297+
if [ -f "$CLEANUP_FILE" ]; then
279298
echo
280299
echo "# Shutting down processes..."
281-
bash "$PID_FILE" # > /dev/null 2>&1
282-
rm "$PID_FILE"
300+
bash "$CLEANUP_FILE" # > /dev/null 2>&1
301+
rm "$CLEANUP_FILE"
283302
echo "# Finished shutdown of processes."
284303
fi
285304
}
@@ -291,3 +310,28 @@ function exit_on_package_build() {
291310
exit 0
292311
fi
293312
}
313+
314+
function check_processes_running() {
315+
local DISPLAY_SERVER ENV_FILE CLEANUP_FILE PID_FILE PID MISSING
316+
DISPLAY_SERVER=$1
317+
ENV_FILE=$2
318+
CLEANUP_FILE=$3
319+
PID_FILE=$4
320+
MISSING=false
321+
322+
while read -r PID; do
323+
if [ -z "$PID" ]; then
324+
continue
325+
elif ! ps --no-headers --pid="$PID" > /dev/null; then
326+
MISSING=true
327+
break
328+
fi
329+
done < "${PID_FILE}"
330+
331+
if $MISSING; then
332+
echo "# Some background processes no longer running. Restarting..."
333+
echo "Some background processes no longer running. Restarting..." > /tmp/debug.output
334+
cleanup "${CLEANUP_FILE}" > /dev/null 2>&1
335+
setup "${DISPLAY_SERVER}" "${ENV_FILE}" "${CLEANUP_FILE}" "${PID_FILE}" > /dev/null 2>&1
336+
fi
337+
}

0 commit comments

Comments
 (0)