@@ -33,6 +33,9 @@ import { ElasticsearchService } from './elasticsearch_service';
3333import { elasticsearchServiceMock } from './elasticsearch_service.mock' ;
3434import { duration } from 'moment' ;
3535
36+ const delay = async ( durationMs : number ) =>
37+ await new Promise ( resolve => setTimeout ( resolve , durationMs ) ) ;
38+
3639let elasticsearchService : ElasticsearchService ;
3740const configService = configServiceMock . create ( ) ;
3841const deps = {
@@ -42,7 +45,7 @@ configService.atPath.mockReturnValue(
4245 new BehaviorSubject ( {
4346 hosts : [ 'http://1.2.3.4' ] ,
4447 healthCheck : {
45- delay : duration ( 2000 ) ,
48+ delay : duration ( 10 ) ,
4649 } ,
4750 ssl : {
4851 verificationMode : 'none' ,
@@ -125,21 +128,21 @@ describe('#setup', () => {
125128
126129 const config = MockClusterClient . mock . calls [ 0 ] [ 0 ] ;
127130 expect ( config ) . toMatchInlineSnapshot ( `
128- Object {
129- "healthCheckDelay": "PT2S ",
130- "hosts": Array [
131- "http://8.8.8.8",
132- ],
133- "logQueries": true,
134- "requestHeadersWhitelist": Array [
135- undefined,
136- ],
137- "ssl": Object {
138- "certificate": "certificate-value",
139- "verificationMode": "none",
140- },
141- }
142- ` ) ;
131+ Object {
132+ "healthCheckDelay": "PT0.01S ",
133+ "hosts": Array [
134+ "http://8.8.8.8",
135+ ],
136+ "logQueries": true,
137+ "requestHeadersWhitelist": Array [
138+ undefined,
139+ ],
140+ "ssl": Object {
141+ "certificate": "certificate-value",
142+ "verificationMode": "none",
143+ },
144+ }
145+ `) ;
143146 } ) ;
144147 it ( 'falls back to elasticsearch config if custom config not passed' , async ( ) => {
145148 const setupContract = await elasticsearchService . setup ( deps ) ;
@@ -150,24 +153,24 @@ Object {
150153
151154 const config = MockClusterClient . mock . calls [ 0 ] [ 0 ] ;
152155 expect ( config ) . toMatchInlineSnapshot ( `
153- Object {
154- "healthCheckDelay": "PT2S ",
155- "hosts": Array [
156- "http://1.2.3.4",
157- ],
158- "requestHeadersWhitelist": Array [
159- undefined,
160- ],
161- "ssl": Object {
162- "alwaysPresentCertificate": undefined,
163- "certificate": undefined,
164- "certificateAuthorities": undefined,
165- "key": undefined,
166- "keyPassphrase": undefined,
167- "verificationMode": "none",
168- },
169- }
170- ` ) ;
156+ Object {
157+ "healthCheckDelay": "PT0.01S ",
158+ "hosts": Array [
159+ "http://1.2.3.4",
160+ ],
161+ "requestHeadersWhitelist": Array [
162+ undefined,
163+ ],
164+ "ssl": Object {
165+ "alwaysPresentCertificate": undefined,
166+ "certificate": undefined,
167+ "certificateAuthorities": undefined,
168+ "key": undefined,
169+ "keyPassphrase": undefined,
170+ "verificationMode": "none",
171+ },
172+ }
173+ `) ;
171174 } ) ;
172175
173176 it ( 'does not merge elasticsearch hosts if custom config overrides' , async ( ) => {
@@ -213,6 +216,45 @@ Object {
213216 ` ) ;
214217 } ) ;
215218 } ) ;
219+
220+ it ( 'esNodeVersionCompatibility$ only starts polling when subscribed to' , async done => {
221+ const mockAdminClusterClientInstance = elasticsearchServiceMock . createClusterClient ( ) ;
222+ const mockDataClusterClientInstance = elasticsearchServiceMock . createClusterClient ( ) ;
223+ MockClusterClient . mockImplementationOnce (
224+ ( ) => mockAdminClusterClientInstance
225+ ) . mockImplementationOnce ( ( ) => mockDataClusterClientInstance ) ;
226+
227+ mockAdminClusterClientInstance . callAsInternalUser . mockRejectedValue ( new Error ( ) ) ;
228+
229+ const setupContract = await elasticsearchService . setup ( deps ) ;
230+ await delay ( 10 ) ;
231+
232+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 0 ) ;
233+ setupContract . esNodesCompatibility$ . subscribe ( ( ) => {
234+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 1 ) ;
235+ done ( ) ;
236+ } ) ;
237+ } ) ;
238+
239+ it ( 'esNodeVersionCompatibility$ stops polling when unsubscribed from' , async done => {
240+ const mockAdminClusterClientInstance = elasticsearchServiceMock . createClusterClient ( ) ;
241+ const mockDataClusterClientInstance = elasticsearchServiceMock . createClusterClient ( ) ;
242+ MockClusterClient . mockImplementationOnce (
243+ ( ) => mockAdminClusterClientInstance
244+ ) . mockImplementationOnce ( ( ) => mockDataClusterClientInstance ) ;
245+
246+ mockAdminClusterClientInstance . callAsInternalUser . mockRejectedValue ( new Error ( ) ) ;
247+
248+ const setupContract = await elasticsearchService . setup ( deps ) ;
249+
250+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 0 ) ;
251+ const sub = setupContract . esNodesCompatibility$ . subscribe ( async ( ) => {
252+ sub . unsubscribe ( ) ;
253+ await delay ( 100 ) ;
254+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 1 ) ;
255+ done ( ) ;
256+ } ) ;
257+ } ) ;
216258} ) ;
217259
218260describe ( '#stop' , ( ) => {
@@ -229,4 +271,27 @@ describe('#stop', () => {
229271 expect ( mockAdminClusterClientInstance . close ) . toHaveBeenCalledTimes ( 1 ) ;
230272 expect ( mockDataClusterClientInstance . close ) . toHaveBeenCalledTimes ( 1 ) ;
231273 } ) ;
274+
275+ it ( 'stops pollEsNodeVersions even if there are active subscriptions' , async done => {
276+ expect . assertions ( 2 ) ;
277+ const mockAdminClusterClientInstance = elasticsearchServiceMock . createCustomClusterClient ( ) ;
278+ const mockDataClusterClientInstance = elasticsearchServiceMock . createCustomClusterClient ( ) ;
279+
280+ MockClusterClient . mockImplementationOnce (
281+ ( ) => mockAdminClusterClientInstance
282+ ) . mockImplementationOnce ( ( ) => mockDataClusterClientInstance ) ;
283+
284+ mockAdminClusterClientInstance . callAsInternalUser . mockRejectedValue ( new Error ( ) ) ;
285+
286+ const setupContract = await elasticsearchService . setup ( deps ) ;
287+
288+ setupContract . esNodesCompatibility$ . subscribe ( async ( ) => {
289+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 1 ) ;
290+
291+ await elasticsearchService . stop ( ) ;
292+ await delay ( 100 ) ;
293+ expect ( mockAdminClusterClientInstance . callAsInternalUser ) . toHaveBeenCalledTimes ( 1 ) ;
294+ done ( ) ;
295+ } ) ;
296+ } ) ;
232297} ) ;
0 commit comments