Skip to content

Commit d891e8c

Browse files
authored
Add 'using' test (#8480)
1 parent 736bd4f commit d891e8c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @param {string[]} logs */
2+
function connect(logs) {
3+
logs.push("Connected");
4+
return {
5+
send(message) {
6+
logs.push(`Sent ${message}`);
7+
},
8+
[Symbol.dispose]() {
9+
logs.push("Disconnected synchronously");
10+
},
11+
async [Symbol.asyncDispose]() {
12+
logs.push("Disconnected asynchronously");
13+
},
14+
};
15+
}
16+
17+
/** @param {string[]} logs */
18+
export async function testExplicitResourceManagement(logs) {
19+
using syncConnect = connect(logs);
20+
await using asyncConnect = connect(logs);
21+
22+
syncConnect.send("hello");
23+
asyncConnect.send("goodbye");
24+
}

fixtures/worker-app/src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cookie from "cookie";
22
import { randomBytes } from "isomorphic-random-example";
33
import { now } from "./dep";
4+
import { testExplicitResourceManagement } from "./explicit-resource-management";
45
import { logErrors } from "./log";
56

67
console.log("startup log");
@@ -59,6 +60,12 @@ export default {
5960
});
6061
}
6162

63+
if (pathname === "/explicit-resource-management") {
64+
const logs = [];
65+
await testExplicitResourceManagement(logs);
66+
return Response.json(logs);
67+
}
68+
6269
if (request.headers.get("X-Test-URL") !== null) {
6370
return new Response(request.url);
6471
}

fixtures/worker-app/tests/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,20 @@ describe("'wrangler dev' correctly renders pages", () => {
182182
});
183183
expect(await response.text()).toEqual("x".repeat(100));
184184
});
185+
186+
it("uses explicit resource management", async ({ expect }) => {
187+
const response = await fetch(
188+
`http://${ip}:${port}/explicit-resource-management`
189+
);
190+
expect(await response.json()).toMatchInlineSnapshot(`
191+
[
192+
"Connected",
193+
"Connected",
194+
"Sent hello",
195+
"Sent goodbye",
196+
"Disconnected asynchronously",
197+
"Disconnected synchronously",
198+
]
199+
`);
200+
});
185201
});

0 commit comments

Comments
 (0)