diff options
Diffstat (limited to 'cli')
-rw-r--r-- | cli/bench/http/deno_http_flash_ops.js | 4 | ||||
-rw-r--r-- | cli/tests/unit/flash_test.ts | 67 |
2 files changed, 70 insertions, 1 deletions
diff --git a/cli/bench/http/deno_http_flash_ops.js b/cli/bench/http/deno_http_flash_ops.js index 1b833e7f7..40ca25ff1 100644 --- a/cli/bench/http/deno_http_flash_ops.js +++ b/cli/bench/http/deno_http_flash_ops.js @@ -25,13 +25,15 @@ function respond(token, response) { const response = encode( "HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nHello World", ); +let offset = 0; while (true) { let token = nextRequest(); if (token === 0) token = await opAsync("op_flash_next_async", serverId); - for (let i = 0; i < token; i++) { + for (let i = offset; i < offset + token; i++) { respond( i, response, ); } + offset += token; } 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); |