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 13, 2025
1 parent c2dcc1e commit 9380b4c
Show file tree
Hide file tree
Showing 6 changed files with 1,279 additions and 21 deletions.
82 changes: 64 additions & 18 deletions build/wd_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,71 @@ 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
fi
}
trap cleanup EXIT
# Start sidecar if specified
if [ ! -z "$(SIDECAR)" ]; then
"$(SIDECAR)" & SIDECAR_PID=$!
# Wait until the process is ready
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 +145,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"),
},
)
9 changes: 6 additions & 3 deletions src/node/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,10 @@ Socket.prototype._unrefTimer = function _unrefTimer(
for (let s = this; s != null; s = s._parent) {
if (s[kTimeout] != null) {
clearTimeout(s[kTimeout] as unknown as number);
s[kTimeout] = this?.setTimeout(s.timeout, s._onTimeout.bind(s));
s[kTimeout] = (this as SocketClass).setTimeout(
s.timeout,
s._onTimeout.bind(s)
);
}
}
};
Expand Down Expand Up @@ -363,8 +366,8 @@ Socket.prototype.setTimeout = function (

Socket.prototype._onTimeout = function (this: SocketClass): void {
const handle = this._handle;
const lastWriteQueueSize = this[kLastWriteQueueSize];
if (lastWriteQueueSize != null && lastWriteQueueSize > 0 && handle) {
const lastWriteQueueSize = this[kLastWriteQueueSize] as number;
if (lastWriteQueueSize > 0 && handle) {
// `lastWriteQueueSize !== writeQueueSize` means there is
// an active write in progress, so we suppress the timeout.
const { writeQueueSize } = handle;
Expand Down
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",
)
42 changes: 42 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,42 @@
// 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', () => {
// Do nothing
});
s.end();
});
server.listen(9999, () => console.info('Listening on port 9999'));

const echoServer = net.createServer((s) => {
s.setTimeout(100);
s.on('error', () => {
// Do nothing
});
s.pipe(s);
});
echoServer.listen(9998, () => console.info('Listening on port 9998'));

const timeoutServer = net.createServer((s) => {
s.setTimeout(100);
s.resume();
s.once('timeout', () => {
// Try to reset the timeout.
s.write('WHAT.');
});

s.on('end', () => {
s.end();
});
s.on('error', () => {
// Do nothing
});
});
timeoutServer.listen(9997, () => console.info('Listening on port 9997'));
Loading

0 comments on commit 9380b4c

Please sign in to comment.