Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/bun.js/test/Collection.zig
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,12 @@ pub fn step(buntest_strong: bun_test.BunTestPtr, globalThis: *jsc.JSGlobalObject
this.active_scope = new_scope;
group.log("collection:runOne set scope to {s}", .{this.active_scope.base.name orelse "undefined"});

BunTest.runTestCallback(buntest_strong, globalThis, callback.get(), false, .{
.collection = .{
.active_scope = previous_scope,
},
}, .epoch);
if (BunTest.runTestCallback(buntest_strong, globalThis, callback.get(), false, .{
.collection = .{ .active_scope = previous_scope },
}, .epoch)) |cfg_data| {
// the result is available immediately; queue
buntest.addResult(cfg_data);
}

return .{ .waiting = .{} };
}
Expand Down
6 changes: 5 additions & 1 deletion src/bun.js/test/Execution.zig
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,11 @@ fn stepSequenceOne(buntest_strong: bun_test.BunTestPtr, globalThis: *jsc.JSGloba
};
groupLog.log("runSequence queued callback: {}", .{callback_data});

BunTest.runTestCallback(buntest_strong, globalThis, cb.get(), next_item.has_done_parameter, callback_data, next_item.timespec);
if (BunTest.runTestCallback(buntest_strong, globalThis, cb.get(), next_item.has_done_parameter, callback_data, next_item.timespec) != null) {
// the result is available immediately; advance the sequence and run again.
this.advanceSequence(sequence, group);
return null; // run again
}
return .{ .execute = .{ .timeout = next_item.timespec } };
} else {
switch (next_item.base.mode) {
Expand Down
37 changes: 27 additions & 10 deletions src/bun.js/test/bun_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ pub const BunTest = struct {
errdefer group.log("ended in error", .{});

const result, const this_ptr = callframe.argumentsAsArray(2);
if (this_ptr.asPtrAddress() == 0) return; // extra handler

const refdata: *RefData = this_ptr.asPromisePtr(RefData);
defer refdata.deref();
Expand Down Expand Up @@ -543,8 +544,8 @@ pub const BunTest = struct {
}
}

/// if sync, the result is queued and appended later
pub fn runTestCallback(this_strong: BunTestPtr, globalThis: *jsc.JSGlobalObject, cfg_callback: jsc.JSValue, cfg_done_parameter: bool, cfg_data: BunTest.RefDataValue, timeout: bun.timespec) void {
/// if sync, the result is returned. if async, null is returned.
pub fn runTestCallback(this_strong: BunTestPtr, globalThis: *jsc.JSGlobalObject, cfg_callback: jsc.JSValue, cfg_done_parameter: bool, cfg_data: BunTest.RefDataValue, timeout: bun.timespec) ?RefDataValue {
group.begin(@src());
defer group.end();
const this = this_strong.get();
Expand Down Expand Up @@ -581,25 +582,41 @@ pub const BunTest = struct {
} else bun.debugAssert(false); // this should be unreachable, we create DoneCallback above
}

if (result != null and result.?.asPromise() != null) {
if (result) |result_jsvalue| if (result_jsvalue.asPromise()) |promise| {
defer result_jsvalue.ensureStillAlive(); // because sometimes we use promise without result

group.log("callTestCallback -> promise: data {}", .{cfg_data});
// TODO: supress unhandled rejection error
result_jsvalue.then(globalThis, null, bunTestThen, bunTestCatch);
drain(globalThis); // drain microtasks to see if the promise is immediately resolved
if (dcb_ref == null) {
const immediate_status = promise.status(globalThis.vm());
if (immediate_status != .pending) {
// immediately resolved
if (immediate_status == .rejected) {
// post error
const value = promise.result(globalThis.vm());
this.onUncaughtException(globalThis, value, true, cfg_data);
}
return cfg_data;
}
}
// not immediately resolved; register 'then' to handle the result when it becomes available
const this_ref: *RefData = if (dcb_ref) |dcb_ref_value| dcb_ref_value.dupe() else ref(this_strong, cfg_data);
result.?.then(globalThis, this_ref, bunTestThen, bunTestCatch);
drain(globalThis);
return;
}
result_jsvalue.then(globalThis, this_ref, bunTestThen, bunTestCatch);
return null;
};

if (dcb_ref) |_| {
// completed asynchronously
group.log("callTestCallback -> wait for done callback", .{});
drain(globalThis);
return;
return null;
}

group.log("callTestCallback -> sync", .{});
drain(globalThis);
this.addResult(cfg_data);
return;
return cfg_data;
}

/// called from the uncaught exception handler, or if a test callback rejects or throws an error
Expand Down
15 changes: 15 additions & 0 deletions test/js/bun/test/concurrent_immediate.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
beforeEach(() => {
console.log("beforeEach");
});
afterEach(() => {
console.log("afterEach");
});
test.concurrent("test 1", () => {
console.log("start test 1");
});
test.concurrent("test 2", () => {
console.log("start test 2");
});
test.concurrent("test 3", () => {
console.log("start test 3");
});
77 changes: 77 additions & 0 deletions test/js/bun/test/concurrent_immediate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe, normalizeBunSnapshot } from "harness";

test("concurrent immediate", async () => {
const result = await Bun.spawn({
cmd: [bunExe(), "test", import.meta.dir + "/concurrent_immediate.fixture.ts"],
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const exitCode = await result.exited;
const stdout = await result.stdout.text();
const stderr = await result.stderr.text();
expect(exitCode).toBe(0);
expect(normalizeBunSnapshot(stdout)).toMatchInlineSnapshot(`
"bun test <version> (<revision>)
beforeEach
start test 1
afterEach
beforeEach
start test 2
afterEach
beforeEach
start test 3
afterEach"
`);

const result2 = await Bun.spawn({
cmd: [bunExe(), "test", import.meta.dir + "/concurrent_immediate_promise.fixture.ts"],
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const exitCode2 = await result2.exited;
const stdout2 = await result2.stdout.text();
const stderr2 = await result2.stderr.text();
expect(exitCode2).toBe(0);
expect(normalizeBunSnapshot(stdout2)).toBe(normalizeBunSnapshot(stdout));
expect(normalizeBunSnapshot(stderr2).replaceAll("_promise.", ".")).toBe(normalizeBunSnapshot(stderr));
});

function filterImportantLines(stderr: string) {
return normalizeBunSnapshot(stderr)
.split("\n")
.filter(l => l.startsWith("(pass)") || l.startsWith("(fail)") || l.startsWith("error:"))
.join("\n");
}

test("concurrent immediate error", async () => {
const result = await Bun.spawn({
cmd: [bunExe(), "test", import.meta.dir + "/concurrent_immediate_error.fixture.ts"],
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const exitCode = await result.exited;
const stdout = await result.stdout.text();
const stderr = await result.stderr.text();
expect(exitCode).toBe(1);
expect(filterImportantLines(stderr)).toMatchInlineSnapshot(`
"(pass) test 1
error: test 2 error
(fail) test 2
(pass) test 3"
`);

const result2 = await Bun.spawn({
cmd: [bunExe(), "test", import.meta.dir + "/concurrent_immediate_error_promise.fixture.ts"],
stdout: "pipe",
stderr: "pipe",
env: bunEnv,
});
const exitCode2 = await result2.exited;
const stdout2 = await result2.stdout.text();
const stderr2 = await result2.stderr.text();
expect(filterImportantLines(stderr2)).toBe(filterImportantLines(stderr));
});
15 changes: 15 additions & 0 deletions test/js/bun/test/concurrent_immediate_error.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
beforeEach(() => {
console.log("beforeEach");
});
afterEach(() => {
console.log("afterEach");
});
test.concurrent("test 1", () => {
console.log("start test 1");
});
test.concurrent("test 2", () => {
throw new Error("test 2 error");
});
test.concurrent("test 3", () => {
console.log("start test 3");
});
15 changes: 15 additions & 0 deletions test/js/bun/test/concurrent_immediate_error_promise.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
beforeEach(async () => {
console.log("beforeEach");
});
afterEach(async () => {
console.log("afterEach");
});
test.concurrent("test 1", async () => {
console.log("start test 1");
});
test.concurrent("test 2", async () => {
throw new Error("test 2 error");
});
test.concurrent("test 3", async () => {
console.log("start test 3");
});
15 changes: 15 additions & 0 deletions test/js/bun/test/concurrent_immediate_promise.fixture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
beforeEach(async () => {
console.log("beforeEach");
});
afterEach(async () => {
console.log("afterEach");
});
test.concurrent("test 1", async () => {
console.log("start test 1");
});
test.concurrent("test 2", async () => {
console.log("start test 2");
});
test.concurrent("test 3", async () => {
console.log("start test 3");
});