Skip to content

Commit a6f11c8

Browse files
committed
fix tests
1 parent 6235b5b commit a6f11c8

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

packages/replay/src/eventBuffer/EventBufferProxy.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ export class EventBufferProxy implements EventBuffer {
1414
private _fallback: EventBufferArray;
1515
private _compression: EventBufferCompressionWorker;
1616
private _used: EventBuffer;
17+
private _ensureWorkerIsLoadedPromise: Promise<void>;
1718

1819
public constructor(worker: Worker) {
1920
this._fallback = new EventBufferArray();
2021
this._compression = new EventBufferCompressionWorker(worker);
2122
this._used = this._fallback;
2223

23-
void this.ensureWorkerIsLoaded();
24+
this._ensureWorkerIsLoadedPromise = this._ensureWorkerIsLoaded().catch(() => {
25+
// Ignore errors here
26+
});
2427
}
2528

2629
/** @inheritDoc */
@@ -51,17 +54,18 @@ export class EventBufferProxy implements EventBuffer {
5154
/** @inheritDoc */
5255
public async finish(): Promise<ReplayRecordingData> {
5356
// Ensure the worker is loaded, so the sent event is compressed
54-
try {
55-
await this.ensureWorkerIsLoaded();
56-
} catch (error) {
57-
// If this fails, we'll just send uncompressed events
58-
}
57+
await this.ensureWorkerIsLoaded();
5958

6059
return this._used.finish();
6160
}
6261

6362
/** Ensure the worker has loaded. */
64-
public async ensureWorkerIsLoaded(): Promise<void> {
63+
public ensureWorkerIsLoaded(): Promise<void> {
64+
return this._ensureWorkerIsLoadedPromise;
65+
}
66+
67+
/** Actually check if the worker has been loaded. */
68+
private async _ensureWorkerIsLoaded(): Promise<void> {
6569
try {
6670
await this._compression.ensureReady();
6771
} catch (error) {

packages/replay/test/unit/eventBuffer.test.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ describe('Unit | eventBuffer', () => {
132132
buffer.addEvent(TEST_EVENT);
133133

134134
const promise1 = buffer.finish();
135+
await new Promise(process.nextTick);
135136

136137
buffer.addEvent({ ...TEST_EVENT, type: 5 });
137138
const promise2 = buffer.finish();
@@ -146,7 +147,7 @@ describe('Unit | eventBuffer', () => {
146147
expect(restored2).toEqual(JSON.stringify([{ ...TEST_EVENT, type: 5 }]));
147148
});
148149

149-
it('handles an error when compressing the payload xxx', async function () {
150+
it('handles an error when compressing the payload', async function () {
150151
const buffer = createEventBuffer({
151152
useCompression: true,
152153
}) as EventBufferProxy;
@@ -184,7 +185,7 @@ describe('Unit | eventBuffer', () => {
184185
consoleErrorSpy.mockRestore();
185186
});
186187

187-
it('first uses simple buffer, and switches over once worker is loaded', async function () {
188+
it('waits for the worker to be loaded when calling finish', async function () {
188189
const buffer = createEventBuffer({
189190
useCompression: true,
190191
}) as EventBufferProxy;
@@ -196,26 +197,10 @@ describe('Unit | eventBuffer', () => {
196197

197198
expect(buffer.pendingEvents).toEqual([TEST_EVENT, TEST_EVENT]);
198199

199-
// Finish before the worker is loaded
200200
const result = await buffer.finish();
201-
expect(typeof result).toBe('string');
202-
expect(result).toEqual(JSON.stringify([TEST_EVENT, TEST_EVENT]));
203-
204-
// Now actually finish loading the worker
205-
await buffer.ensureWorkerIsLoaded();
206-
207-
buffer.addEvent(TEST_EVENT);
208-
buffer.addEvent(TEST_EVENT);
209-
buffer.addEvent(TEST_EVENT);
210-
211-
expect(buffer.pendingEvents).toEqual([TEST_EVENT, TEST_EVENT, TEST_EVENT]);
212-
213-
const result2 = await buffer.finish();
214-
expect(result2).toBeInstanceOf(Uint8Array);
215-
216-
const restored2 = pako.inflate(result2 as Uint8Array, { to: 'string' });
217-
218-
expect(restored2).toEqual(JSON.stringify([TEST_EVENT, TEST_EVENT, TEST_EVENT]));
201+
expect(result).toBeInstanceOf(Uint8Array);
202+
const restored = pako.inflate(result as Uint8Array, { to: 'string' });
203+
expect(restored).toEqual(JSON.stringify([TEST_EVENT, TEST_EVENT]));
219204
});
220205

221206
it('keeps using simple buffer if worker cannot be loaded', async function () {

0 commit comments

Comments
 (0)