Skip to content

Commit

Permalink
[js] Refactor await'able sleep helper (#1186)
Browse files Browse the repository at this point in the history
Replace multiple copies of that code with a shared helper.
  • Loading branch information
emaxx-google authored Oct 21, 2024
1 parent 093f2ee commit 7861375
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 34 deletions.
13 changes: 2 additions & 11 deletions common/js/src/messaging/message-channel-pair-unittest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

goog.require('GoogleSmartCard.MessageChannelPair');

goog.require('GoogleSmartCard.PromiseHelpers');
goog.require('goog.messaging.AbstractChannel');

goog.setTestOnly();
Expand Down Expand Up @@ -50,16 +51,6 @@ function failWhenReceivedUnexpectedMessage(messageChannel) {
});
}

/**
* @param {number} delayMilliseconds
* @return {!Promise<void>}
*/
function sleep(delayMilliseconds) {
return new Promise((resolve, reject) => {
setTimeout(resolve, delayMilliseconds);
});
}

// Send a message via the first item of the pair and check that it's received at
// the second item of the pair.
goog.exportSymbol('test_MessageChannelPair_sendViaFirst', async function() {
Expand Down Expand Up @@ -137,6 +128,6 @@ goog.exportSymbol(
// if it's present, manifest itself. There's no reliable way to wait for
// this, since normally the message sent after disposal should be silently
// discarded.
await sleep(/*delayMilliseconds=*/ 1000);
await GSC.PromiseHelpers.sleep(/*delayMilliseconds=*/ 1000);
});
}); // goog.scope
11 changes: 11 additions & 0 deletions common/js/src/promise-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,15 @@ const GSC = GoogleSmartCard;
GSC.PromiseHelpers.suppressUnhandledRejectionError = function(promise) {
promise.thenCatch(function() {});
};

/**
* Await'able version of `setTimeout()`.
* @param {number} delayMilliseconds
* @return {!Promise<void>}
*/
GSC.PromiseHelpers.sleep = async function(delayMilliseconds) {
return new Promise((resolve, reject) => {
setTimeout(resolve, delayMilliseconds);
});
};
}); // goog.scope
13 changes: 2 additions & 11 deletions smart_card_connector_app/src/chrome-api-jstocxxtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ goog.require('GoogleSmartCard.PcscLiteClient.API');
goog.require('GoogleSmartCard.PcscLiteCommon.Constants');
goog.require('GoogleSmartCard.PcscLiteServer.ReaderTrackerThroughPcscServerHook');
goog.require('GoogleSmartCard.PcscLiteServerClientsManagement.ReadinessTracker');
goog.require('GoogleSmartCard.PromiseHelpers');
goog.require('GoogleSmartCard.TestingConstants');
goog.require('GoogleSmartCard.TestingLibusbSmartCardSimulationConstants');
goog.require('goog.Thenable');
Expand Down Expand Up @@ -103,16 +104,6 @@ function createChromeApiProvider() {
pcscReadinessTracker.promise);
}

/**
* @param {number} timeoutMillis
* @return {!Promise}
*/
async function sleep(timeoutMillis) {
return new Promise(resolve => {
setTimeout(resolve, timeoutMillis);
});
}

/**
* @param {!goog.Thenable} promise
* @param {number} timeoutMillis
Expand All @@ -129,7 +120,7 @@ async function assertRemainsPending(promise, timeoutMillis) {
if (sleeping)
fail(`Unexpectedly rejected within ${timeoutMillis} ms`);
});
await sleep(timeoutMillis);
await GSC.PromiseHelpers.sleep(timeoutMillis);
sleeping = false;
}

Expand Down
15 changes: 3 additions & 12 deletions smart_card_connector_app/src/pcsc-api-jstocxxtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ goog.require('GoogleSmartCard.PcscLiteCommon.Constants');
goog.require('GoogleSmartCard.PcscLiteServer.ReaderTrackerThroughPcscServerHook');
goog.require('GoogleSmartCard.PcscLiteServerClientsManagement.ClientHandler');
goog.require('GoogleSmartCard.PcscLiteServerClientsManagement.ReadinessTracker');
goog.require('GoogleSmartCard.PromiseHelpers');
goog.require('GoogleSmartCard.RequesterMessage');
goog.require('GoogleSmartCard.TestingLibusbSmartCardSimulationHook');
goog.require('GoogleSmartCard.TestingLibusbSmartCardSimulationConstants');
Expand Down Expand Up @@ -178,16 +179,6 @@ function disposeFakeClient(fakeClient) {
fakeClient.apiMessageChannelPair.dispose();
}

/**
* @param {number} timeoutMillis
* @return {!Promise}
*/
async function sleep(timeoutMillis) {
return new Promise(resolve => {
setTimeout(resolve, timeoutMillis);
});
}

/**
* @param {!goog.Thenable} promise
* @param {number} timeoutMillis
Expand All @@ -204,7 +195,7 @@ async function assertRemainsPending(promise, timeoutMillis) {
if (sleeping)
fail(`Unexpectedly rejected within ${timeoutMillis} ms`);
});
await sleep(timeoutMillis);
await GSC.PromiseHelpers.sleep(timeoutMillis);
sleeping = false;
}

Expand Down Expand Up @@ -1373,7 +1364,7 @@ goog.exportSymbol('testPcscApi', {
'tearDown': async function() {
// Wait for some time before exiting, to let any possible bug have a
// chance to trigger an error.
await sleep(/*timeoutMillis=*/ 1000);
await GSC.PromiseHelpers.sleep(/*delayMilliseconds=*/ 1000);
// Verify the C/C++ module hasn't crashed.
assertFalse(testController.executableModule.isDisposed());
},
Expand Down

0 comments on commit 7861375

Please sign in to comment.