diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2022-08-19 10:14:56 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-19 10:14:56 +0530 |
commit | 8bdcec1c84636aa00bf7444539e68b49d79b1fbf (patch) | |
tree | 77fb6ea2aadda9b50a839ac858f0a11073c06521 /cli/tests | |
parent | cd21cff29942f24ba7d38287186cce64d0e84e56 (diff) |
fix(ext/flash): concurrent response streams (#15493)
Diffstat (limited to 'cli/tests')
-rw-r--r-- | cli/tests/unit/flash_test.ts | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/cli/tests/unit/flash_test.ts b/cli/tests/unit/flash_test.ts index 57138b14f..51534c79b 100644 --- a/cli/tests/unit/flash_test.ts +++ b/cli/tests/unit/flash_test.ts @@ -1848,6 +1848,73 @@ Deno.test( }, ); +Deno.test( + { permissions: { net: true } }, + async function httpServerConcurrentRequests() { + const ac = new AbortController(); + const listeningPromise = deferred(); + + let reqCount = -1; + let timerId: number | undefined; + const server = Deno.serve(async (req) => { + reqCount++; + if (reqCount === 0) { + const msg = new TextEncoder().encode("data: hello\r\n\r\n"); + // SSE + const body = new ReadableStream({ + start(controller) { + timerId = setInterval(() => { + controller.enqueue(msg); + }, 1000); + }, + cancel() { + if (typeof timerId === "number") { + clearInterval(timerId); + } + }, + }); + return new Response(body, { + headers: { + "Content-Type": "text/event-stream", + }, + }); + } + + return new Response(`hello ${reqCount}`); + }, { + port: 4503, + signal: ac.signal, + onListen: onListen(listeningPromise), + onError: createOnErrorCb(ac), + }); + + const sseRequest = await fetch(`http://localhost:4503/`); + + const decoder = new TextDecoder(); + const stream = sseRequest.body!.getReader(); + { + const { done, value } = await stream.read(); + assert(!done); + assertEquals(decoder.decode(value), "data: hello\r\n\r\n"); + } + + const helloRequest = await fetch(`http://localhost:4503/`); + assertEquals(helloRequest.status, 200); + assertEquals(await helloRequest.text(), "hello 1"); + + { + const { done, value } = await stream.read(); + assert(!done); + assertEquals(decoder.decode(value), "data: hello\r\n\r\n"); + } + + await stream.cancel(); + clearInterval(timerId); + ac.abort(); + await server; + }, +); + function chunkedBodyReader(h: Headers, r: BufReader): Deno.Reader { // Based on https://tools.ietf.org/html/rfc2616#section-19.4.6 const tp = new TextProtoReader(r); |