blob: a9d420ca7097c2dca692bbddad39ab74fccc996e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#!/usr/bin/env bash
# ---------------------------------------------------------------
# Shared helpers for OPC UA discovery integration tests.
#
# Source this file from test scripts:
# source "$(dirname "$0")/test_helpers.sh"
# ---------------------------------------------------------------
FAILURES=0
# Waits up to 5 seconds for a process to listen on a TCP port.
# Exits immediately if the process dies before the port opens.
#
# wait_for_port <port> <pid> <label>
wait_for_port() {
local port="$1" pid="$2" label="$3" i=0
while [ $i -lt 50 ]; do
if ! kill -0 "$pid" 2>/dev/null; then
echo "FAIL: $label exited prematurely"
exit 1
fi
if ss -tlnp 2>/dev/null | grep -q ":${port} "; then
return 0
fi
sleep 0.1
i=$((i + 1))
done
echo "FAIL: $label did not listen on port $port within 5 s"
exit 1
}
# Records a PASS/FAIL result. Increments FAILURES on failure.
#
# check <label> <exit-code>
check() {
local label="$1" result="$2"
if [ "$result" -eq 0 ]; then
echo "PASS: $label"
else
echo "FAIL: $label"
FAILURES=$((FAILURES + 1))
fi
}
# Asserts that the given TCP ports are not already in use.
# Exits immediately if any port is occupied.
#
# assert_ports_free <port> [port...]
assert_ports_free() {
for port in "$@"; do
if ss -tlnp 2>/dev/null | grep -q ":${port} "; then
echo "FAIL: port $port is already in use"
exit 1
fi
done
}
|