Skip to content

Commit de9bda2

Browse files
committed
fix: Tests
1 parent 6965615 commit de9bda2

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

packages/browser/src/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class BrowserBackend implements Backend {
4141
private transport?: Transport;
4242

4343
/** A simple queue holding all requests. */
44-
private queue: Queue<SentryResponse> = new Queue();
44+
private readonly queue: Queue<SentryResponse> = new Queue();
4545

4646
/**
4747
* @inheritDoc
@@ -184,7 +184,7 @@ export class BrowserBackend implements Backend {
184184
/**
185185
* @inheritDoc
186186
*/
187-
public close(timeout?: number): Promise<boolean> {
187+
public async close(timeout?: number): Promise<boolean> {
188188
return this.queue.drain(timeout);
189189
}
190190
}

packages/core/src/base.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
314314
/**
315315
* @inheritDoc
316316
*/
317-
public close(timeout?: number): Promise<boolean> {
317+
public async close(timeout?: number): Promise<boolean> {
318318
return this.backend.close(timeout);
319319
}
320320
}

packages/core/src/queue.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1+
/** A simple queue that holds promises. */
12
export class Queue<T> {
2-
private queue: Set<Promise<T>> = new Set();
3+
/** Internal set of queued Promises */
4+
private readonly queue: Set<Promise<T>> = new Set();
35

4-
public add(task: Promise<T>): Promise<T> {
6+
/**
7+
* Add a promise to the queue.
8+
*
9+
* @param task Can be any Promise<T>
10+
* @returns The original promise.
11+
*/
12+
public async add(task: Promise<T>): Promise<T> {
513
this.queue.add(task);
614
task.then(() => this.queue.delete(task)).catch(() => this.queue.delete(task));
715
return task;
816
}
917

18+
/**
19+
* This function returns the number of unresolved promises in the queue.
20+
*/
1021
public length(): number {
1122
return this.queue.size;
1223
}
1324

14-
public drain(timeout?: number): Promise<boolean> {
15-
return new Promise(resolve => {
25+
/**
26+
* This will drain the whole queue, returns true if queue is empty or drained.
27+
* If timeout is provided and the queue takes longer to drain, the promise still resolves but with false.
28+
*
29+
* @param timeout Number in ms to wait until it resolves with false.
30+
*/
31+
public async drain(timeout?: number): Promise<boolean> {
32+
return new Promise<boolean>(resolve => {
1633
const capturedSetTimeout = setTimeout(() => {
1734
if (timeout && timeout > 0) {
1835
resolve(false);

packages/core/test/lib/queue.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { Queue } from '../../src/queue';
22

3+
// tslint:disable:no-floating-promises
4+
35
describe('Queue', () => {
46
beforeEach(() => {
57
jest.useFakeTimers();
@@ -70,4 +72,15 @@ describe('Queue', () => {
7072
});
7173
jest.runAllTimers();
7274
});
75+
76+
test('drain() on empty queue', async () => {
77+
expect.assertions(3);
78+
const q = new Queue<void>();
79+
expect(q.length()).toBe(0);
80+
q.drain().then(result => {
81+
expect(result).toBeTruthy();
82+
expect(q.length()).toBe(0);
83+
});
84+
jest.runAllTimers();
85+
});
7386
});

packages/node/src/backend.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class NodeBackend implements Backend {
2828
private transport?: Transport;
2929

3030
/** A simple queue holding all requests. */
31-
private queue: Queue<SentryResponse> = new Queue();
31+
private readonly queue: Queue<SentryResponse> = new Queue();
3232

3333
/**
3434
* @inheritDoc
@@ -128,7 +128,7 @@ export class NodeBackend implements Backend {
128128
/**
129129
* @inheritDoc
130130
*/
131-
public close(timeout?: number): Promise<boolean> {
131+
public async close(timeout?: number): Promise<boolean> {
132132
return this.queue.drain(timeout);
133133
}
134134
}

packages/node/test/index.test.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,15 @@ describe('SentryNode', () => {
5858
captureMessage('test');
5959
captureMessage('test');
6060
captureMessage('test');
61-
client.close(50).then(result => {
62-
expect(result).toBeFalsy();
63-
done();
64-
});
61+
client
62+
.close(50)
63+
.then(result => {
64+
expect(result).toBeFalsy();
65+
done();
66+
})
67+
.catch(() => {
68+
// test
69+
});
6570
jest.runAllTimers();
6671
getCurrentHub().popScope();
6772
});
@@ -79,10 +84,15 @@ describe('SentryNode', () => {
7984
captureMessage('test');
8085
captureMessage('test');
8186
jest.runAllTimers();
82-
client.close(50).then(result => {
83-
expect(result).toBeFalsy();
84-
done();
85-
});
87+
client
88+
.close(50)
89+
.then(result => {
90+
expect(result).toBeFalsy();
91+
done();
92+
})
93+
.catch(() => {
94+
// test
95+
});
8696
jest.runAllTimers();
8797
getCurrentHub().popScope();
8898
});

0 commit comments

Comments
 (0)