Skip to content

Commit

Permalink
[Translation service] Constrain service api (#2539)
Browse files Browse the repository at this point in the history
* [translation-service] Delay `request`'s work in api.js

The "work" got executed immediately, and just the response was
delayed with setTimeout. The user could make a next fetch without
waiting for a response. Now the work itself is done in the timeout
callback, getting delayed too.

* [translation-service] Add `ConnectionError` error class

Any Error child class satisfied tests that expected
`toThrow(Error)`, meaning unintended behaviour still passed the
tests.

* [CI] Format code

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
themetar and github-actions[bot] authored Sep 17, 2024
1 parent 7dcdd63 commit 432ca4e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
3 changes: 2 additions & 1 deletion exercises/concept/translation-service/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"SleeplessByte"
],
"contributors": [
"AndrewLawendy"
"AndrewLawendy",
"themetar"
],
"files": {
"solution": [
Expand Down
20 changes: 12 additions & 8 deletions exercises/concept/translation-service/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { AbusiveClientError, NotAvailable, Untranslatable } from './errors';
import {
AbusiveClientError,
NotAvailable,
Untranslatable,
ConnectionError,
} from './errors';

const mutex = { current: false };

Expand Down Expand Up @@ -85,13 +90,12 @@ export class ExternalApi {
}

if (this.values[text]) {
this.values[text].shift();
setTimeout(() => {
this.values[text].shift();

// If it's now available, yay, otherwise, nay
setTimeout(
() => callback(this.values[text][0] ? undefined : makeRandomError()),
1,
);
// If it's now available, yay, otherwise, nay
callback(this.values[text][0] ? undefined : makeRandomError());
}, 1);
return;
}

Expand All @@ -114,7 +118,7 @@ function rejectWithRandomDelay(value) {
}

function makeRandomError() {
return new Error(`Error code ${Math.ceil(Math.random() * 10000)}`);
return new ConnectionError(`Error code ${Math.ceil(Math.random() * 10000)}`);
}

class BadRequest extends Error {
Expand Down
6 changes: 6 additions & 0 deletions exercises/concept/translation-service/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ export class Untranslatable extends Error {
super('jIyajbe’');
}
}

export class ConnectionError extends Error {
constructor(message) {
super(message);
}
}
6 changes: 3 additions & 3 deletions exercises/concept/translation-service/service.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
BatchIsEmpty,
} from './service';

import { NotAvailable, Untranslatable } from './errors';
import { NotAvailable, Untranslatable, ConnectionError } from './errors';
import { ExternalApi } from './api';

describe('Free service', () => {
Expand Down Expand Up @@ -134,7 +134,7 @@ describe('Request service', () => {
test('it requests at most three times (does not retry thrice or more)', async () => {
const actual = service.request('ghobe’');

await expect(actual).rejects.toThrow(Error);
await expect(actual).rejects.toThrow(ConnectionError);
});
});

Expand Down Expand Up @@ -182,7 +182,7 @@ describe('Premium service', () => {
test('it requests at most three times (does not retry thrice or more)', async () => {
const actual = service.premium('ghobe’', 0);

await expect(actual).rejects.toThrow(Error);
await expect(actual).rejects.toThrow(ConnectionError);
});

test('it recognizes sufficient quality', async () => {
Expand Down

0 comments on commit 432ca4e

Please sign in to comment.