summaryrefslogtreecommitdiff
path: root/std/http/racing_server.ts
diff options
context:
space:
mode:
authorRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
committerRyan Dahl <ry@tinyclouds.org>2019-10-09 17:10:09 -0400
commit151ce0266eb4de2c8fc600c81c192a5f791b6169 (patch)
tree7cb04016a1c7ee88adde83814548d7a9409dcde3 /std/http/racing_server.ts
parenta355f7c807686918734416d91b79c26c21effba9 (diff)
Move everything into std subdir
Diffstat (limited to 'std/http/racing_server.ts')
-rw-r--r--std/http/racing_server.ts50
1 files changed, 50 insertions, 0 deletions
diff --git a/std/http/racing_server.ts b/std/http/racing_server.ts
new file mode 100644
index 000000000..9d118dc6d
--- /dev/null
+++ b/std/http/racing_server.ts
@@ -0,0 +1,50 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { serve, ServerRequest } from "./server.ts";
+import { delay } from "../util/async.ts";
+
+const addr = Deno.args[1] || "127.0.0.1:4501";
+const server = serve(addr);
+
+const body = new TextEncoder().encode("Hello 1\n");
+const body4 = new TextEncoder().encode("World 4\n");
+
+async function delayedRespond(request: ServerRequest): Promise<void> {
+ await delay(3000);
+ await request.respond({ status: 200, body });
+}
+
+async function largeRespond(request: ServerRequest, c: string): Promise<void> {
+ const b = new Uint8Array(1024 * 1024);
+ b.fill(c.charCodeAt(0));
+ await request.respond({ status: 200, body: b });
+}
+
+async function main(): Promise<void> {
+ let step = 1;
+ for await (const request of server) {
+ switch (step) {
+ case 1:
+ // Try to wait long enough.
+ // For pipelining, this should cause all the following response
+ // to block.
+ delayedRespond(request);
+ break;
+ case 2:
+ // HUGE body.
+ largeRespond(request, "a");
+ break;
+ case 3:
+ // HUGE body.
+ largeRespond(request, "b");
+ break;
+ default:
+ request.respond({ status: 200, body: body4 });
+ break;
+ }
+ step++;
+ }
+}
+
+main();
+
+console.log("Racing server listening...\n");