Skip to content

Commit

Permalink
[RUMF-1530] ✅ use new helper to read metadata in segmentCollection.spec
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Mar 30, 2023
1 parent 296af8a commit 50668f5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
54 changes: 25 additions & 29 deletions packages/rum/src/domain/segmentCollection/segmentCollection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { mockClock, restorePageVisibility } from '@datadog/browser-core/test'
import { createRumSessionManagerMock } from '../../../../rum-core/test'
import type { BrowserRecord, SegmentContext } from '../../types'
import { RecordType } from '../../types'
import { MockWorker } from '../../../test'
import { MockWorker, readMetadataFromReplayPayload } from '../../../test'
import {
computeSegmentContext,
doStartSegmentCollection,
Expand Down Expand Up @@ -51,14 +51,8 @@ describe('startSegmentCollection', () => {
lifeCycle.notify(LifeCycleEventType.PAGE_EXITED, { reason: PageExitReason.UNLOADING })
}

function getSentFormData(spy: jasmine.Spy<HttpRequest['send']>) {
const payload = spy.calls.mostRecent().args[0]

if (!(payload.data instanceof FormData)) {
throw new Error('SegmentCollection unexpectedly sent a payload without FormData')
}

return payload.data
function readMostRecentMetadata(spy: jasmine.Spy<HttpRequest['send']>) {
return readMetadataFromReplayPayload(spy.calls.mostRecent().args[0])
}

beforeEach(() => {
Expand Down Expand Up @@ -95,9 +89,9 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.sendOnExit).not.toHaveBeenCalled()
})

it('creation reason should reflect that it is the initial segment', () => {
it('creation reason should reflect that it is the initial segment', async () => {
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('init')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('init')
})
})

Expand Down Expand Up @@ -134,10 +128,10 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.sendOnExit).toHaveBeenCalled()
})

it('next segment is created because of beforeunload event', () => {
it('next segment is created because of beforeunload event', async () => {
addRecordAndFlushSegment(emulatePageUnload)
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('before_unload')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('before_unload')
})
})

Expand All @@ -151,10 +145,10 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.sendOnExit).toHaveBeenCalled()
})

it('next segment is created because of visibility hidden event', () => {
it('next segment is created because of visibility hidden event', async () => {
addRecordAndFlushSegment(emulatePageHidden)
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('visibility_hidden')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('visibility_hidden')
})
})

Expand All @@ -168,10 +162,10 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.sendOnExit).toHaveBeenCalled()
})

it('next segment is created because of page freeze event', () => {
it('next segment is created because of page freeze event', async () => {
addRecordAndFlushSegment(emulatePageFrozen)
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('page_frozen')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('page_frozen')
})
})

Expand All @@ -185,10 +179,10 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.send).toHaveBeenCalled()
})

it('next segment is created because of view change', () => {
it('next segment is created because of view change', async () => {
addRecordAndFlushSegment(emulateViewChange)
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('view_change')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('view_change')
})
})

Expand All @@ -200,27 +194,27 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.send).toHaveBeenCalled()
})

it('next segment is created because the bytes limit has been reached', () => {
it('next segment is created because the bytes limit has been reached', async () => {
addRecordAndFlushSegment(() => {
addRecord(VERY_BIG_RECORD)
})
addRecordAndFlushSegment()

expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('segment_bytes_limit')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('segment_bytes_limit')
})

it('continues to add records to the current segment while the worker is processing messages', () => {
it('continues to add records to the current segment while the worker is processing messages', async () => {
addRecord(VERY_BIG_RECORD)
addRecord(RECORD)
addRecord(RECORD)
addRecord(RECORD)
worker.processAllMessages()

expect(httpRequestSpy.send).toHaveBeenCalledTimes(1)
expect(getSentFormData(httpRequestSpy.send).get('records_count')).toBe('4')
expect((await readMostRecentMetadata(httpRequestSpy.send)).records_count).toBe('4')
})

it('does not flush segment prematurely when records from the previous segment are still being processed', () => {
it('does not flush segment prematurely when records from the previous segment are still being processed', async () => {
// Add two records to the current segment
addRecord(VERY_BIG_RECORD)
addRecord(RECORD)
Expand All @@ -235,7 +229,7 @@ describe('startSegmentCollection', () => {
worker.processAllMessages()

expect(httpRequestSpy.send).toHaveBeenCalledTimes(1)
expect(getSentFormData(httpRequestSpy.send).get('records_count')).toBe('2')
expect((await readMostRecentMetadata(httpRequestSpy.send)).records_count).toBe('2')
})
})

Expand All @@ -248,16 +242,16 @@ describe('startSegmentCollection', () => {
expect(httpRequestSpy.send).toHaveBeenCalled()
})

it('next segment is created because of the segment duration limit has been reached', () => {
it('next segment is created because of the segment duration limit has been reached', async () => {
clock = mockClock()
addRecordAndFlushSegment(() => {
clock!.tick(SEGMENT_DURATION_LIMIT)
})
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).toBe('segment_duration_limit')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).toBe('segment_duration_limit')
})

it('does not flush a segment after SEGMENT_DURATION_LIMIT if a segment has been created in the meantime', () => {
it('does not flush a segment after SEGMENT_DURATION_LIMIT if a segment has been created in the meantime', async () => {
clock = mockClock()
addRecord(RECORD)
clock.tick(BEFORE_SEGMENT_DURATION_LIMIT)
Expand All @@ -268,7 +262,9 @@ describe('startSegmentCollection', () => {
worker.processAllMessages()
expect(httpRequestSpy.sendOnExit).toHaveBeenCalledTimes(1)
addRecordAndFlushSegment()
expect(getSentFormData(httpRequestSpy.sendOnExit).get('creation_reason')).not.toBe('segment_duration_limit')
expect((await readMostRecentMetadata(httpRequestSpy.sendOnExit)).creation_reason).not.toBe(
'segment_duration_limit'
)
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/rum/test/readReplayPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function readSegmentFromReplayPayload(payload: Payload) {
// In the next commit, this method will change and will be async. This is an intermediary
// implementation to prepare for the real metadata change.
// eslint-disable-next-line @typescript-eslint/require-await
async function readMetadataFromReplayPayload(payload: Payload) {
export async function readMetadataFromReplayPayload(payload: Payload) {
const data = payload.data as FormData
const result: Record<string, string> = {}
data.forEach((value, key) => {
Expand Down

0 comments on commit 50668f5

Please sign in to comment.