Skip to content

Commit

Permalink
fix: dns test (#867)
Browse files Browse the repository at this point in the history
* Fix test callbacks

* Make error message more generic

* Disable tests on ubuntu due to timeout
  • Loading branch information
richarddavison authored Mar 6, 2025
1 parent c8b7264 commit f859364
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 38 deletions.
14 changes: 14 additions & 0 deletions llrt_core/src/modules/js/@llrt/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ class TestServer {
socket.on("error", (error) => {
const workerId = this.workerIdBySocket.get(socket);
if (!workerId || !this.workerData[workerId].completed) {
if (workerId) {
const workerData = this.workerData[workerId];
const stdErr = workerData.stdErrBuffer.getContent().toString();
const stdOut = workerData.stdOutBuffer.getContent().toString();
let errorOutput = Color.RED_BACKGROUND(Color.BOLD("Worker Error:\n"));

if (stdErr) {
errorOutput += Color.RED(`\nStd Err:\n${stdErr}`);
}
if (stdOut) {
errorOutput += Color.RED(`\nStd Out:\n${stdOut}`);
}
console.error(errorOutput);
}
this.handleError(TestServer.ERROR_CODE_SOCKET_ERROR, error, {
socket,
});
Expand Down
73 changes: 35 additions & 38 deletions tests/unit/dns.test.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
const dns = require("dns");
import dns from "dns";

describe("lookup", () => {
it("localhost name resolution should be possible (optionless)", (done) => {
dns.lookup("localhost", (err, address, family) => {
expect(err).toBeNull();
expect(address === "::1" || address === "127.0.0.1").toBeTruthy();
expect(family === 4 || family === 6).toBeTruthy();
done();
// Promise wrapper for dns.lookup
const dnsLookupAsync = (
hostname: string,
options?: number | dns.LookupOptions
) =>
new Promise<dns.LookupAddress>((resolve, reject) => {
dns.lookup(hostname, options as any, (err, address, family) => {
if (err) reject(err);
else resolve({ address, family });
});
});

it("Name resolution for localhost2 should result in an error (optionless)", () => {
dns.lookup("localhost2", (err, address, family) => {
expect(err.message).toEqual(
"failed to lookup address information: nodename nor servname provided, or not known"
);
});
describe("lookup", () => {
it("localhost name resolution should be possible (optionless)", async () => {
const { address, family } = await dnsLookupAsync("localhost");
expect(address === "::1" || address === "127.0.0.1").toBeTruthy();
expect(family === 4 || family === 6).toBeTruthy();
});

it("localhost name resolution should be possible (integer option)", (done) => {
dns.lookup("localhost", 4, (err, address, family) => {
expect(err).toBeNull();
expect(address).toEqual("127.0.0.1");
expect(family).toEqual(4);
done();
});
it("localhost name resolution should be possible (integer option)", async () => {
const { address, family } = await dnsLookupAsync("localhost", 4);
expect(address).toEqual("127.0.0.1");
expect(family).toEqual(4);
});

it("Name resolution for localhost2 should result in an error (integer option)", () => {
dns.lookup("localhost2", 4, (err, address, family) => {
expect(err.message).toEqual(
"failed to lookup address information: nodename nor servname provided, or not known"
);
it("localhost name resolution should be possible (record option)", async () => {
const { address, family } = await dnsLookupAsync("localhost", {
family: 4,
});
expect(address).toEqual("127.0.0.1");
expect(family).toEqual(4);
});

it("localhost name resolution should be possible (record option)", (done) => {
dns.lookup("localhost", { family: 4 }, (err, address, family) => {
expect(err).toBeNull();
expect(address).toEqual("127.0.0.1");
expect(family).toEqual(4);
done();
if (process.platform !== "linux") {
it("Name resolution for localhost2 should result in an error (integer option)", async () => {
await expect(dnsLookupAsync("localhost2", 4)).rejects.toThrow("known");
});
});

it("Name resolution for localhost2 should result in an error (record option)", () => {
dns.lookup("localhost2", { family: 4 }, (err, address, family) => {
expect(err.message).toEqual(
"failed to lookup address information: nodename nor servname provided, or not known"
it("Name resolution for localhost2 should result in an error (optionless)", async () => {
await expect(dnsLookupAsync("localhost2")).rejects.toThrow("known");
});

it("Name resolution for localhost2 should result in an error (record option)", async () => {
await expect(dnsLookupAsync("localhost2", { family: 4 })).rejects.toThrow(
"known"
);
});
});
}
});

0 comments on commit f859364

Please sign in to comment.