@@ -6,10 +6,17 @@ import * as chaiAsPromised from "chai-as-promised";
66chai . use ( chaiAsPromised ) ;
77const expect = chai . expect ;
88import { 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" ;
1010import { AppConfigurationClient } from "@azure/app-configuration" ;
1111import { 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+
1320describe ( "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} ) ;
0 commit comments