Skip to content
This repository was archived by the owner on Feb 20, 2026. It is now read-only.

Commit 047afa1

Browse files
authored
test: add test for behavior with invalid duration returned (#964)
* test: add test for behavior with invalid duration returned * Add constant for expected backoff to clarify test
1 parent 98d7448 commit 047afa1

1 file changed

Lines changed: 25 additions & 3 deletions

File tree

test/test-profiler.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,12 @@ describe('Profiler', () => {
734734
| undefined
735735
| sinon.SinonStub<[DecorateRequestOptions, BodyResponseCallback], void>;
736736
let randomStub: sinon.SinonStub<[], number> | undefined;
737+
738+
const RANDOM_VALUE = 0.5;
739+
// Retryer calculates expected backoff as RANDOM_VALUE * testConfig.initialBackoffMillis => 0.5 * 1000
740+
const EXPECTED_BACKOFF = 500;
737741
before(() => {
738-
randomStub = sinon.stub(Math, 'random').returns(0.5);
742+
randomStub = sinon.stub(Math, 'random').returns(RANDOM_VALUE);
739743
});
740744
afterEach(() => {
741745
if (requestStub) {
@@ -782,7 +786,7 @@ describe('Profiler', () => {
782786

783787
const profiler = new Profiler(testConfig);
784788
const delayMillis = await profiler.collectProfile();
785-
assert.deepStrictEqual(500, delayMillis);
789+
assert.deepStrictEqual(EXPECTED_BACKOFF, delayMillis);
786790
}
787791
);
788792
it('should reset backoff after success', async () => {
@@ -868,7 +872,25 @@ describe('Profiler', () => {
868872
);
869873
const profiler = new Profiler(testConfig);
870874
const delayMillis = await profiler.collectProfile();
871-
assert.strictEqual(500, delayMillis);
875+
assert.strictEqual(EXPECTED_BACKOFF, delayMillis);
876+
}
877+
);
878+
it(
879+
'should return expected backoff when non-200 error and invalid server backoff' +
880+
' string specified',
881+
async () => {
882+
requestStub = sinon
883+
.stub(common.ServiceObject.prototype, 'request')
884+
.onCall(0)
885+
.callsArgWith(
886+
1,
887+
undefined,
888+
{error: {details: [{retryDelay: 'not a duration'}]}},
889+
{statusCode: 409}
890+
);
891+
const profiler = new Profiler(testConfig);
892+
const delayMillis = await profiler.collectProfile();
893+
assert.strictEqual(EXPECTED_BACKOFF, delayMillis);
872894
}
873895
);
874896
it(

0 commit comments

Comments
 (0)