Skip to content

Commit

Permalink
test: add devserver test logging, retry instead of wait for rebuild (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard authored Apr 28, 2023
1 parent 6392c5c commit bfd0ab3
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 76 deletions.
100 changes: 62 additions & 38 deletions e2e/js_run_devserver/multirun_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
set -o errexit -o nounset -o pipefail

BZLMOD_FLAG="${BZLMOD_FLAG:-}"
PORT1="$1"
PORT2="$2"
TARGET="$3"

# sedi makes `sed -i` work on both OSX & Linux
# See https://stackoverflow.com/questions/2320564/i-need-my-sed-i-command-for-in-place-editing-to-work-with-both-gnu-sed-and-bsd
Expand All @@ -14,9 +17,9 @@ _sedi () {
sed "${sedi[@]}" "$@"
}

echo "$$: TEST - $0: $1"
echo "$$: TEST - $0: $TARGET"

./node_modules/.bin/ibazel run "$1" "$BZLMOD_FLAG" 2>&1 &
./node_modules/.bin/ibazel run "$TARGET" "$BZLMOD_FLAG" 2>&1 &
ibazel_pid="$!"

function _exit {
Expand All @@ -28,105 +31,126 @@ function _exit {
}
trap _exit EXIT

echo "$$: Waiting for $1 devserver to launch on 8080..."
echo "$$: Waiting for $TARGET devserver to launch on $PORT1..."

# Wait for $PORT1 to start the http server
n=0
while ! nc -z localhost 8080; do
while ! nc -z localhost $PORT1; do
if [ $n -gt 100 ]; then
echo "$$: ERROR: Expected http://localhost:8080 to be available"
echo "$$: ERROR: Expected http://localhost:$PORT1 to be available"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

echo "$$: Waiting for $1 devserver to launch on 8081..."
echo "$$: Waiting for $TARGET devserver to launch on $PORT2..."

# Wait for $PORT2 to start the http server
n=0
while ! nc -z localhost 8081; do
while ! nc -z localhost $PORT2; do
if [ $n -gt 100 ]; then
echo "$$: ERROR: Expected http://localhost:8081 to be available"
echo "$$: ERROR: Expected http://localhost:$PORT2 to be available"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done


echo "$$: Waiting 5 seconds for devservers to settle..."
sleep 5

# Verify the initial state of $PORT1 and $PORT2
echo "$$: Devservers ready"

if ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to contain 'My first website'"
if ! curl http://localhost:$PORT1/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:$PORT1/index.html to contain 'My first website'"
exit 1
fi

if ! curl http://localhost:8081/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:8081/index.html to contain 'My first website'"
if ! curl http://localhost:$PORT2/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:$PORT2/index.html to contain 'My first website'"
exit 1
fi

if ! curl http://localhost:8080/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:8080/other.html to contain 'My other website'"
if ! curl http://localhost:$PORT1/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:$PORT1/other.html to contain 'My other website'"
exit 1
fi

if ! curl http://localhost:8081/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:8081/other.html to contain 'My other website'"
if ! curl http://localhost:$PORT2/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:$PORT2/other.html to contain 'My other website'"
exit 1
fi

if curl http://localhost:8080/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:8080/new.html to fail with 404"
if curl http://localhost:$PORT1/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:$PORT1/new.html to fail with 404"
exit 1
fi

if curl http://localhost:8081/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:8081/new.html to fail with 404"
if curl http://localhost:$PORT2/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:$PORT2/new.html to fail with 404"
exit 1
fi

if curl http://localhost:8080/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to NOT contain 'A second line'"
if curl http://localhost:$PORT1/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:$PORT1/index.html to NOT contain 'A second line'"
exit 1
fi

if curl http://localhost:8081/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:8081/index.html to NOT contain 'A second line'"
if curl http://localhost:$PORT2/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:$PORT2/index.html to NOT contain 'A second line'"
exit 1
fi

echo "$$: <div>A second line</div>" >> src/index.html
echo "<div>A second line</div>" >> src/index.html

# Wait for $PORT1 to show the updated file
n=0
while ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "A second line"; do
while ! curl http://localhost:$PORT1/index.html --fail 2>/dev/null | grep "A second line"; do
if [ $n -gt 30 ]; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to contain 'A second line'"
echo "$$: ERROR: Expected http://localhost:$PORT1/index.html to contain 'A second line'"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

if ! curl http://localhost:8081/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:8081/index.html to contain 'A second line'"
exit 1
fi
# Wait for $PORT2 to show the updated file
n=0
while ! curl http://localhost:$PORT2/index.html --fail 2>/dev/null | grep "A second line"; do
if [ $n -gt 30 ]; then
echo "$$: ERROR: Expected http://localhost:$PORT2/index.html to contain 'A second line'"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

echo "$$: <div>A new file</div>" > src/new.html
echo "<div>A new file</div>" > src/new.html
_sedi 's#"other.html"#"other.html", "new.html"#' src/BUILD.bazel

# Wait for $PORT1 to show the new file
n=0
while ! curl http://localhost:8080/new.html --fail 2>/dev/null | grep "A new file"; do
while ! curl http://localhost:$PORT1/new.html --fail 2>/dev/null | grep "A new file"; do
if [ $n -gt 30 ]; then
echo "$$: ERROR: Expected http://localhost:8080/new.html to contain 'A new file'"
echo "$$: ERROR: Expected http://localhost:$PORT1/new.html to contain 'A new file'"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

if ! curl http://localhost:8081/new.html --fail 2>/dev/null | grep "A new file"; then
echo "$$: ERROR: Expected http://localhost:8080/new.html to contain 'A new file'"
exit 1
fi
# Wait for $PORT2 to show the new file
n=0
while ! curl http://localhost:$PORT2/new.html --fail 2>/dev/null | grep "A new file"; do
if [ $n -gt 30 ]; then
echo "$$: ERROR: Expected http://localhost:$PORT2/new.html to contain 'A new file'"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

echo "$$: All tests passed"
57 changes: 32 additions & 25 deletions e2e/js_run_devserver/serve_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
set -o errexit -o nounset -o pipefail

BZLMOD_FLAG="${BZLMOD_FLAG:-}"
PORT="$1"
TARGET="$2"

# sedi makes `sed -i` work on both OSX & Linux
# See https://stackoverflow.com/questions/2320564/i-need-my-sed-i-command-for-in-place-editing-to-work-with-both-gnu-sed-and-bsd
Expand All @@ -14,9 +16,9 @@ _sedi () {
sed "${sedi[@]}" "$@"
}

echo "$$: TEST - $0: $1"
echo "$$: TEST - $0: $TARGET"

./node_modules/.bin/ibazel run "$1" "$BZLMOD_FLAG" 2>&1 &
./node_modules/.bin/ibazel run "$TARGET" "$BZLMOD_FLAG" 2>&1 &
ibazel_pid="$!"

function _exit {
Expand All @@ -28,60 +30,65 @@ function _exit {
}
trap _exit EXIT

echo "$$: Waiting for $1 devserver to launch on 8080..."
echo "$$: Waiting for $TARGET devserver to launch on $PORT..."

# Wait for $PORT to start the http server
n=0
while ! nc -z localhost 8080; do
while ! nc -z localhost $PORT; do
if [ $n -gt 100 ]; then
echo "$$: ERROR: Expected http://localhost:8080 to be available"
echo "$$: ERROR: Expected http://localhost:$PORT to be available"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

echo "$$: Waiting 5 seconds for devservers to settle..."
echo "$$: Waiting 5 seconds for devserver to settle..."
sleep 5

echo "$$: Devserver ready"

if ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to contain 'My first website'"
if ! curl http://localhost:$PORT/index.html --fail 2>/dev/null | grep "My first website"; then
echo "$$: ERROR: Expected http://localhost:$PORT/index.html to contain 'My first website'"
exit 1
fi

if ! curl http://localhost:8080/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:8080/other.html to contain 'My other website'"
if ! curl http://localhost:$PORT/other.html --fail 2>/dev/null | grep "My other website"; then
echo "$$: ERROR: Expected http://localhost:$PORT/other.html to contain 'My other website'"
exit 1
fi

if curl http://localhost:8080/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:8080/new.html to fail with 404"
if curl http://localhost:$PORT/new.html --fail 2>/dev/null; then
echo "$$: ERROR: Expected http://localhost:$PORT/new.html to fail with 404"
exit 1
fi

if curl http://localhost:8080/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to NOT contain 'A second line'"
if curl http://localhost:$PORT/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:$PORT/index.html to NOT contain 'A second line'"
exit 1
fi

echo "$$: <div>A second line</div>" >> src/index.html
echo "<div>A second line</div>" >> src/index.html

echo "$$: Waiting 5 seconds for ibazel rebuild after change to src/index.html..."
sleep 5

if ! curl http://localhost:8080/index.html --fail 2>/dev/null | grep "A second line"; then
echo "$$: ERROR: Expected http://localhost:8080/index.html to contain 'A second line'"
exit 1
fi
# Wait for $PORT to show the updated file
n=0
while ! curl http://localhost:$PORT/index.html --fail 2>/dev/null | grep "A second line"; do
if [ $n -gt 30 ]; then
echo "$$: ERROR: Expected http://localhost:$PORT/index.html to contain 'A second line'"
exit 1
fi
sleep 1 # wait before check again
((n=n+1))
done

echo "$$: <div>A new file</div>" > src/new.html
echo "<div>A new file</div>" > src/new.html
_sedi 's#"other.html"#"other.html", "new.html"#' src/BUILD.bazel

# Wait for $PORT to show the new file
n=0
while ! curl http://localhost:8080/new.html --fail 2>/dev/null | grep "A new file"; do
while ! curl http://localhost:$PORT/new.html --fail 2>/dev/null | grep "A new file"; do
if [ $n -gt 60 ]; then
echo "$$: ERROR: Expected http://localhost:8080/new.html to contain 'A new file'"
echo "$$: ERROR: Expected http://localhost:$PORT/new.html to contain 'A new file'"
exit 1
fi
sleep 1 # wait before check again
Expand Down
14 changes: 14 additions & 0 deletions e2e/js_run_devserver/src/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ js_run_devserver(
# can be used with `multirun` below.
command(
name = "serve",
args = [
"-p",
"8080",
],
arguments = ["."],
command = ":devserver",
)
Expand Down Expand Up @@ -72,6 +76,10 @@ js_run_devserver(
# can be used with `multirun` below.
command(
name = "serve_alt",
args = [
"-p",
"8081",
],
arguments = ["http_root"],
command = ":devserver_alt",
)
Expand Down Expand Up @@ -103,6 +111,7 @@ js_library(
# Another example but this time using a simple custom http-server js_library
js_run_devserver(
name = "serve_simple",
args = ["8082"],
chdir = package_name(),
command = "./simple.js",
data = [
Expand All @@ -115,6 +124,7 @@ js_run_devserver(
# Now the js_binary variant
js_binary(
name = "simple_bin",
args = ["8083"],
data = [
"//:node_modules/chalk",
],
Expand All @@ -124,6 +134,7 @@ js_binary(
# Another example but this time using a simple custom http-server js_binary
js_run_devserver(
name = "serve_simple_bin",
args = ["8083"],
chdir = package_name(),
data = [
":web_files",
Expand All @@ -147,6 +158,7 @@ js_library(
# Another example but this time using a naughty custom http-server js_library
js_run_devserver(
name = "serve_naughty",
args = ["8090"],
chdir = package_name(),
command = "./naughty.js",
data = [
Expand All @@ -160,6 +172,7 @@ js_run_devserver(
# Now the js_binary variant
js_binary(
name = "naughty_bin",
args = ["8091"],
data = [
"//:node_modules/chalk",
],
Expand All @@ -169,6 +182,7 @@ js_binary(
# Another example but this time using a naughty custom http-server js_binary
js_run_devserver(
name = "serve_naughty_bin",
args = ["8091"],
chdir = package_name(),
data = [
":web_files",
Expand Down
Loading

0 comments on commit bfd0ab3

Please sign in to comment.