Skip to content

Commit

Permalink
add tests that uses nodejs sidecar
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 11, 2025
1 parent c2dcc1e commit cb624a0
Show file tree
Hide file tree
Showing 5 changed files with 1,314 additions and 18 deletions.
86 changes: 68 additions & 18 deletions build/wd_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,75 @@ def _wd_test_impl(ctx):
if is_windows:
# Batch script executables must end with ".bat"
executable = ctx.actions.declare_file("%s_wd_test.bat" % ctx.label.name)
ctx.actions.write(
output = executable,
# PowerShell correctly handles forward slashes in executable paths generated by Bazel (e.g. "bazel-bin/src/workerd/server/workerd.exe")
content = "powershell -Command \"%*\" `-dTEST_TMPDIR=$ENV:TEST_TMPDIR\r\n",
is_executable = True,
)
content = """
@echo off
setlocal EnableDelayedExpansion
REM Start sidecar if specified
if not "$(SIDECAR)" == "" (
start /b "" "$(SIDECAR)" > nul 2>&1
set SIDECAR_PID=!ERRORLEVEL!
timeout /t 1 > nul
)
REM Run the actual test
powershell -Command "%*" `-dTEST_TMPDIR=$ENV:TEST_TMPDIR
set TEST_EXIT=!ERRORLEVEL!
REM Cleanup sidecar if it was started
if defined SIDECAR_PID (
taskkill /F /PID !SIDECAR_PID! > nul 2>&1
)
exit /b !TEST_EXIT!
""".replace("$(SIDECAR)", ctx.file.sidecar.path if ctx.file.sidecar else "")
else:
executable = ctx.outputs.executable
ctx.actions.write(
output = executable,
content = """
#! /bin/sh
echo
echo \\(cd `pwd` \\&\\& \"$@\" -dTEST_TMPDIR=$TEST_TMPDIR\\)
echo
exec \"$@\" -dTEST_TMPDIR=$TEST_TMPDIR
""",
is_executable = True,
)
content = """#!/bin/sh
set -e
cleanup() {
if [ ! -z "$SIDECAR_PID" ]; then
kill $SIDECAR_PID 2>/dev/null || true
wait $SIDECAR_PID 2>/dev/null || true
if [ -f sidecar.log ]; then
cat sidecar.log
fi
fi
}
trap cleanup EXIT
# Start sidecar if specified
if [ ! -z "$(SIDECAR)" ]; then
"$(SIDECAR)" > sidecar.log 2>&1 &
SIDECAR_PID=$!
sleep 3
fi
# Run the actual test
"$@" -dTEST_TMPDIR=$TEST_TMPDIR
""".replace("$(SIDECAR)", ctx.file.sidecar.short_path if ctx.file.sidecar else "")

ctx.actions.write(
output = executable,
content = content,
is_executable = True,
)

runfiles = ctx.runfiles(files = ctx.files.data)
if ctx.file.sidecar:
runfiles = runfiles.merge(ctx.runfiles(files = [ctx.file.sidecar]))

# Also merge the sidecar's own runfiles if it has any
default_runfiles = ctx.attr.sidecar[DefaultInfo].default_runfiles
if default_runfiles:
runfiles = runfiles.merge(default_runfiles)

return [
DefaultInfo(
executable = executable,
runfiles = ctx.runfiles(files = ctx.files.data),
runfiles = runfiles,
),
]

Expand All @@ -104,6 +149,11 @@ _wd_test = rule(
),
"flags": attr.string_list(),
"data": attr.label_list(allow_files = True),
"sidecar": attr.label(
allow_single_file = True,
executable = True,
cfg = "exec",
),
"_platforms_os_windows": attr.label(default = "@platforms//os:windows"),
},
)
14 changes: 14 additions & 0 deletions src/workerd/api/node/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@aspect_rules_js//js:defs.bzl", "js_binary")
load("@bazel_skylib//lib:selects.bzl", "selects")
load("//:build/kj_test.bzl", "kj_test")
load("//:build/wd_cc_library.bzl", "wd_cc_library")
Expand Down Expand Up @@ -253,3 +254,16 @@ wd_test(
args = ["--experimental"],
data = ["tests/dns-nodejs-test.js"],
)

js_binary(
name = "net-nodejs-tcp-server",
entry_point = "tests/net-nodejs-tcp-server.js",
)

wd_test(
size = "large",
src = "tests/net-nodejs-test.wd-test",
args = ["--experimental"],
data = ["tests/net-nodejs-test.js"],
sidecar = "net-nodejs-tcp-server",
)
26 changes: 26 additions & 0 deletions src/workerd/api/node/tests/net-nodejs-tcp-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

// This file is used as a sidecar for the net-nodejs-test tests.
// It creates 2 TCP servers to act as a source of truth for the node:net tests.
// We execute this command using Node.js, which makes net.createServer available.
const net = require('node:net');

const server = net.createServer((s) => {
s.on('error', (err) => {
// Required for process to not crash.
console.error(err);
});
s.end();
});
server.listen(9999, () => console.info('Listening on port 9999'));

const echoServer = net.createServer((s) => {
s.on('error', (err) => {
// Required for process to not crash.
console.error(err);
});
s.pipe(s);
});
echoServer.listen(9998, () => console.info('Listening on port 9998'));
Loading

0 comments on commit cb624a0

Please sign in to comment.