diff --git a/src/__tests__/LDClient-events-test.js b/src/__tests__/LDClient-events-test.js index 8ed5e28..b345127 100644 --- a/src/__tests__/LDClient-events-test.js +++ b/src/__tests__/LDClient-events-test.js @@ -1,6 +1,6 @@ import * as messages from '../messages'; -import { withCloseable } from 'launchdarkly-js-test-helpers'; +import { withCloseable, sleepAsync } from 'launchdarkly-js-test-helpers'; import { respond, respondJson } from './mockHttp'; import * as stubPlatform from './stubPlatform'; @@ -426,6 +426,28 @@ describe('LDClient events', () => { }); }); + async function expectDiagnosticEventAndDiscardRegularEvent(server) { + const req0 = await server.nextRequest(); + const req1 = await server.nextRequest(); + const expectedPath = '/events/diagnostic/' + envName; + const otherPath = '/events/bulk/' + envName; + let initEventReq; + if (req0.path === expectedPath) { + expect(req1.path).toEqual(otherPath); + initEventReq = req0; + } else { + expect(req0.path).toEqual(otherPath); + expect(req1.path).toEqual(expectedPath); + initEventReq = req1; + } + return JSON.parse(initEventReq.body); + } + + async function expectNoMoreRequests(server, timeout) { + await sleepAsync(timeout); + expect(server.requests.length()).toEqual(0); + } + it('sends diagnostic init event on startup', async () => { const server = platform.testing.http.newServer(); server.byDefault(respond(202)); @@ -439,15 +461,11 @@ describe('LDClient events', () => { await withCloseable(client, async () => { await client.waitForInitialization(); await client.flush(); - const req0 = await server.nextRequest(); - const req1 = await server.nextRequest(); - const expectedPath = '/events/diagnostic/' + envName; - expect([req0.path, req1.path]).toContain(expectedPath); - const req = req0.path === expectedPath ? req0 : req1; - const data = JSON.parse(req.body); + const data = await expectDiagnosticEventAndDiscardRegularEvent(server); expect(data.kind).toEqual('diagnostic-init'); expect(data.platform).toEqual(platform.diagnosticPlatformData); expect(data.sdk).toEqual(platform.diagnosticSdkData); + await expectNoMoreRequests(server, 50); }); }); }); @@ -467,15 +485,11 @@ describe('LDClient events', () => { await withCloseable(client, async () => { await client.waitForInitialization(); await client.flush(); - const req0 = await server.nextRequest(); - const req1 = await server.nextRequest(); - const expectedPath = '/events/diagnostic/' + envName; - expect([req0.path, req1.path]).toContain(expectedPath); - const req = req0.path === expectedPath ? req0 : req1; - const data = JSON.parse(req.body); + const data = await expectDiagnosticEventAndDiscardRegularEvent(server); expect(data.kind).toEqual('diagnostic-combined'); expect(data.platform).toEqual(platform1.diagnosticPlatformData); expect(data.sdk).toEqual(platform1.diagnosticSdkData); + await expectNoMoreRequests(server, 50); }); }); }); @@ -497,6 +511,7 @@ describe('LDClient events', () => { expect(server.requests.length()).toEqual(1); const req = await server.nextRequest(); expect(req.path).toEqual('/events/bulk/' + envName); + await expectNoMoreRequests(server, 50); }); }); }); diff --git a/src/__tests__/stubPlatform.js b/src/__tests__/stubPlatform.js index 8a5dcac..18ff9a9 100644 --- a/src/__tests__/stubPlatform.js +++ b/src/__tests__/stubPlatform.js @@ -83,7 +83,14 @@ export function defaults() { makeClient: (env, user, options = {}) => { const config = { logger: p.testing.logger, ...options }; - return LDClient.initialize(env, user, config, p).client; + // We want to simulate what the platform-specific SDKs will do in their own initialization functions. + // They will call the common package's LDClient.initialize() and receive the clientVars object which + // contains both the underlying client (in its "client" property) and some internal methods that the + // platform-specific SDKs can use to do internal stuff. One of those is start(), which they will + // call after doing any other initialization things they may need to do. + const clientVars = LDClient.initialize(env, user, config, p); + clientVars.start(); + return clientVars.client; }, setCurrentUrl: url => { diff --git a/src/index.js b/src/index.js index 8d07fe9..7fb8beb 100644 --- a/src/index.js +++ b/src/index.js @@ -43,9 +43,6 @@ export function initialize(env, user, specifiedOptions, platform, extraOptionDef const diagnosticsManager = diagnosticsEnabled ? diagnostics.DiagnosticsManager(platform, diagnosticsAccumulator, eventSender, environment, options, diagnosticId) : null; - if (diagnosticsManager) { - diagnosticsManager.start(); - } const stream = Stream(platform, options, environment, diagnosticsAccumulator);