Files
mtusi2026lab2/student-tests/lifecycle.sh
T

63 lines
3.0 KiB
Bash

#!/bin/sh
# Lifecycle proof for the in-memory design:
# 1. create a user, a chat and a message, read the message back;
# 2. stop the container with SIGTERM and record how the process shut down;
# 3. start the same container again and show the old X-User-Id is gone (401).
# Usage: BASE_URL=http://localhost:8080 CONTAINER=mtusi-messenger-lab2 sh student-tests/lifecycle.sh
set -eu
BASE_URL=${BASE_URL:-http://localhost:8080}
CONTAINER=${CONTAINER:-mtusi-messenger-lab2}
HERE=$(dirname "$0")
json_field() { # json_field '<json>' key -> first string value of key
printf '%s' "$1" | sed -n "s/.*\"$2\":\"\([^\"]*\)\".*/\1/p" | head -n 1
}
echo "== 1. write and read before restart =="
user=$(curl -sS -X POST "$BASE_URL/api/v1/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"lifecycle_$(date +%s)\",\"display_name\":\"Lifecycle\"}")
user_id=$(json_field "$user" id)
[ -n "$user_id" ] || { echo "user was not created: $user" >&2; exit 1; }
chat=$(curl -sS -X POST "$BASE_URL/api/v1/chats" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \
-d "{\"title\":\"lifecycle\",\"member_ids\":[\"$user_id\"]}")
chat_id=$(json_field "$chat" id)
[ -n "$chat_id" ] || { echo "chat was not created: $chat" >&2; exit 1; }
message=$(curl -sS -X POST "$BASE_URL/api/v1/chats/$chat_id/messages" -H 'Content-Type: application/json' -H "X-User-Id: $user_id" \
-d '{"text":"before restart"}')
message_id=$(json_field "$message" id)
[ -n "$message_id" ] || { echo "message was not created: $message" >&2; exit 1; }
history=$(curl -sS "$BASE_URL/api/v1/chats/$chat_id/messages" -H "X-User-Id: $user_id")
case "$history" in
*"$message_id"*) echo "message $message_id is readable before restart" ;;
*) echo "message missing from history: $history" >&2; exit 1 ;;
esac
echo "== 2. SIGTERM via docker stop (grace 10s) =="
started=$(date +%s)
docker stop -t 10 "$CONTAINER" >/dev/null
finished=$(date +%s)
exit_code=$(docker inspect -f '{{.State.ExitCode}}' "$CONTAINER")
echo "docker stop took $((finished - started))s wall clock, container exit code $exit_code"
echo "last log lines:"
docker logs --tail 3 "$CONTAINER" 2>&1 | sed 's/^/ /'
if [ "$exit_code" != "0" ]; then
echo "process did not exit cleanly on SIGTERM" >&2
exit 1
fi
echo "== 3. restart the same container: state must be gone =="
if ! docker start "$CONTAINER" >/dev/null; then
echo "cannot start $CONTAINER again: run it without --rm so the stopped container survives docker stop" >&2
exit 1
fi
BASE_URL="$BASE_URL" sh "$HERE/wait-ready.sh" >/dev/null
me_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/users/me" -H "X-User-Id: $user_id")
chat_status=$(curl -s -o /dev/null -w '%{http_code}' "$BASE_URL/api/v1/chats/$chat_id" -H "X-User-Id: $user_id")
echo "GET /users/me with the old X-User-Id -> $me_status"
echo "GET /chats/$chat_id with the old X-User-Id -> $chat_status"
if [ "$me_status" != "401" ] || [ "$chat_status" != "401" ]; then
echo "expected 401 for both after restart" >&2
exit 1
fi
echo "PASS: state lived only in the process memory and was lost on restart"