Skip to content

Commit a0e6543

Browse files
add testcase
1 parent 90c4159 commit a0e6543

File tree

7 files changed

+116
-20
lines changed

7 files changed

+116
-20
lines changed

src/AzureAppConfigurationImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ export class AzureAppConfigurationImpl implements AzureAppConfiguration {
335335
async #initialize() {
336336
if (!this.#isInitialLoadCompleted) {
337337
await this.#inspectFmPackage();
338-
const retryEnabled = this.#options?.startupOptions?.retryEnabled ?? false;
338+
const retryEnabled = this.#options?.startupOptions?.retryEnabled ?? true; // enable startup retry by default
339339
const startTimestamp = Date.now();
340340
while (startTimestamp + MAX_STARTUP_TIMEOUT > Date.now()) {
341341
try {

src/StartupOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface StartupOptions {
66
* Specifies whether to enable retry on startup or not.
77
*
88
* @remarks
9-
* If not specified, the default value is false.
9+
* If not specified, the default value is true.
1010
*/
1111
retryEnabled?: boolean;
1212

test/clientOptions.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ describe("custom client options", function () {
4848
policy: countPolicy,
4949
position: "perRetry"
5050
}]
51+
},
52+
startupOptions: {
53+
retryEnabled: false
5154
}
5255
});
5356
};
@@ -73,6 +76,9 @@ describe("custom client options", function () {
7376
retryOptions: {
7477
maxRetries
7578
}
79+
},
80+
startupOptions: {
81+
retryEnabled: false
7682
}
7783
});
7884
};
@@ -108,6 +114,9 @@ describe("custom client options", function () {
108114
policy: countPolicy,
109115
position: "perRetry"
110116
}]
117+
},
118+
startupOptions: {
119+
retryEnabled: false
111120
}
112121
});
113122
};

test/failover.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ describe("failover", function () {
6969
mockConfigurationManagerGetClients([], isFailoverable);
7070

7171
const connectionString = createMockedConnectionString();
72-
return expect(load(connectionString)).eventually.rejectedWith("All clients failed to get configuration settings.");
72+
return expect(load(connectionString, {startupOptions: {retryEnabled: false}}))
73+
.eventually.rejectedWith("All clients failed to get configuration settings.");
7374
});
7475

7576
it("should validate endpoint", () => {

test/keyvault.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function mockNewlyCreatedKeyVaultSecretClients() {
2626
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2727
mockSecretClientGetSecret(mockedData.map(([_key, secretUri, value]) => [secretUri, value]));
2828
}
29+
2930
describe("key vault reference", function () {
3031
this.timeout(MAX_TIME_OUT);
3132

@@ -39,7 +40,7 @@ describe("key vault reference", function () {
3940
});
4041

4142
it("require key vault options to resolve reference", async () => {
42-
return expect(load(createMockedConnectionString())).eventually.rejectedWith("Configure keyVaultOptions to resolve Key Vault Reference(s).");
43+
return expect(load(createMockedConnectionString(), {startupOptions: {retryEnabled: false}})).eventually.rejectedWith("Configure keyVaultOptions to resolve Key Vault Reference(s).");
4344
});
4445

4546
it("should resolve key vault reference with credential", async () => {
@@ -93,6 +94,9 @@ describe("key vault reference", function () {
9394
secretClients: [
9495
new SecretClient("https://fake-vault-name.vault.azure.net", createMockedTokenCredential()),
9596
]
97+
},
98+
startupOptions: {
99+
retryEnabled: false
96100
}
97101
});
98102
return expect(loadKeyVaultPromise).eventually.rejectedWith("No key vault credential or secret resolver callback configured, and no matching secret client could be found.");

test/requestTracing.test.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ describe("request tracing", function () {
3535

3636
it("should have correct user agent prefix", async () => {
3737
try {
38-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
38+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} } );
3939
} catch (e) { /* empty */ }
4040
expect(headerPolicy.headers).not.undefined;
4141
expect(headerPolicy.headers.get("User-Agent")).satisfy((ua: string) => ua.startsWith("javascript-appconfiguration-provider"));
4242
});
4343

4444
it("should have request type in correlation-context header", async () => {
4545
try {
46-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
46+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
4747
} catch (e) { /* empty */ }
4848
expect(headerPolicy.headers).not.undefined;
4949
expect(headerPolicy.headers.get("Correlation-Context")).eq("RequestType=Startup");
@@ -53,6 +53,9 @@ describe("request tracing", function () {
5353
try {
5454
await load(createMockedConnectionString(fakeEndpoint), {
5555
clientOptions,
56+
startupOptions: {
57+
retryEnabled: false
58+
},
5659
keyVaultOptions: {
5760
credential: createMockedTokenCredential()
5861
}
@@ -68,7 +71,7 @@ describe("request tracing", function () {
6871
const replicaCount = 2;
6972
sinon.stub(ConfigurationClientManager.prototype, "getReplicaCount").returns(replicaCount);
7073
try {
71-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
74+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
7275
} catch (e) { /* empty */ }
7376
expect(headerPolicy.headers).not.undefined;
7477
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -80,7 +83,7 @@ describe("request tracing", function () {
8083
it("should detect env in correlation-context header", async () => {
8184
process.env.NODE_ENV = "development";
8285
try {
83-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
86+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
8487
} catch (e) { /* empty */ }
8588
expect(headerPolicy.headers).not.undefined;
8689
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -92,7 +95,7 @@ describe("request tracing", function () {
9295
it("should detect host type in correlation-context header", async () => {
9396
process.env.WEBSITE_SITE_NAME = "website-name";
9497
try {
95-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
98+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
9699
} catch (e) { /* empty */ }
97100
expect(headerPolicy.headers).not.undefined;
98101
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -105,7 +108,7 @@ describe("request tracing", function () {
105108
for (const indicator of ["TRUE", "true"]) {
106109
process.env.AZURE_APP_CONFIGURATION_TRACING_DISABLED = indicator;
107110
try {
108-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
111+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
109112
} catch (e) { /* empty */ }
110113
expect(headerPolicy.headers).not.undefined;
111114
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -124,6 +127,9 @@ describe("request tracing", function () {
124127

125128
const settings = await load(createMockedConnectionString(fakeEndpoint), {
126129
clientOptions,
130+
startupOptions: {
131+
retryEnabled: false
132+
},
127133
refreshOptions: {
128134
enabled: true,
129135
refreshIntervalInMs: 1000,
@@ -157,6 +163,9 @@ describe("request tracing", function () {
157163
]], listKvCallback);
158164

159165
const settings = await load(createMockedConnectionString(fakeEndpoint), {
166+
startupOptions: {
167+
retryEnabled: false
168+
},
160169
featureFlagOptions: {
161170
enabled: true,
162171
selectors: [ {keyFilter: "*"} ],
@@ -195,6 +204,9 @@ describe("request tracing", function () {
195204
]], listKvCallback);
196205

197206
const settings = await load(createMockedConnectionString(fakeEndpoint), {
207+
startupOptions: {
208+
retryEnabled: false
209+
},
198210
featureFlagOptions: {
199211
enabled: true,
200212
selectors: [ {keyFilter: "*"} ],
@@ -231,6 +243,9 @@ describe("request tracing", function () {
231243
]], listKvCallback);
232244

233245
const settings = await load(createMockedConnectionString(fakeEndpoint), {
246+
startupOptions: {
247+
retryEnabled: false
248+
},
234249
featureFlagOptions: {
235250
enabled: true,
236251
selectors: [ {keyFilter: "*"} ],
@@ -268,6 +283,9 @@ describe("request tracing", function () {
268283
]], listKvCallback);
269284

270285
const settings = await load(createMockedConnectionString(fakeEndpoint), {
286+
startupOptions: {
287+
retryEnabled: false
288+
},
271289
featureFlagOptions: {
272290
enabled: true,
273291
selectors: [ {keyFilter: "*"} ],
@@ -330,7 +348,7 @@ describe("request tracing", function () {
330348
(global as any).importScripts = function importScripts() { };
331349

332350
try {
333-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
351+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
334352
} catch (e) { /* empty */ }
335353
expect(headerPolicy.headers).not.undefined;
336354
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -348,7 +366,7 @@ describe("request tracing", function () {
348366
(global as any).importScripts = function importScripts() { };
349367

350368
try {
351-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
369+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
352370
} catch (e) { /* empty */ }
353371
expect(headerPolicy.headers).not.undefined;
354372
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -366,7 +384,7 @@ describe("request tracing", function () {
366384
(global as any).importScripts = function importScripts() { };
367385

368386
try {
369-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
387+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
370388
} catch (e) { /* empty */ }
371389
expect(headerPolicy.headers).not.undefined;
372390
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -384,7 +402,7 @@ describe("request tracing", function () {
384402
(global as any).importScripts = function importScripts() { };
385403

386404
try {
387-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
405+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
388406
} catch (e) { /* empty */ }
389407
expect(headerPolicy.headers).not.undefined;
390408
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -402,7 +420,7 @@ describe("request tracing", function () {
402420
(global as any).importScripts = undefined;
403421

404422
try {
405-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
423+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
406424
} catch (e) { /* empty */ }
407425
expect(headerPolicy.headers).not.undefined;
408426
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -440,7 +458,7 @@ describe("request tracing", function () {
440458
(global as any).document = new (global as any).Document();
441459

442460
try {
443-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
461+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
444462
} catch (e) { /* empty */ }
445463
expect(headerPolicy.headers).not.undefined;
446464
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -455,7 +473,7 @@ describe("request tracing", function () {
455473
(global as any).document = undefined; // not an instance of Document
456474

457475
try {
458-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
476+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
459477
} catch (e) { /* empty */ }
460478
expect(headerPolicy.headers).not.undefined;
461479
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -470,7 +488,7 @@ describe("request tracing", function () {
470488
(global as any).document = {}; // Not an instance of Document
471489

472490
try {
473-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
491+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
474492
} catch (e) { /* empty */ }
475493
expect(headerPolicy.headers).not.undefined;
476494
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -485,7 +503,7 @@ describe("request tracing", function () {
485503
(global as any).document = new (global as any).Document();
486504

487505
try {
488-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
506+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
489507
} catch (e) { /* empty */ }
490508
expect(headerPolicy.headers).not.undefined;
491509
const correlationContext = headerPolicy.headers.get("Correlation-Context");
@@ -500,7 +518,7 @@ describe("request tracing", function () {
500518
(global as any).document = new (global as any).Document();
501519

502520
try {
503-
await load(createMockedConnectionString(fakeEndpoint), { clientOptions });
521+
await load(createMockedConnectionString(fakeEndpoint), { clientOptions, startupOptions: {retryEnabled: false} });
504522
} catch (e) { /* empty */ }
505523
expect(headerPolicy.headers).not.undefined;
506524
const correlationContext = headerPolicy.headers.get("Correlation-Context");

test/startup.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT license.
3+
4+
import * as chai from "chai";
5+
import * as chaiAsPromised from "chai-as-promised";
6+
chai.use(chaiAsPromised);
7+
const expect = chai.expect;
8+
import { load } from "./exportedApi";
9+
import { MAX_TIME_OUT, createMockedConnectionString, createMockedKeyValue, mockAppConfigurationClientListConfigurationSettings, restoreMocks } from "./utils/testHelper.js";
10+
11+
describe("startup", function () {
12+
this.timeout(MAX_TIME_OUT);
13+
14+
afterEach(() => {
15+
restoreMocks();
16+
});
17+
18+
it("should throw exception when timeout", async () => {
19+
expect(load(createMockedConnectionString(), {startupOptions: {timeoutInMs: 1}}))
20+
.eventually.rejectedWith("Load operation timed out.");
21+
});
22+
23+
it("should retry for load operation when retryEnabled is true", async () => {
24+
let attempt = 0;
25+
const failForInitialAttempt = () => {
26+
if (attempt < 1) {
27+
attempt += 1;
28+
throw new Error("Failed to list configuration settings.");
29+
}
30+
};
31+
mockAppConfigurationClientListConfigurationSettings(
32+
[[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)],
33+
failForInitialAttempt);
34+
35+
const settings = await load(
36+
createMockedConnectionString(), {
37+
startupOptions: {
38+
retryEnabled: true
39+
}
40+
}
41+
);
42+
expect(settings).not.undefined;
43+
expect(settings.get("TestKey")).eq("TestValue");
44+
});
45+
46+
it("should not retry for load operation when retryEnabled is false", async () => {
47+
let attempt = 0;
48+
const failForInitialAttempt = () => {
49+
if (attempt < 1) {
50+
attempt += 1;
51+
throw new Error("Test Error");
52+
}
53+
};
54+
mockAppConfigurationClientListConfigurationSettings(
55+
[[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)],
56+
failForInitialAttempt);
57+
58+
return expect(load(createMockedConnectionString(), {
59+
startupOptions: {
60+
retryEnabled: false
61+
}
62+
})).eventually.rejectedWith("Test Error");
63+
});
64+
});

0 commit comments

Comments
 (0)