Skip to content

Commit

Permalink
Add domain_socket_stress_test.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
bell-db committed Sep 22, 2024
1 parent d1a8783 commit ee9e233
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions domain_socket_stress_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash

BYTE_LENGTH="$1"
CLIENT_MODE="${2:-nc}"

TEST_FILE_PATH="/tmp/domain_socket_test_file"
SOCKET_PATH="/tmp/domain_socket_test.sck"
TEST_RESULT_PATH="$TEST_FILE_PATH.copy"
dd if=/dev/urandom of="$TEST_FILE_PATH" bs=1 count="$BYTE_LENGTH" 2>/dev/null

test_socket() {
# Start a process to consume the data from the socket
(socat UNIX-LISTEN:"$SOCKET_PATH",unlink-close EXEC:"/bin/cat" && echo "Completed echoing random bytes from the socket") &
SERVER_PID=$!
echo "Starting the server (PID: $SERVER_PID)"

# Wait for the socket file to be created so that the server has started
while [ ! -e "$SOCKET_PATH" ]; do
sleep 0.001
done
echo "The server has started and is listening to the socket (PID: $SERVER_PID)"

# `nc` can fail even if we wait for another second to ensure the server has started
# sleep 1

# Start dumping random bytes to the socket in the background
if [[ "$CLIENT_MODE" == "nc" ]]; then
(nc -U "$SOCKET_PATH" < "$TEST_FILE_PATH" > "$TEST_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
elif [[ "$CLIENT_MODE" == "ncat" ]]; then
(ncat -U "$SOCKET_PATH" < "$TEST_FILE_PATH" > "$TEST_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
elif [[ "$CLIENT_MODE" == "socat" ]]; then
(socat - "UNIX-CONNECT:$SOCKET_PATH" < "$TEST_FILE_PATH" > "$TEST_RESULT_PATH" && echo "Completed dumping random bytes to the socket") &
else
echo "Invalid writer mode: $CLIENT_MODE"
exit 1
fi
CLIENT_PID=$!
echo "Started dumping random bytes to the socket (PID: $CLIENT_PID)"

# Ensure the client process is killed
wait $CLIENT_PID 2>/dev/null
echo "The client process has stopped (PID: $CLIENT_PID)"

# Ensure the server process is killed
wait $SERVER_PID 2>/dev/null
echo "The server process has stopped (PID: $SERVER_PID)"

# Check the size of the data read from the socket
DATA_SIZE=$(wc -c < "$TEST_RESULT_PATH")
if [ "$DATA_SIZE" -ne "$BYTE_LENGTH" ]; then
echo "Error: Expected $BYTE_LENGTH bytes, but read $DATA_SIZE bytes"
exit 1
else
echo "Successfully read $BYTE_LENGTH bytes from the socket"
fi

rm "$TEST_RESULT_PATH"
}

rm -f "$SOCKET_PATH"

# Repeat the process
counter=0;
while test_socket; do
((counter++)); echo "Iterations completed: $counter";
done
echo "Command failed after $counter successful iterations."

0 comments on commit ee9e233

Please sign in to comment.