Files
go-socket/tests/06_send_message_reverse.sh
T

79 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Send message from user_two to the group, verify user_one receives it
source "$(dirname "$0")/config.sh"
TOKEN1=$(load_state "TOKEN1")
TOKEN2=$(load_state "TOKEN2")
GROUP_ID=$(load_state "GROUP_ID")
if [[ -z "$TOKEN1" || -z "$TOKEN2" || -z "$GROUP_ID" ]]; then
echo "ERROR: Missing state. Run previous scripts first."
echo " TOKEN1=$TOKEN1"
echo " TOKEN2=$TOKEN2"
echo " GROUP_ID=$GROUP_ID"
exit 1
fi
MESSAGE="Hello from user_two!"
echo "=== Sending message from user_two to group $GROUP_ID ==="
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
echo "[DEBUG] tmpdir: $TMPDIR"
echo "[DEBUG] TOKEN1: ${TOKEN1:0:20}..."
echo "[DEBUG] TOKEN2: ${TOKEN2:0:20}..."
echo "[DEBUG] GROUP_ID: $GROUP_ID"
# Receiver (user_one): authenticate then wait for messages
echo "[DEBUG] starting receiver (user_one)..."
{ echo '{"token":"'"$TOKEN1"'"}'; sleep 5; } \
| stdbuf -oL websocat ws://localhost:8080/ws > "$TMPDIR/received" 2>"$TMPDIR/recv_err" &
RECV_PID=$!
echo "[DEBUG] receiver PID: $RECV_PID"
sleep 0.5
# Sender (user_two): authenticate, send message
SEND_PAYLOAD='{"subject":'"$GROUP_ID"',"content":"'"$MESSAGE"'"}'
echo "[DEBUG] starting sender (user_two)..."
echo "[DEBUG] send payload: $SEND_PAYLOAD"
{ echo '{"token":"'"$TOKEN2"'"}'; sleep 0.5; echo "$SEND_PAYLOAD"; sleep 0.5; } \
| websocat ws://localhost:8080/ws > "$TMPDIR/send_out" 2>"$TMPDIR/send_err" &
SEND_PID=$!
echo "[DEBUG] sender PID: $SEND_PID"
echo "[DEBUG] waiting 2s for message delivery..."
sleep 2
echo "[DEBUG] killing connections..."
kill $RECV_PID $SEND_PID 2>/dev/null
wait $RECV_PID 2>/dev/null
RECV_EXIT=$?
wait $SEND_PID 2>/dev/null
SEND_EXIT=$?
echo "[DEBUG] receiver exit: $RECV_EXIT, sender exit: $SEND_EXIT"
RECV_ERR=$(cat "$TMPDIR/recv_err")
SEND_ERR=$(cat "$TMPDIR/send_err")
SEND_OUT=$(cat "$TMPDIR/send_out")
[[ -n "$RECV_ERR" ]] && echo "[DEBUG] receiver stderr: $RECV_ERR"
[[ -n "$SEND_ERR" ]] && echo "[DEBUG] sender stderr: $SEND_ERR"
[[ -n "$SEND_OUT" ]] && echo "[DEBUG] sender received: $SEND_OUT"
RESPONSE=$(cat "$TMPDIR/received")
echo "user_one received: $RESPONSE"
if [[ -z "$RESPONSE" ]]; then
echo "[DEBUG] EMPTY response - no data received by user_one"
echo "FAIL: message not received"
exit 1
fi
if echo "$RESPONSE" | grep -q "$MESSAGE"; then
echo "PASS"
else
echo "FAIL: message not received"
exit 1
fi