Skip to content

Commit 9c8ae3c

Browse files
committed
Update tests
1 parent 85cd242 commit 9c8ae3c

File tree

6 files changed

+56
-28
lines changed

6 files changed

+56
-28
lines changed

packages/polling/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,15 @@
5454
"@web/test-runner-playwright": "^0.11.0",
5555
"chai": "^4.3.4",
5656
"mocha": "^9.0.3",
57+
"mock-socket": "^9.3.1",
5758
"postcss": "^8.4.24",
5859
"rimraf": "^5.0.1",
5960
"rollup": "^3.25.1",
6061
"rollup-plugin-postcss": "^4.0.2",
6162
"rollup-plugin-sourcemaps": "^0.6.3",
6263
"terser": "^5.18.1",
6364
"tslib": "^2.5.3",
64-
"typescript": "~5.1.3",
65-
"ws": "^8.18.2"
65+
"typescript": "~5.1.3"
6666
},
6767
"publishConfig": {
6868
"access": "public"

packages/polling/src/socketstream.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { IDisposable } from '@lumino/disposable';
55

66
import { Signal, Stream } from '@lumino/signaling';
77

8-
import { Poll } from './poll';
8+
import { IPoll, Poll } from '.';
99

1010
/**
1111
* A utility class to wrap and augment a web socket. A socket stream emits web
@@ -44,7 +44,7 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
4444
*/
4545
dispose() {
4646
const { socket, subscription } = this;
47-
subscription.dispose();
47+
(subscription as Poll).dispose();
4848
if (socket) {
4949
this.socket = null;
5050
socket.onclose = () => undefined;
@@ -72,9 +72,9 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
7272
protected socket: WebSocket | null = null;
7373

7474
/**
75-
* A handle to the socket subscription to dispose when necessary.
75+
* A handle to the socket subscription.
7676
*/
77-
protected readonly subscription: IDisposable;
77+
protected readonly subscription: IPoll<unknown, unknown, 'standby'>;
7878

7979
/**
8080
* Open a web socket and subscribe to its updates.

packages/polling/tests/src/index.spec.ts

+2
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33

44
import './poll.spec';
55
import './ratelimiter.spec';
6+
import './socketstream.spec';
7+

packages/polling/tests/src/poll.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ describe('Poll', () => {
334334
const tock = (poll: Poll): void => {
335335
tocker.push(poll.state.phase);
336336
expect(ticker.join(' ')).to.equal(tocker.join(' '));
337-
poll.tick.then(tock);
337+
poll.tick.then(tock).catch(_ => undefined);
338338
};
339339
// Kick off the promise listener, but void its settlement to verify that
340340
// the poll's internal sync of the promise and the signal is correct.
341-
poll.tick.then(tock);
341+
void poll.tick.then(tock);
342342
await poll.stop();
343343
await poll.start();
344344
await poll.tick;

packages/polling/tests/src/socketstream.spec.ts

+38-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,35 @@
33

44
import { expect } from 'chai';
55

6-
import WebSocket from 'ws';
6+
import { WebSocket } from 'mock-socket';
77

8-
import { SocketStream } from '@lumino/polling';
8+
import { IPoll, SocketStream } from '@lumino/polling';
9+
10+
window.WebSocket = WebSocket;
11+
12+
/**
13+
* Return a promise that resolves in the given milliseconds with the given value.
14+
*/
15+
function sleep<T>(milliseconds: number = 0, value?: T): Promise<T | undefined> {
16+
return new Promise((resolve, reject) => {
17+
setTimeout(() => {
18+
resolve(value);
19+
}, milliseconds);
20+
});
21+
}
22+
23+
class TestSocketStream<T, U> extends SocketStream<T, U> {
24+
constructor(sender: T, connector: () => WebSocket) {
25+
super(sender, connector);
26+
this.subscription.ticked.connect((_, state) => {
27+
this.phases.push(state.phase);
28+
});
29+
}
30+
phases: IPoll.Phase<'standby'>[] = [];
31+
}
932

1033
describe('SocketStream', () => {
11-
let stream: SocketStream<unknown, unknown>;
34+
let stream: TestSocketStream<unknown, unknown>;
1235

1336
afterEach(() => {
1437
stream.dispose();
@@ -17,8 +40,19 @@ describe('SocketStream', () => {
1740
describe('#constructor()', () => {
1841
it('should create a socket stream', () => {
1942
const url = 'https://www.example.com/';
20-
stream = new SocketStream(null, () => new WebSocket(url) as any);
43+
stream = new TestSocketStream(null, () => new WebSocket(url));
2144
expect(stream).to.be.an.instanceof(SocketStream);
2245
});
2346
});
47+
48+
describe('#dispose()', () => {
49+
it('should clean up after itself upon dispose', async () => {
50+
const url = 'https://www.example.com/';
51+
stream = new TestSocketStream(null, () => new WebSocket(url));
52+
await sleep(500);
53+
stream.dispose();
54+
expect(stream.phases[0]).to.equal('started');
55+
stream.phases.slice(1).every(phase => expect(phase).to.equal('rejected'));
56+
});
57+
});
2458
});

yarn.lock

+8-16
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ __metadata:
710710
"@web/test-runner-playwright": ^0.11.0
711711
chai: ^4.3.4
712712
mocha: ^9.0.3
713+
mock-socket: ^9.3.1
713714
postcss: ^8.4.24
714715
rimraf: ^5.0.1
715716
rollup: ^3.25.1
@@ -718,7 +719,6 @@ __metadata:
718719
terser: ^5.18.1
719720
tslib: ^2.5.3
720721
typescript: ~5.1.3
721-
ws: ^8.18.2
722722
languageName: unknown
723723
linkType: soft
724724

@@ -8321,6 +8321,13 @@ __metadata:
83218321
languageName: node
83228322
linkType: hard
83238323

8324+
"mock-socket@npm:^9.3.1":
8325+
version: 9.3.1
8326+
resolution: "mock-socket@npm:9.3.1"
8327+
checksum: cb2dde4fc5dde280dd5ccb78eaaa223382ee16437f46b86558017655584ad08c22e733bde2dd5cc86927def506b6caeb0147e3167b9a62d70d5cf19d44103853
8328+
languageName: node
8329+
linkType: hard
8330+
83248331
"modify-values@npm:^1.0.1":
83258332
version: 1.0.1
83268333
resolution: "modify-values@npm:1.0.1"
@@ -12601,21 +12608,6 @@ __metadata:
1260112608
languageName: node
1260212609
linkType: hard
1260312610

12604-
"ws@npm:^8.18.2":
12605-
version: 8.18.2
12606-
resolution: "ws@npm:8.18.2"
12607-
peerDependencies:
12608-
bufferutil: ^4.0.1
12609-
utf-8-validate: ">=5.0.2"
12610-
peerDependenciesMeta:
12611-
bufferutil:
12612-
optional: true
12613-
utf-8-validate:
12614-
optional: true
12615-
checksum: e38beae19ba4d68577ec24eb34fbfab376333fedd10f99b07511a8e842e22dbc102de39adac333a18e4c58868d0703cd5f239b04b345e22402d0ed8c34ea0aa0
12616-
languageName: node
12617-
linkType: hard
12618-
1261912611
"xtend@npm:~4.0.1":
1262012612
version: 4.0.2
1262112613
resolution: "xtend@npm:4.0.2"

0 commit comments

Comments
 (0)