Skip to content

Commit

Permalink
fix plugin pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Aug 16, 2024
1 parent 6dbef1e commit 0ae8a07
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CorePlugin, PluginType, sleep } from '@segment/analytics-core'
import {
createMockFetchImplementation,
createRemotePlugin,
getBufferedPageCtxFixture,
} from '../../test-helpers/fixtures'
import unfetch from 'unfetch'
Expand Down Expand Up @@ -96,14 +97,8 @@ describe('Lazy destination loading', () => {
google: {},
},
remotePlugins: [
{
name: 'braze',
libraryName: 'braze',
},
{
name: 'google',
libraryName: 'google',
},
createRemotePlugin('braze'),
createRemotePlugin('google'),
],
})
)
Expand All @@ -113,66 +108,66 @@ describe('Lazy destination loading', () => {

describe('critical plugins (plugins that block the event pipeline)', () => {
test('pipeline _will_ wait for *enrichment* plugins to load', async () => {
jest.mocked(unfetch).mockImplementation(
createMockFetchImplementation({
remotePlugins: [],
})
)
const testEnrichmentHarness = createTestPluginFactory(
'enrichIt',
'enrichment'
)
const dest1Harness = createTestPluginFactory('braze', 'destination')
const dest2Harness = createTestPluginFactory('google', 'destination')

const analytics = new AnalyticsBrowser()

const testEnrichmentPlugin = testEnrichmentHarness.factory(
null
) as CorePlugin
const testPlugin = testEnrichmentHarness.factory(null) as CorePlugin

analytics.register(testEnrichmentPlugin).catch(() => {})
analytics.register(testPlugin).catch(() => {})
analytics.track('test event 1').catch(() => {})

const analyticsLoaded = analytics.load({
writeKey: 'abc',
plugins: [dest1Harness.factory, dest2Harness.factory],
plugins: [],
})
dest1Harness.loadingGuard.resolve()

expect(testEnrichmentHarness.trackSpy).not.toHaveBeenCalled()
expect(dest1Harness.trackSpy).not.toHaveBeenCalled()

// now we'll let the enrichment plugin load
testEnrichmentHarness.loadingGuard.resolve()

await analyticsLoaded
await sleep(200)
expect(testEnrichmentHarness.trackSpy).toHaveBeenCalledTimes(1)
expect(dest1Harness.trackSpy).toHaveBeenCalledTimes(1)
})

test('pipeline _will_ wait for *before* plugins to load', async () => {
const analytics = new AnalyticsBrowser()
const testEnrichmentHarness = createTestPluginFactory(
'enrichIt',
'before'
jest.mocked(unfetch).mockImplementation(
createMockFetchImplementation({
remotePlugins: [],
})
)
const testBeforeHarness = createTestPluginFactory('enrichIt', 'before')

const dest1Harness = createTestPluginFactory('braze', 'destination')
const testEnrichmentPlugin = testEnrichmentHarness.factory(
null
) as CorePlugin
const analytics = new AnalyticsBrowser()

const testPlugin = testBeforeHarness.factory(null) as CorePlugin

analytics.register(testPlugin).catch(() => {})
analytics.track('test event 1').catch(() => {})
analytics.register(testEnrichmentPlugin).catch(() => {})
analytics.load({ writeKey: 'abc', plugins: [dest1Harness.factory] })
dest1Harness.loadingGuard.resolve()

await sleep(50)
expect(testEnrichmentHarness.trackSpy).not.toHaveBeenCalled()
expect(dest1Harness.trackSpy).not.toHaveBeenCalled()
const analyticsLoaded = analytics.load({
writeKey: 'abc',
plugins: [],
})

// now we'll let the enrichment plugin load
await testEnrichmentHarness.loadingGuard.resolve()
await sleep(50)
expect(testEnrichmentHarness.trackSpy).toHaveBeenCalledTimes(1)
expect(dest1Harness.trackSpy).toHaveBeenCalledTimes(1)
expect(testBeforeHarness.trackSpy).not.toHaveBeenCalled()

// now we'll let the before plugin load
testBeforeHarness.loadingGuard.resolve()

await analyticsLoaded
await sleep(200)
expect(testBeforeHarness.trackSpy).toHaveBeenCalledTimes(1)
})
})

Expand Down Expand Up @@ -204,9 +199,6 @@ describe('Lazy destination loading', () => {
// and we'll also let one destination load so we can assert some behaviours
dest1Harness.loadingGuard.resolve()

await testEnrichmentHarness.loadPromise
await dest1Harness.loadPromise

analytics.track('test event 1').catch(() => {})

// even though there's one destination that still hasn't loaded, the next assertions
Expand Down
1 change: 0 additions & 1 deletion packages/browser/src/lib/load-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export function loadScript(
script.onerror = (): void => {
script.onerror = script.onload = null
script.setAttribute('status', 'error')
console.error(`Failed to load ${src}`)
reject(new Error(`Failed to load ${src}`))
}

Expand Down
14 changes: 14 additions & 0 deletions packages/browser/src/test-helpers/fixtures/cdn-settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AnalyticsBrowserSettings } from '../..'
import type { RemotePlugin } from '../../plugins/remote-loader'
import { mockIntegrationName } from './classic-destination'

type CDNSettings = NonNullable<AnalyticsBrowserSettings['cdnSettings']>
Expand Down Expand Up @@ -299,3 +300,16 @@ export const cdnSettingsMinimal: CDNSettings = {
[mockIntegrationName]: {},
},
}

export const createRemotePlugin = (
name: string,
creationName?: string
): RemotePlugin => {
return {
name,
url: 'https://foo.com/v1/projects/abc/plugins/def.js',
creationName: creationName || name,
libraryName: name,
settings: {},
}
}

0 comments on commit 0ae8a07

Please sign in to comment.