2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-04-13 15:23:56 -04:00
|
|
|
import { serve, ServerRequest } from "./server.ts";
|
2019-07-28 07:35:47 -04:00
|
|
|
import { delay } from "../util/async.ts";
|
2019-04-13 15:23:56 -04:00
|
|
|
|
|
|
|
const addr = Deno.args[1] || "127.0.0.1:4501";
|
|
|
|
const server = serve(addr);
|
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
function body(i: number): string {
|
|
|
|
return `Step${i}\n`;
|
|
|
|
}
|
|
|
|
async function delayedRespond(
|
|
|
|
request: ServerRequest,
|
|
|
|
step: number
|
|
|
|
): Promise<void> {
|
2019-07-28 07:35:47 -04:00
|
|
|
await delay(3000);
|
2020-02-24 22:49:39 -05:00
|
|
|
await request.respond({ status: 200, body: body(step) });
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
2020-02-24 22:49:39 -05:00
|
|
|
async function ignoreToConsume(
|
|
|
|
request: ServerRequest,
|
|
|
|
step: number
|
|
|
|
): Promise<void> {
|
|
|
|
await request.respond({ status: 200, body: body(step) });
|
|
|
|
}
|
|
|
|
|
2019-10-28 15:58:35 -04:00
|
|
|
console.log("Racing server listening...\n");
|
|
|
|
|
|
|
|
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.
|
2020-02-24 22:49:39 -05:00
|
|
|
delayedRespond(request, step);
|
2019-10-28 15:58:35 -04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// HUGE body.
|
|
|
|
largeRespond(request, "a");
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
// HUGE body.
|
|
|
|
largeRespond(request, "b");
|
|
|
|
break;
|
2020-02-24 22:49:39 -05:00
|
|
|
case 4:
|
|
|
|
// Ignore to consume body (content-length)
|
|
|
|
ignoreToConsume(request, step);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
// Ignore to consume body (chunked)
|
|
|
|
ignoreToConsume(request, step);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
// Ignore to consume body (chunked + trailers)
|
|
|
|
ignoreToConsume(request, step);
|
|
|
|
break;
|
2019-10-28 15:58:35 -04:00
|
|
|
default:
|
2020-02-24 22:49:39 -05:00
|
|
|
request.respond({ status: 200, body: body(step) });
|
2019-10-28 15:58:35 -04:00
|
|
|
break;
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|
2019-10-28 15:58:35 -04:00
|
|
|
step++;
|
2019-04-13 15:23:56 -04:00
|
|
|
}
|