|
| 1 | +import { WranglerDevRunner } from '../../helpers/index.js'; |
| 2 | +import { test, expect, describe } from 'vitest'; |
| 3 | +import { randomUUID } from 'node:crypto'; |
| 4 | + |
| 5 | +const fetchOrTimeout = async (fetchPromise: Promise<Response>, timeoutMs: number) => { |
| 6 | + const timeoutPromise = new Promise((_, reject) => |
| 7 | + setTimeout(() => reject(new Error('timeout')), timeoutMs) |
| 8 | + ); |
| 9 | + return await Promise.race([fetchPromise, timeoutPromise]); |
| 10 | +}; |
| 11 | + |
| 12 | +describe('multiple ports functionality', () => { |
| 13 | + test('waitForPort should wait for port to be ready before returning', async () => { |
| 14 | + const runner = new WranglerDevRunner(); |
| 15 | + const url = await runner.getUrl(); |
| 16 | + const id = randomUUID(); |
| 17 | + |
| 18 | + // This only waits for the default port 8080 to be ready |
| 19 | + await fetch(`${url}/startAndWaitFor8080?id=${id}`); |
| 20 | + |
| 21 | + // first server should be ready immediately |
| 22 | + let fetchPromise = fetch(`${url}/server-one?id=${id}`); |
| 23 | + let res = (await fetchOrTimeout(fetchPromise, 1000)) as Response; |
| 24 | + expect(await res.text()).toBe( |
| 25 | + 'Hello from test container server one! process.env.MESSAGE: start with startAndWaitForPorts' |
| 26 | + ); |
| 27 | + |
| 28 | + // Second server listening on 8081 should not be ready yet - this call should not resolve within 1s |
| 29 | + fetchPromise = fetch(`${url}/server-two?id=${id}`); |
| 30 | + expect(fetchOrTimeout(fetchPromise, 1000)).rejects.toThrow('timeout'); |
| 31 | + |
| 32 | + // Wait for the default port (8080) to be ready |
| 33 | + |
| 34 | + await fetch(`${url}/waitForPort?id=${id}&port=8081`); |
| 35 | + |
| 36 | + // Verify the container is actually ready by making a request |
| 37 | + fetchPromise = fetch(`${url}/server-two?id=${id}`); |
| 38 | + res = (await fetchOrTimeout(fetchPromise, 1000)) as Response; |
| 39 | + expect(await res.text()).toBe( |
| 40 | + 'Hello from test container server two! process.env.MESSAGE: start with startAndWaitForPorts' |
| 41 | + ); |
| 42 | + |
| 43 | + await runner.stop([id]); |
| 44 | + }); |
| 45 | + |
| 46 | + test('startAndWaitForPorts should wait for all specified ports to be ready', async () => { |
| 47 | + const runner = new WranglerDevRunner(); |
| 48 | + const url = await runner.getUrl(); |
| 49 | + const id = randomUUID(); |
| 50 | + |
| 51 | + // Start and wait for ports should wait for all ports to be ready |
| 52 | + await fetch(`${url}/startAndWaitForAllPorts?id=${id}`); |
| 53 | + |
| 54 | + // first server should be ready immediately |
| 55 | + let fetchPromise = fetch(`${url}/server-one?id=${id}`); |
| 56 | + let res = (await fetchOrTimeout(fetchPromise, 1000)) as Response; |
| 57 | + expect(await res.text()).toBe( |
| 58 | + 'Hello from test container server one! process.env.MESSAGE: start with startAndWaitForPorts' |
| 59 | + ); |
| 60 | + |
| 61 | + // second server should be ready immediately |
| 62 | + fetchPromise = fetch(`${url}/server-two?id=${id}`); |
| 63 | + res = (await fetchOrTimeout(fetchPromise, 1000)) as Response; |
| 64 | + expect(await res.text()).toBe( |
| 65 | + 'Hello from test container server two! process.env.MESSAGE: start with startAndWaitForPorts' |
| 66 | + ); |
| 67 | + |
| 68 | + await runner.stop([id]); |
| 69 | + }); |
| 70 | +}); |
0 commit comments