Skip to content

Commit

Permalink
chore: replaced verbose stmt by function (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed May 5, 2024
1 parent b8d839c commit 26fa3c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 4 additions & 0 deletions test/helper/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export function assertTrue(value: object | boolean | undefined) {
assertEquals(value, true);
}

export function sleep(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

export function generateAuth(token: string | undefined, seconds: number) {
return {
token,
Expand Down
3 changes: 2 additions & 1 deletion test/playground/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Switcher, checkValue, checkNumeric } from '../../mod.ts'
import { sleep } from "../helper/utils.ts";

const SWITCHER_KEY = 'MY_SWITCHER';
const apiKey = 'JDJiJDA4JEFweTZjSTR2bE9pUjNJOUYvRy9raC4vRS80Q2tzUnk1d3o1aXFmS2o5eWJmVW11cjR0ODNT';
Expand Down Expand Up @@ -86,7 +87,7 @@ const _testThrottledAPICall = async () => {
// Requires remote API
const _testSnapshotUpdate = async () => {
setupSwitcher(false);
await new Promise(resolve => setTimeout(resolve, 2000));
await sleep(2000);

switcher = Switcher.factory();
console.log('checkSnapshot:', await Switcher.checkSnapshot());
Expand Down
21 changes: 10 additions & 11 deletions test/switcher-functional.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, afterAll, afterEach, beforeEach,
assertEquals, assertRejects, assertThrows, assertFalse,
assertSpyCalls, spy } from './deps.ts';
import { given, givenError, tearDown, assertTrue, generateAuth, generateResult, generateDetailedResult } from './helper/utils.ts'
import { given, givenError, tearDown, assertTrue, generateAuth, generateResult, generateDetailedResult, sleep } from './helper/utils.ts'

import {
Switcher,
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('Integrated test - Switcher:', function () {

assertTrue(await switcher.isItOn('FLAG_1')); // sync
assertTrue(await switcher.isItOn('FLAG_1')); // async
await new Promise(resolve => setTimeout(resolve, 100)); // wait resolve async Promise
await sleep(100); // wait resolve async Promise

assertSpyCalls(spyPrepare, 1);
assertSpyCalls(spyExecutionLogger, 2); // 1st (sync) + 2nd (async)
Expand Down Expand Up @@ -139,19 +139,19 @@ describe('Integrated test - Switcher:', function () {
assertSpyCalls(spyPrepare, 1);

// Next call should call the API again - token has expired
await new Promise(resolve => setTimeout(resolve, 2000));
await sleep(2000); // wait resolve async Promise

// given
given('POST@/criteria/auth', generateAuth('[auth_token]', 5));
given('POST@/criteria', generateResult(false)); // after token expires

// test - stores result in cache after token renewal
await new Promise(resolve => setTimeout(resolve, 500));
await sleep(500);
result = await switcher.isItOn('FLAG_3');
assertTrue(result);
assertSpyCalls(spyPrepare, 2);

await new Promise(resolve => setTimeout(resolve, 50));
await sleep(50);
result = await switcher.isItOn('FLAG_3');
assertFalse(result);
assertSpyCalls(spyPrepare, 2);
Expand All @@ -175,14 +175,14 @@ describe('Integrated test - Switcher:', function () {
assertTrue(await switcher.isItOn('FLAG_1')); // async

// Next call should call the API again - valid token but crashes on checkCriteria
await new Promise(resolve => setTimeout(resolve, 1000));
await sleep(1000);
assertEquals(asyncErrorMessage, null);

// given
given('POST@/criteria', { message: 'error' }, 500);
assertTrue(await switcher.isItOn('FLAG_1')); // async

await new Promise(resolve => setTimeout(resolve, 1000));
await sleep(1000);
assertEquals(asyncErrorMessage, 'Something went wrong: [checkCriteria] failed with status 500');
});
});
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('Integrated test - Switcher:', function () {
assertTrue(await switcher.isItOn());

// The program delay 2 secs later for the next call
await new Promise(resolve => setTimeout(resolve, 2000));
await sleep(2000);

// Prepare the stub to provide the new token
given('POST@/criteria/auth', generateAuth('asdad12d2232d2323f', 1));
Expand Down Expand Up @@ -589,15 +589,14 @@ describe('Integrated test - Switcher:', function () {
givenError('POST@/criteria/auth', 'ECONNREFUSED');
assertTrue(await switcher.isItOn('FF2FOR2030'));

await new Promise(resolve => setTimeout(resolve, 500));
await sleep(500);
// The call below is in silent mode. It is getting the configuration from the local snapshot again
assertTrue(await switcher.isItOn());

// As the silent mode was configured to retry after 2 seconds, it's still in time,
// therefore, remote call was not yet invoked
assertSpyCalls(spyRemote, 0);

await new Promise(resolve => setTimeout(resolve, 3000));
await sleep(3000);

// Setup the remote mocked response and made it to return false just to make sure it's not fetching from the snapshot
given('GET@/check', undefined, 200);
Expand Down

0 comments on commit 26fa3c7

Please sign in to comment.