From 09099b3747d90beb2036ae592dbdbaffd74266b8 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Tue, 9 Sep 2025 21:10:40 -0700 Subject: [PATCH] test without microtask --- src/sql/mysql/MySQLConnection.zig | 13 +++++++++-- test/js/sql/sql-mysql.test.ts | 38 +++++++++++++++++-------------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/sql/mysql/MySQLConnection.zig b/src/sql/mysql/MySQLConnection.zig index c4331ed963..8deaf3aff9 100644 --- a/src/sql/mysql/MySQLConnection.zig +++ b/src/sql/mysql/MySQLConnection.zig @@ -382,7 +382,7 @@ pub fn failWithJSValue(this: *MySQLConnection, value: JSValue) void { this.status = .failed; const on_close = this.consumeOnCloseCallback(this.globalObject) orelse return; - + on_close.ensureStillAlive(); const loop = this.vm.eventLoop(); loop.enter(); defer loop.exit(); @@ -391,7 +391,15 @@ pub fn failWithJSValue(this: *MySQLConnection, value: JSValue) void { js_error.ensureStillAlive(); const queries_array = this.getQueriesArray(); queries_array.ensureStillAlive(); - this.globalObject.queueMicrotask(on_close, &[_]JSValue{ js_error, queries_array }); + // this.globalObject.queueMicrotask(on_close, &[_]JSValue{ js_error, queries_array }); + _ = on_close.call( + this.globalObject, + .js_undefined, + &[_]JSValue{ + value.toError() orelse value, + this.getQueriesArray(), + }, + ) catch |e| this.globalObject.reportActiveExceptionAsUnhandled(e); } pub fn fail(this: *MySQLConnection, message: []const u8, err: AnyMySQLError.Error) void { @@ -1316,6 +1324,7 @@ pub fn setStatus(this: *@This(), status: ConnectionState) void { switch (status) { .connected => { const on_connect = this.consumeOnConnectCallback(this.globalObject) orelse return; + on_connect.ensureStillAlive(); var js_value = this.js_value; if (js_value == .zero) { js_value = .js_undefined; diff --git a/test/js/sql/sql-mysql.test.ts b/test/js/sql/sql-mysql.test.ts index 611805d88d..41c7c3fb39 100644 --- a/test/js/sql/sql-mysql.test.ts +++ b/test/js/sql/sql-mysql.test.ts @@ -612,23 +612,27 @@ if (docker) { expect(e.message).toBe("password error"); } }); - test("Support dynamic async password function that throws", async () => { - await using sql = new SQL({ - ...options, - max: 1, - password: async () => { - await Bun.sleep(10); - throw new Error("password error"); - }, + + for (let i = 0; i < 10000; i++) { + test.only("Support dynamic async password function that throws" + i, async () => { + await using sql = new SQL({ + ...options, + max: 1, + password: async () => { + await Bun.sleep(10); + throw new Error("password error"); + }, + }); + try { + await sql`select true as x`; + expect.unreachable(); + } catch (e: any) { + expect(e).toBeInstanceOf(Error); + expect(e.message).toBe("password error"); + } }); - try { - await sql`select true as x`; - expect.unreachable(); - } catch (e: any) { - expect(e).toBeInstanceOf(Error); - expect(e.message).toBe("password error"); - } - }); + } + test("sql file", async () => { await using sql = new SQL(options); expect((await sql.file(rel("select.sql")))[0].x).toBe(1); @@ -880,7 +884,7 @@ if (docker) { } catch (e) { expect(e).toBeInstanceOf(Error); expect(e.code).toBe("ERR_MYSQL_CONNECTION_TIMEOUT"); - expect(e.message).toMatch(/Connection timeout after 200ms/); + expect(e.message).toMatch(/Connection timeut after 200ms/); } finally { sql.close(); server.close();