-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
unstable_dev
and add e2e tests for service bindings (#4170)
* Use `unstable_dev` and add e2e tests for service bindings Co-authored-by: MrBBot <bcoll@cloudflare.com>
- Loading branch information
Showing
2 changed files
with
157 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,40 @@ | ||
import { fork } from "child_process"; | ||
import * as path from "path"; | ||
import { fetch } from "undici"; | ||
import { describe, expect, it, beforeAll, afterAll } from "vitest"; | ||
import type { ChildProcess } from "child_process"; | ||
|
||
import { UnstableDevWorker, unstable_dev } from "wrangler"; | ||
describe("Service Bindings", () => { | ||
let aProcess: ChildProcess; | ||
let aIP: string; | ||
let aPort: number; | ||
let aResolveReadyPromise: (value: unknown) => void; | ||
const aReadyPromise = new Promise((resolve) => { | ||
aResolveReadyPromise = resolve; | ||
}); | ||
|
||
let bProcess: ChildProcess; | ||
let bIP: string; | ||
let bPort: number; | ||
let bResolveReadyPromise: (value: unknown) => void; | ||
const bReadyPromise = new Promise((resolve) => { | ||
bResolveReadyPromise = resolve; | ||
}); | ||
let aWorker: UnstableDevWorker; | ||
|
||
beforeAll(() => { | ||
aProcess = fork( | ||
path.join("..", "..", "..", "packages", "wrangler", "bin", "wrangler.js"), | ||
["dev", "index.ts", "--local", "--port=0"], | ||
{ | ||
stdio: ["ignore", "ignore", "ignore", "ipc"], | ||
cwd: path.resolve(__dirname, "..", "a"), | ||
} | ||
).on("message", (message) => { | ||
const parsedMessage = JSON.parse(message.toString()); | ||
aIP = parsedMessage.ip; | ||
aPort = parsedMessage.port; | ||
aResolveReadyPromise(undefined); | ||
}); | ||
let bWorker: UnstableDevWorker; | ||
|
||
bProcess = fork( | ||
path.join("..", "..", "..", "packages", "wrangler", "bin", "wrangler.js"), | ||
["dev", "index.ts", "--local", "--port=0"], | ||
{ | ||
stdio: ["ignore", "ignore", "ignore", "ipc"], | ||
cwd: path.resolve(__dirname, "..", "b"), | ||
} | ||
).on("message", (message) => { | ||
const parsedMessage = JSON.parse(message.toString()); | ||
bIP = parsedMessage.ip; | ||
bPort = parsedMessage.port; | ||
bResolveReadyPromise(undefined); | ||
beforeAll(async () => { | ||
bWorker = await unstable_dev("./b/index.ts", { | ||
config: "./b/wrangler.toml", | ||
}); | ||
}); | ||
|
||
afterAll(async () => { | ||
await aReadyPromise; | ||
await bReadyPromise; | ||
await new Promise((resolve, reject) => { | ||
aProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
aProcess.kill("SIGTERM"); | ||
|
||
bProcess.once("exit", (code) => { | ||
if (!code) { | ||
resolve(code); | ||
} else { | ||
reject(code); | ||
} | ||
}); | ||
bProcess.kill("SIGTERM"); | ||
aWorker = await unstable_dev("./a/index.ts", { | ||
config: "./a/wrangler.toml", | ||
}); | ||
}); | ||
|
||
it("connects up Durable Objects and keeps state across wrangler instances", async () => { | ||
await aReadyPromise; | ||
await bReadyPromise; | ||
|
||
// Service registry is polled every 300ms, | ||
// so let's give worker A some time to find B | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 700)); | ||
}); | ||
|
||
const responseA = await fetch(`http://${aIP}:${aPort}/`); | ||
it("connects up Durable Objects and keeps state across wrangler instances", async () => { | ||
const responseA = await aWorker.fetch(`https://example.com/`); | ||
const textA = await responseA.text(); | ||
expect(textA).toEqual("hello world"); | ||
|
||
const responseB = await fetch(`http://${bIP}:${bPort}/`); | ||
const responseB = await bWorker.fetch(`/`); | ||
const textB = await responseB.text(); | ||
expect(textB).toEqual("hello world"); | ||
}); | ||
|
||
it("gives facade service workers a constructor name of Fetcher", async () => { | ||
await aReadyPromise; | ||
await bReadyPromise; | ||
|
||
// Service registry is polled every 300ms, | ||
// so let's give worker A some time to find B | ||
await new Promise((resolve) => setTimeout(resolve, 700)); | ||
|
||
const responseA = await fetch(`http://${aIP}:${aPort}/constructor`); | ||
const responseA = await aWorker.fetch(`/constructor`); | ||
const textA = await responseA.text(); | ||
expect(textA).toEqual("Fetcher"); | ||
}); | ||
afterAll(async () => { | ||
await aWorker?.stop(); | ||
await bWorker?.stop(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters