-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
292 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-signals': patch | ||
--- | ||
|
||
Scope signal buffer to session |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
'@segment/analytics-signals': minor | ||
--- | ||
|
||
* Clear signal buffer at start of new session | ||
* Prune signalBuffer to maxBufferSize on new session (if different) | ||
* Add sessionStorage storage type |
117 changes: 107 additions & 10 deletions
117
packages/signals/signals/src/core/buffer/__tests__/buffer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,123 @@ | ||
import { sleep } from '@segment/analytics-core' | ||
import { range } from '../../../test-helpers/range' | ||
import { createInteractionSignal } from '../../../types/factories' | ||
import { getSignalBuffer, SignalBuffer } from '../index' | ||
|
||
describe(getSignalBuffer, () => { | ||
let buffer: SignalBuffer | ||
beforeEach(async () => { | ||
sessionStorage.clear() | ||
buffer = getSignalBuffer({ | ||
maxBufferSize: 10, | ||
}) | ||
await buffer.clear() | ||
}) | ||
describe('indexDB', () => { | ||
it('should instantiate without throwing an error', () => { | ||
expect(buffer).toBeTruthy() | ||
}) | ||
it('should add and clear', async () => { | ||
const mockSignal = createInteractionSignal({ | ||
eventType: 'submit', | ||
target: {}, | ||
}) | ||
await buffer.add(mockSignal) | ||
await expect(buffer.getAll()).resolves.toEqual([mockSignal]) | ||
await buffer.clear() | ||
await expect(buffer.getAll()).resolves.toHaveLength(0) | ||
}) | ||
|
||
it('should delete older signals when maxBufferSize is exceeded', async () => { | ||
const signals = range(15).map((_, idx) => | ||
createInteractionSignal({ | ||
idx: idx, | ||
eventType: 'change', | ||
target: {}, | ||
}) | ||
) | ||
|
||
for (const signal of signals) { | ||
await buffer.add(signal) | ||
} | ||
|
||
const storedSignals = await buffer.getAll() | ||
expect(storedSignals).toHaveLength(10) | ||
expect(storedSignals).toEqual(signals.slice(-10).reverse()) | ||
}) | ||
|
||
it('should delete older signals on initialize if current number exceeds maxBufferSize', async () => { | ||
const signals = range(15).map((_, idx) => | ||
createInteractionSignal({ | ||
idx: idx, | ||
eventType: 'change', | ||
target: {}, | ||
}) | ||
) | ||
|
||
for (const signal of signals) { | ||
await buffer.add(signal) | ||
} | ||
|
||
// Re-initialize buffer | ||
buffer = getSignalBuffer({ | ||
maxBufferSize: 10, | ||
}) | ||
|
||
const storedSignals = await buffer.getAll() | ||
expect(storedSignals).toHaveLength(10) | ||
expect(storedSignals).toEqual(signals.slice(-10).reverse()) | ||
}) | ||
|
||
it('should instantiate without throwing an error', () => { | ||
expect(buffer).toBeTruthy() | ||
it('should clear signal buffer if there is a new session according to session storage', async () => { | ||
const mockSignal = createInteractionSignal({ | ||
eventType: 'submit', | ||
target: {}, | ||
}) | ||
await buffer.add(mockSignal) | ||
await expect(buffer.getAll()).resolves.toEqual([mockSignal]) | ||
|
||
// Simulate a new session by clearing session storage and re-initializing the buffer | ||
sessionStorage.clear() | ||
await sleep(100) | ||
buffer = getSignalBuffer({ | ||
maxBufferSize: 10, | ||
}) | ||
|
||
await expect(buffer.getAll()).resolves.toHaveLength(0) | ||
}) | ||
}) | ||
it('should add and clear', async () => { | ||
const mockSignal = createInteractionSignal({ | ||
eventType: 'submit', | ||
target: {}, | ||
describe('sessionStorage', () => { | ||
it('should instantiate without throwing an error', () => { | ||
expect(buffer).toBeTruthy() | ||
}) | ||
|
||
it('should add and clear', async () => { | ||
const mockSignal = createInteractionSignal({ | ||
eventType: 'submit', | ||
target: {}, | ||
}) | ||
await buffer.add(mockSignal) | ||
await expect(buffer.getAll()).resolves.toEqual([mockSignal]) | ||
await buffer.clear() | ||
await expect(buffer.getAll()).resolves.toHaveLength(0) | ||
}) | ||
|
||
it('should delete older signals when maxBufferSize is exceeded', async () => { | ||
const signals = range(15).map((_, idx) => | ||
createInteractionSignal({ | ||
idx: idx, | ||
eventType: 'change', | ||
target: {}, | ||
}) | ||
) | ||
|
||
for (const signal of signals) { | ||
await buffer.add(signal) | ||
} | ||
|
||
const storedSignals = await buffer.getAll() | ||
expect(storedSignals).toHaveLength(10) | ||
expect(storedSignals).toEqual(signals.slice(-10).reverse()) | ||
}) | ||
await buffer.add(mockSignal) | ||
await expect(buffer.getAll()).resolves.toEqual([mockSignal]) | ||
await buffer.clear() | ||
await expect(buffer.getAll()).resolves.toHaveLength(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.