Congratulations!

Congratulations! You’ve finished the scripted challenges for the “High-Level Infrastructure” module.

Return to module overview

Bonus Challenges

If you want more challenges, there’s more tests left to implement. They’re tricky, though! They all have to do with request cancellation.

Simulating Hangs

Implement the "simulates hangs" test (at the bottom) and make it pass. The following code should cause rot13Client.transform()’s transformPromise to never resolve:

const rot13Client = Rot13Client.createNull([ { hang: true } ]);

To complete this challenge, you’ll need the ability to get the raw transformPromise returned by rot13Client.transform(). The best way to do so is to factor out a transform() function from the transformAsync() test helper.

Once you have the transformPromise, this method will help you assert that it never resolves:

assert.promiseDoesNotResolveAsync(promise)

You’ll implement the behavior with the help of this function:

const httpClient = HttpClient.createNull({
	"/rot13/transform": [ { hang: true } ]
});

Request Cancellation

Implement the "can cancel requests" test and make it pass. This code should cause the request to be cancelled:

const { transformPromise, cancelFn } = rot13Client.transform(...);
cancelFn();

You can use the HTTP request tracker (httpRequests.data) to see when a request has been cancelled.

Calling cancelFn() should also cause transformPromise to reject (throw an exception):

await assert.throwsAsync(
	() => responsePromise,
	"ROT-13 service request cancelled\n" +
		`Host: ${HOST}:9999\n` +
		"Endpoint: /rot13/transform",
	"should throw exception",
);

In the production code, you’ll need to call this method instead of httpClient.requestAsync():

const { responsePromise, cancelFn } = httpClient.request({ host, port, method, path, headers, body });
// ...
cancelFn();   // cancel the HTTP request

Cancellation Tracking

Implement the "tracks requests that are cancelled" test and make it pass. Cancelling a request should add a cancellation object to the ROT-13 request tracker:

assert.deepEqual(rot13Requests.data, [
	{
		port: 9999,
		text: "my text",
		correlationId: "my-correlation-id",
	},
	{
		port: 9999,
		text: "my text",
		correlationId: "my-correlation-id",
		cancelled: true,
	}
]);

Then implement the "doesn't track attempted cancellations that don't actually cancel the request" test and make it pass. If the cancellation doesn’t do anything—for example, if the request is complete, or if it was already cancelled—don’t add a cancellation object to the ROT-13 request tracker.

assert.deepEqual(rot13Requests.data, [
	{
		port: 9999,
		text: "my text",
		correlationId: "my-correlation-id",
	},
]);

httpClient.request()’s cancelFn() returns a boolean that tells you if the request was cancelled.

const { responsePromise, cancelFn } = httpClient.request({ host, port, method, path, headers, body });
// ...
const cancellationOccurred = cancelFn();

Solutions

To see the solutions to these bonus challenges, check out the javascript or typescript branch.

Return to module overview