Skip to content

Commit 5c5866e

Browse files
committed
improve test
1 parent e4ca372 commit 5c5866e

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

src/ConfigurationClientWrapper.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class ConfigurationClientWrapper {
1212
endpoint: string;
1313
client: AppConfigurationClient;
1414
backoffEndTime: number = 0; // Timestamp
15-
failedAttempts: number = 0;
15+
#failedAttempts: number = 0;
1616

1717
constructor(endpoint: string, client: AppConfigurationClient) {
1818
this.endpoint = endpoint;
@@ -21,17 +21,11 @@ export class ConfigurationClientWrapper {
2121

2222
updateBackoffStatus(successfull: boolean) {
2323
if (successfull) {
24-
if (this.failedAttempts > 0) {
25-
this.failedAttempts = 0;
26-
}
27-
this.failedAttempts -= 1;
24+
this.#failedAttempts = 0;
2825
this.backoffEndTime = Date.now();
2926
} else {
30-
if (this.failedAttempts < 0) {
31-
this.failedAttempts = 0;
32-
}
33-
this.failedAttempts += 1;
34-
this.backoffEndTime = Date.now() + calculateBackoffDuration(this.failedAttempts);
27+
this.#failedAttempts += 1;
28+
this.backoffEndTime = Date.now() + calculateBackoffDuration(this.#failedAttempts);
3529
}
3630
}
3731
}

test/loadBalance.test.ts

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ import * as chaiAsPromised from "chai-as-promised";
66
chai.use(chaiAsPromised);
77
const expect = chai.expect;
88
import { load } from "./exportedApi.js";
9-
import { mockAppConfigurationClientListConfigurationSettings, restoreMocks, createMockedConnectionString, sleepInMs, createMockedFeatureFlag, createMockedEndpoint, mockConfigurationManagerGetClients } from "./utils/testHelper.js";
9+
import { restoreMocks, createMockedConnectionString, sleepInMs, createMockedEndpoint, mockConfigurationManagerGetClients, mockAppConfigurationClientLoadBalanceMode } from "./utils/testHelper.js";
1010
import { AppConfigurationClient } from "@azure/app-configuration";
1111
import { ConfigurationClientWrapper } from "../src/ConfigurationClientWrapper.js";
1212

13+
const fakeEndpoint_1 = createMockedEndpoint("fake_1");
14+
const fakeEndpoint_2 = createMockedEndpoint("fake_2");
15+
const fakeClientWrapper_1 = new ConfigurationClientWrapper(fakeEndpoint_1, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_1)));
16+
const fakeClientWrapper_2 = new ConfigurationClientWrapper(fakeEndpoint_2, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_2)));
17+
const clientRequestCounter_1 = {count: 0};
18+
const clientRequestCounter_2 = {count: 0};
19+
1320
describe("load balance", function () {
1421
this.timeout(10000);
1522

@@ -21,22 +28,9 @@ describe("load balance", function () {
2128
});
2229

2330
it("should load balance the request when loadBalancingEnabled", async () => {
24-
// mock multiple pages of feature flags
25-
const page1 = [
26-
createMockedFeatureFlag("Alpha_1", { enabled: true }),
27-
createMockedFeatureFlag("Alpha_2", { enabled: true }),
28-
];
29-
const page2 = [
30-
createMockedFeatureFlag("Beta_1", { enabled: true }),
31-
createMockedFeatureFlag("Beta_2", { enabled: true }),
32-
];
33-
const fakeEndpoint_1 = createMockedEndpoint("fake_1");
34-
const fakeEndpoint_2 = createMockedEndpoint("fake_2");
35-
const fakeClientWrapper_1 = new ConfigurationClientWrapper(fakeEndpoint_1, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_1)));
36-
const fakeClientWrapper_2 = new ConfigurationClientWrapper(fakeEndpoint_2, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_2)));
37-
const mockedClientWrappers = [fakeClientWrapper_1, fakeClientWrapper_2];
38-
mockConfigurationManagerGetClients(mockedClientWrappers, false);
39-
mockAppConfigurationClientListConfigurationSettings(page1, page2);
31+
mockConfigurationManagerGetClients([fakeClientWrapper_1, fakeClientWrapper_2], false);
32+
mockAppConfigurationClientLoadBalanceMode(fakeClientWrapper_1, clientRequestCounter_1);
33+
mockAppConfigurationClientLoadBalanceMode(fakeClientWrapper_2, clientRequestCounter_2);
4034

4135
const connectionString = createMockedConnectionString();
4236
const settings = await load(connectionString, {
@@ -53,38 +47,27 @@ describe("load balance", function () {
5347
}
5448
});
5549
// one request for key values, one request for feature flags
56-
expect(fakeClientWrapper_1.failedAttempts).eq(-1);
57-
expect(fakeClientWrapper_2.failedAttempts).eq(-1);
50+
expect(clientRequestCounter_1.count).eq(1);
51+
expect(clientRequestCounter_2.count).eq(1);
5852

5953
await sleepInMs(2 * 1000 + 1);
6054
await settings.refresh();
6155
// refresh request for feature flags
62-
expect(fakeClientWrapper_1.failedAttempts).eq(-2);
63-
expect(fakeClientWrapper_2.failedAttempts).eq(-1);
56+
expect(clientRequestCounter_1.count).eq(2);
57+
expect(clientRequestCounter_2.count).eq(1);
6458

6559
await sleepInMs(2 * 1000 + 1);
6660
await settings.refresh();
67-
expect(fakeClientWrapper_1.failedAttempts).eq(-2);
68-
expect(fakeClientWrapper_2.failedAttempts).eq(-2);
61+
expect(clientRequestCounter_1.count).eq(2);
62+
expect(clientRequestCounter_2.count).eq(2);
6963
});
7064

71-
it("should load balance the request when loadBalancingEnabled", async () => {
72-
// mock multiple pages of feature flags
73-
const page1 = [
74-
createMockedFeatureFlag("Alpha_1", { enabled: true }),
75-
createMockedFeatureFlag("Alpha_2", { enabled: true }),
76-
];
77-
const page2 = [
78-
createMockedFeatureFlag("Beta_1", { enabled: true }),
79-
createMockedFeatureFlag("Beta_2", { enabled: true }),
80-
];
81-
const fakeEndpoint_1 = createMockedEndpoint("fake_1");
82-
const fakeEndpoint_2 = createMockedEndpoint("fake_2");
83-
const fakeClientWrapper_1 = new ConfigurationClientWrapper(fakeEndpoint_1, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_1)));
84-
const fakeClientWrapper_2 = new ConfigurationClientWrapper(fakeEndpoint_2, new AppConfigurationClient(createMockedConnectionString(fakeEndpoint_2)));
85-
const mockedClientWrappers = [fakeClientWrapper_1, fakeClientWrapper_2];
86-
mockConfigurationManagerGetClients(mockedClientWrappers, false);
87-
mockAppConfigurationClientListConfigurationSettings(page1, page2);
65+
it("should not load balance the request when loadBalance disabled", async () => {
66+
clientRequestCounter_1.count = 0;
67+
clientRequestCounter_2.count = 0;
68+
mockConfigurationManagerGetClients([fakeClientWrapper_1, fakeClientWrapper_2], false);
69+
mockAppConfigurationClientLoadBalanceMode(fakeClientWrapper_1, clientRequestCounter_1);
70+
mockAppConfigurationClientLoadBalanceMode(fakeClientWrapper_2, clientRequestCounter_2);
8871

8972
const connectionString = createMockedConnectionString();
9073
// loadBalancingEnabled is default to false
@@ -101,13 +84,13 @@ describe("load balance", function () {
10184
}
10285
});
10386
// one request for key values, one request for feature flags
104-
expect(fakeClientWrapper_1.failedAttempts).eq(-2);
105-
expect(fakeClientWrapper_2.failedAttempts).eq(0);
87+
expect(clientRequestCounter_1.count).eq(2);
88+
expect(clientRequestCounter_2.count).eq(0);
10689

10790
await sleepInMs(2 * 1000 + 1);
10891
await settings.refresh();
10992
// refresh request for feature flags
110-
expect(fakeClientWrapper_1.failedAttempts).eq(-3);
111-
expect(fakeClientWrapper_2.failedAttempts).eq(0);
93+
expect(clientRequestCounter_1.count).eq(3);
94+
expect(clientRequestCounter_2.count).eq(0);
11295
});
11396
});

test/utils/testHelper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ function mockAppConfigurationClientListConfigurationSettings(...pages: Configura
100100
});
101101
}
102102

103+
function mockAppConfigurationClientLoadBalanceMode(clientWrapper: ConfigurationClientWrapper, countObject: { count: number }) {
104+
const emptyPages: ConfigurationSetting[][] = [];
105+
sinon.stub(clientWrapper.client, "listConfigurationSettings").callsFake((listOptions) => {
106+
countObject.count += 1;
107+
const kvs = _filterKVs(emptyPages.flat(), listOptions);
108+
return getMockedIterator(emptyPages, kvs, listOptions);
109+
});
110+
}
111+
103112
function mockConfigurationManagerGetClients(fakeClientWrappers: ConfigurationClientWrapper[], isFailoverable: boolean, ...pages: ConfigurationSetting[][]) {
104113
// Stub the getClients method on the class prototype
105114
sinon.stub(ConfigurationClientManager.prototype, "getClients").callsFake(async () => {
@@ -233,6 +242,7 @@ export {
233242
sinon,
234243
mockAppConfigurationClientListConfigurationSettings,
235244
mockAppConfigurationClientGetConfigurationSetting,
245+
mockAppConfigurationClientLoadBalanceMode,
236246
mockConfigurationManagerGetClients,
237247
mockSecretClientGetSecret,
238248
restoreMocks,

0 commit comments

Comments
 (0)