dotnet-cqrs/Svrnty.Sample/test-rabbitmq-integration.sh

121 lines
3.5 KiB
Bash
Executable File

#!/bin/bash
# Test script for RabbitMQ cross-service event streaming integration
# This script demonstrates Phase 4 functionality
set -e
echo "=== RabbitMQ Cross-Service Event Streaming Test ==="
echo ""
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${YELLOW}Step 1: Check if infrastructure is running${NC}"
echo "Checking PostgreSQL..."
if ! docker ps | grep -q svrnty-postgres; then
echo "PostgreSQL is not running. Starting infrastructure..."
docker-compose up -d
echo "Waiting for services to be healthy..."
sleep 10
else
echo "Infrastructure is already running"
fi
echo ""
echo -e "${YELLOW}Step 2: Build the sample project${NC}"
dotnet build Svrnty.Sample.csproj
echo ""
echo -e "${YELLOW}Step 3: Start the sample application${NC}"
echo "Starting application in background..."
dotnet run --no-build > /tmp/svrnty-sample.log 2>&1 &
APP_PID=$!
echo "Application PID: $APP_PID"
echo "Waiting for application to start (10 seconds)..."
sleep 10
echo ""
echo -e "${YELLOW}Step 4: Test command execution via HTTP API${NC}"
echo "Creating a user (this will emit UserAddedEvent to RabbitMQ)..."
RESPONSE=$(curl -s -X POST http://localhost:6001/api/command/addUser \
-H "Content-Type: application/json" \
-d '{
"name": "Alice Johnson",
"email": "alice@example.com",
"age": 30
}')
echo "Response: $RESPONSE"
USER_ID=$(echo $RESPONSE | grep -o '"result":[0-9]*' | grep -o '[0-9]*')
echo "Created user with ID: $USER_ID"
echo ""
echo -e "${YELLOW}Step 5: Check RabbitMQ for published events${NC}"
echo "Events should be visible in RabbitMQ Management UI:"
echo " http://localhost:15672 (guest/guest)"
echo ""
echo "Check the following:"
echo " - Exchanges: svrnty-sample.user-events"
echo " - Queues: svrnty-sample.email-service"
echo " - Message rate should show 1 message published"
echo ""
echo -e "${YELLOW}Step 6: Check application logs${NC}"
echo "Looking for RabbitMQ event consumption logs..."
sleep 2
if grep -q "RABBITMQ.*Received external event" /tmp/svrnty-sample.log; then
echo -e "${GREEN}✓ RabbitMQ consumer received the event!${NC}"
grep "RABBITMQ" /tmp/svrnty-sample.log | tail -5
else
echo "No RabbitMQ events found in logs yet. Waiting a bit more..."
sleep 3
grep "RABBITMQ" /tmp/svrnty-sample.log | tail -5 || echo "Still no events"
fi
echo ""
echo -e "${YELLOW}Step 7: Test invitation workflow${NC}"
echo "Inviting a user (this will emit UserInvitedEvent to RabbitMQ)..."
INVITE_RESPONSE=$(curl -s -X POST http://localhost:6001/api/command/inviteUser \
-H "Content-Type: application/json" \
-d '{
"email": "bob@example.com",
"inviterName": "Alice Johnson",
"message": "Join our platform!"
}')
echo "Response: $INVITE_RESPONSE"
echo ""
echo -e "${GREEN}Test completed!${NC}"
echo ""
echo "What happened:"
echo "1. UserAddedEvent was emitted by AddUserCommand"
echo "2. Event was published to RabbitMQ exchange: svrnty-sample.user-events"
echo "3. RabbitMQEventConsumerBackgroundService consumed the event from RabbitMQ"
echo "4. Event was also consumed by EventConsumerBackgroundService (internal)"
echo ""
echo "To view all logs:"
echo " tail -f /tmp/svrnty-sample.log"
echo ""
echo "To stop the application:"
echo " kill $APP_PID"
echo ""
echo "To stop infrastructure:"
echo " docker-compose down"
echo ""
# Optionally stop the app
read -p "Stop the application now? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
kill $APP_PID
echo "Application stopped"
fi