Skip to content

Commit

Permalink
Improve handling of application startup and shutdown for tests and ru…
Browse files Browse the repository at this point in the history
…nning locally
  • Loading branch information
davidje13 committed Oct 28, 2023
1 parent c042269 commit 1bf0306
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 42 deletions.
72 changes: 38 additions & 34 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
cache: npm
cache-dependency-path: 'src/*/package-lock.json'
- name: Build and Test
run: PARALLEL_BUILD=false PARALLEL_E2E=false E2E_LAUNCH_DELAY=5 EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 npm test
run: PARALLEL_BUILD=false PARALLEL_E2E=false EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 npm test
- name: Bundle
run: |
cd build
Expand Down Expand Up @@ -57,35 +57,38 @@ jobs:
with:
name: refacto
- name: Unpack
run: |
tar -xf build.tar.gz
rm build.tar.gz
run: tar -xf build.tar.gz && rm build.tar.gz
- name: Smoke Test
run: |
npm install --omit=dev
./index.js >output.log 2>&1 & PID=$!
sleep 5
if ! grep 'Available at' <output.log >/dev/null; then
cat output.log
echo "Application failed to launch" >&2
exit 1
fi
set -e;
npm install --omit=dev;
./index.js > output.log 2>&1 & APP_PID="$!";
while ! grep 'Available at' < output.log > /dev/null 2>&1; do
if ! ps -p "$APP_PID" > /dev/null; then
APP_EXIT_CODE="$(wait "$APP_PID" > /dev/null; echo "$?")";
cat output.log;
echo "Application failed to launch (exit code: $APP_EXIT_CODE).";
false;
fi;
sleep 0.1;
done;
wget localhost:5000 -O test-index.html
if ! grep '<title>Refacto</title>' <test-index.html >/dev/null; then
cat output.log
echo "Unexpected main page response" >&2
cat test-index.html
exit 1
wget localhost:5000 -O test-index.html;
if ! grep '<title>Refacto</title>' < test-index.html > /dev/null; then
cat output.log;
echo "Unexpected main page response" >&2;
cat test-index.html;
false;
fi;
kill -2 "$PID"
sleep 5
if ! grep 'Shutdown complete' <output.log >/dev/null; then
cat output.log
echo "Application failed to shut down" >&2
exit 1
fi
kill -2 "$APP_PID";
wait "$APP_PID";
if ! grep 'Shutdown complete' < output.log > /dev/null; then
cat output.log;
echo "Application failed to shut down" >&2;
false;
fi;
create_release:
needs:
Expand All @@ -105,21 +108,22 @@ jobs:
COMMIT: ${{ github.sha }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
NAME=$(date -u '+%Y%m%d-%H%M%S')
echo "Creating release $NAME..."
set -e;
NAME="$(date -u '+%Y%m%d-%H%M%S')";
echo "Creating release $NAME...";
wget -S \
--header='Accept: application/vnd.github.v3+json' \
--header="Authorization: token $GITHUB_TOKEN" \
--post-data="$(jq -n --arg n "$NAME" --arg c "$COMMIT" '{tag_name: $n, target_commitish: $c}')" \
"$API_BASE/releases" -O release.json
cat release.json
echo "Uploading bundle..."
UPLOAD_URL=$(jq -r '.upload_url' <release.json | sed 's/{[^}]*}//')
"$API_BASE/releases" -O release.json;
cat release.json;
echo "Uploading bundle...";
UPLOAD_URL="$(jq -r '.upload_url' < release.json | sed 's/{[^}]*}//')";
wget -S \
--header='Accept: application/vnd.github.v3+json' \
--header="Authorization: token $GITHUB_TOKEN" \
--header='Content-Type: application/gzip' \
--post-file='build.tar.gz' \
"$UPLOAD_URL?name=build.tar.gz" -O release-file.json
cat release-file.json
echo "Done."
"$UPLOAD_URL?name=build.tar.gz" -O release-file.json;
cat release-file.json;
echo "Done.";
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ addons:
chrome: stable
firefox: latest
env:
- PARALLEL_BUILD=false PARALLEL_E2E=false E2E_LAUNCH_DELAY=5 EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000
- PARALLEL_BUILD=false PARALLEL_E2E=false EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000
cache:
directories:
- src/backend/node_modules
Expand Down
6 changes: 3 additions & 3 deletions ci/gitlab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ before_script:
node:18:
image: node:18
script:
- PARALLEL_BUILD=false PARALLEL_E2E=false E2E_LAUNCH_DELAY=5 EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test
- PARALLEL_BUILD=false PARALLEL_E2E=false EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test

node:20:
image: node:20
script:
- PARALLEL_BUILD=false PARALLEL_E2E=false E2E_LAUNCH_DELAY=5 EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test
- PARALLEL_BUILD=false PARALLEL_E2E=false EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test

node:21:
image: node:21
script:
- PARALLEL_BUILD=false PARALLEL_E2E=false E2E_LAUNCH_DELAY=5 EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test
- PARALLEL_BUILD=false PARALLEL_E2E=false EXPLICIT_WAIT_TIMEOUT=20000 TEST_TIMEOUT=60000 DOCKER=true npm test

cache:
- key: { files: [src/backend/package-lock.json] }
Expand Down
19 changes: 16 additions & 3 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,22 @@ if [[ -z "$TARGET_HOST" ]]; then
"$BUILDDIR/index.js" \
> "$E2E_WORKDIR/app.log" 2>&1 & APP_PID="$!";

trap "kill '$APP_PID'; false" EXIT;
trap "kill '$APP_PID'; wait '$APP_PID' > /dev/null && false" EXIT;

# Wait for startup
sleep "${E2E_LAUNCH_DELAY:-1}";
while ! grep 'Available at' < "$E2E_WORKDIR/app.log" > /dev/null 2>&1; do
if ! ps -p "$APP_PID" > /dev/null; then
trap - EXIT;
APP_EXIT_CODE="$(wait "$APP_PID" > /dev/null; echo "$?")";
echo;
echo 'Application logs:';
sed "s/^/> /" < "$E2E_WORKDIR/app.log";
echo;
echo "Failed to start server for E2E tests (exit code: $APP_EXIT_CODE).";
false;
fi;
sleep 0.1;
done;

TARGET_HOST="http://localhost:$PORT/";
fi;
Expand Down Expand Up @@ -91,7 +103,8 @@ fi;

# Shutdown app server
if [[ -n "$APP_PID" ]]; then
kill "$APP_PID";
kill -2 "$APP_PID";
wait "$APP_PID" || true;
trap - EXIT;
fi;

Expand Down
2 changes: 1 addition & 1 deletion scripts/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ set +e;
PORT="$APP_PORT" \
npm --prefix="$BASEDIR/src/frontend" start --quiet & APP_PID="$!";

trap "kill '$APP_PID'" EXIT;
trap "kill '$APP_PID'; wait '$APP_PID' > /dev/null" EXIT;

echo 'Running backend...';

Expand Down

0 comments on commit 1bf0306

Please sign in to comment.