1
0
mirror of https://github.com/coturn/coturn.git synced 2026-10-24 00:08:57 +03:00
Files
coturn/examples/run_tests_multiplex_peer.sh
Pavel Punsky 0feab35326 Make the multiplex-peer example tests request EVEN-PORT never, not ra… (#2070)
…ndomly

run_client drove turnutils_uclient with -c only. In clnet_allocate, -c
leaves the EVEN-PORT slot to be picked at random between 0 and -1, and
multiplex-peer strictly rejects EVEN-PORT with error 508, so roughly
half of all attempts were thrown away. The script compensated with an
eight-attempt retry loop, which still failed outright about once in 256
per run_client call and about once in 50 per script run - observed on
https://github.com/coturn/coturn/actions/runs/31279481991
(amazonlinux:2023), where all eight attempts of the first UDP case drew
EVEN-PORT.

Pass --no-even-port, which forces the slot to -1 unconditionally, and
drop the retry loop it makes dead. Nothing is lost in coverage:
EVEN-PORT under multiplex-peer is defined to be a 508, so the random
draw only ever decided whether a run was wasted. A retry that can no
longer fire for its stated reason would silently swallow a genuine
regression.

expect_peer_endpoint_limit already passed --no-even-port for the same
reason.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-31 18:21:15 -07:00

185 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
turnserver_pid=""
peer_pid=""
turnserver_log=""
cleanup() {
if [ -n "$peer_pid" ]; then
kill "$peer_pid" 2>/dev/null || true
wait "$peer_pid" 2>/dev/null || true
fi
if [ -n "$turnserver_pid" ]; then
kill "$turnserver_pid" 2>/dev/null || true
wait "$turnserver_pid" 2>/dev/null || true
fi
if [ -n "$turnserver_log" ]; then
rm -f "$turnserver_log"
fi
}
trap cleanup EXIT
# Detect cmake build and adjust path.
BINDIR="../bin"
if [ ! -f "$BINDIR/turnserver" ]; then
BINDIR="../build/bin"
fi
if [ ! -x "$BINDIR/turnserver" ]; then
echo "Cannot find turnserver in ../bin or ../build/bin"
exit 1
fi
MULTIPLEX_PEER_PORT="${MULTIPLEX_PEER_PORT:-35000}"
TURNSERVER_EXTRA_ARGS=("--multiplex-peer" "--multiplex-peer-port=$MULTIPLEX_PEER_PORT")
if [ "$(uname -s)" = "Linux" ]; then
TURNSERVER_EXTRA_ARGS+=("--udp-recvmmsg")
echo 'Using TURNSERVER_EXTRA_ARGS="--udp-recvmmsg"'
fi
turnserver_log="$(mktemp "${TMPDIR:-/tmp}/turnserver-multiplex-peer.XXXXXX.log")"
print_turnserver_log_tail() {
echo "Last turnserver log lines:"
tail -n 40 "$turnserver_log" 2>/dev/null || true
}
run_client() {
local name="$1"
local peer_port="$2"
local expected_bytes="$3"
local output
local rc=0
shift 3
echo "Running $name"
"$BINDIR/turnutils_peer" -p "$peer_port" -L 127.0.0.1 -L ::1 -L 0.0.0.0 > /dev/null &
peer_pid="$!"
sleep 1
output=$("$BINDIR/turnutils_uclient" "$@" 2>&1) || rc=$?
if printf '%s\n' "$output" |
grep -q "start_mclient: tot_send_bytes ~ $expected_bytes, tot_recv_bytes ~ $expected_bytes"; then
kill "$peer_pid" 2>/dev/null || true
wait "$peer_pid" 2>/dev/null || true
peer_pid=""
echo OK
return 0
fi
kill "$peer_pid" 2>/dev/null || true
wait "$peer_pid" 2>/dev/null || true
peer_pid=""
echo FAIL
printf '%s\n' "$output"
print_turnserver_log_tail
if [ "$rc" -ne 0 ]; then
return "$rc"
fi
return 1
}
start_turnserver() {
: > "$turnserver_log"
"$BINDIR/turnserver" \
--use-auth-secret \
--sock-buf-size=1048576 \
--static-auth-secret=secret \
--realm=north.gov \
-L 127.0.0.1 \
-E 127.0.0.1 \
--allow-loopback-peers \
"${TURNSERVER_EXTRA_ARGS[@]}" \
"$@" \
-v \
--dtls \
--cert ca/turn_server_cert.pem \
--pkey ca/turn_server_pkey.pem \
--simple-log \
--log-file "$turnserver_log" \
> /dev/null &
turnserver_pid="$!"
sleep 5
}
stop_turnserver() {
if [ -n "$turnserver_pid" ]; then
kill "$turnserver_pid" 2>/dev/null || true
wait "$turnserver_pid" 2>/dev/null || true
turnserver_pid=""
fi
}
# The client permits the RTCP peer port too, so one peer is already two
# endpoints and a cap of 1 is always exceeded; --no-even-port keeps the other
# 508 on this path (EVEN-PORT) out of the picture.
expect_peer_endpoint_limit() {
local peer_port="$1"
local output
local rc=0
echo "Running turn client UDP with two peer endpoints against --multiplex-peer-max-peers=1"
"$BINDIR/turnutils_peer" -p "$peer_port" -L 127.0.0.1 > /dev/null &
peer_pid="$!"
sleep 1
output=$(timeout 60 "$BINDIR/turnutils_uclient" --no-even-port -e 127.0.0.1 -r "$peer_port" \
-X -g -u user -W secret 127.0.0.1 2>&1) || rc=$?
kill "$peer_pid" 2>/dev/null || true
wait "$peer_pid" 2>/dev/null || true
peer_pid=""
if printf '%s\n' "$output" | grep -q "error 508" &&
grep -q "peer-endpoint limit" "$turnserver_log"; then
echo "OK (second peer endpoint refused with 508)"
return 0
fi
echo FAIL
printf '%s\n' "$output"
print_turnserver_log_tail
if [ "$rc" -ne 0 ]; then
return "$rc"
fi
return 1
}
echo "Running turnserver in multiplex-peer mode on base port $MULTIPLEX_PEER_PORT"
start_turnserver
# Multiplex-peer rejects EVEN-PORT with 508, and -c alone leaves the client
# picking EVEN-PORT at random, so --no-even-port is what makes these runs
# deterministic.
run_client "turn client UDP" 3480 500 -c --no-even-port -e 127.0.0.1 -r 3480 -X -g -u user -W secret 127.0.0.1
run_client "turn client TCP" 3482 500 -c --no-even-port -t -e 127.0.0.1 -r 3482 -X -g -u user -W secret 127.0.0.1
run_client "turn client TLS" 3484 500 -c --no-even-port -t -S -e 127.0.0.1 -r 3484 -X -g -u user -W secret 127.0.0.1
run_client "turn client DTLS" 3490 500 -c --no-even-port -S -e 127.0.0.1 -r 3490 -X -g -u user -W secret 127.0.0.1
sleep 2
stop_turnserver
echo "Restarting turnserver with --multiplex-peer-max-peers=4"
start_turnserver --multiplex-peer-max-peers=4
run_client "turn client UDP (peer-endpoint cap 4)" 3492 500 -c --no-even-port -e 127.0.0.1 -r 3492 -X -g -u user \
-W secret 127.0.0.1
stop_turnserver
echo "Restarting turnserver with --multiplex-peer-max-peers=1"
start_turnserver --multiplex-peer-max-peers=1
# The shared relay socket, and with it the demux table the cap applies to,
# only exists where multiplex-peer is supported.
if grep -q "multiplex-peer: thread 0" "$turnserver_log"; then
expect_peer_endpoint_limit 3494
else
echo "SKIP: multiplex-peer is not active on this platform; peer-endpoint cap not exercised"
fi
sleep 2