|
1 | 1 | import { mock } from "jest-mock-extended";
|
2 |
| -import { |
3 |
| - Demultiplexer, |
4 |
| - Deserializer, |
5 |
| - Flags, |
6 |
| - Frame, |
7 |
| - FrameHandler, |
8 |
| - FrameTypes, |
9 |
| - Multiplexer, |
10 |
| - serializeFrame, |
11 |
| - SetupFrame, |
12 |
| -} from "rsocket-core"; |
| 2 | +import { Demultiplexer, Frame, FrameHandler, Multiplexer } from "rsocket-core"; |
13 | 3 | import { WebsocketDuplexConnection } from "../WebsocketDuplexConnection";
|
14 |
| -// import { MockSocket } from "../__mocks__/ws"; |
15 | 4 | import { Duplex } from "stream";
|
16 | 5 |
|
17 |
| -// const deserializer = mock<Deserializer>(); |
| 6 | +function makeDuplexStub() { |
| 7 | + const listeners = { |
| 8 | + close: [], |
| 9 | + }; |
| 10 | + return mock<Duplex>({ |
| 11 | + on(event, cb) { |
| 12 | + listeners[event]?.push(cb); |
| 13 | + return this; |
| 14 | + }, |
| 15 | + destroy(error?: Error) { |
| 16 | + listeners.close.forEach((cb) => cb(error)); |
| 17 | + return this; |
| 18 | + }, |
| 19 | + }); |
| 20 | +} |
18 | 21 |
|
19 | 22 | describe("WebsocketDuplexConnection", function () {
|
20 | 23 | describe("when closed", () => {
|
@@ -125,69 +128,55 @@ describe("WebsocketDuplexConnection", function () {
|
125 | 128 | expect(onCloseCallback).toBeCalledTimes(1);
|
126 | 129 | expect(onCloseCallback).toBeCalledWith(error);
|
127 | 130 | });
|
128 |
| - // |
129 |
| - // it("subsequent calls to close result in only a single invocation of onClose", () => { |
130 |
| - // const socketStub = mock<WebSocket>(); |
131 |
| - // const multiplexerDemultiplexer = mock< |
132 |
| - // Multiplexer & Demultiplexer & FrameHandler |
133 |
| - // >(); |
134 |
| - // const connection = new WebsocketDuplexConnection( |
135 |
| - // socketStub, |
136 |
| - // deserializer, |
137 |
| - // () => multiplexerDemultiplexer |
138 |
| - // ); |
139 |
| - // const onCloseCallback = jest.fn(); |
140 |
| - // const error = new Error(); |
141 |
| - // connection.onClose(onCloseCallback); |
142 |
| - // connection.close(error); |
143 |
| - // connection.close(error); |
144 |
| - // |
145 |
| - // expect(onCloseCallback).toBeCalledTimes(1); |
146 |
| - // expect(onCloseCallback).toBeCalledWith(error); |
147 |
| - // }); |
148 |
| - // |
149 |
| - // it("the onClose callback is called with an error when the socket is closed unexpectedly", () => { |
150 |
| - // const socket = new MockSocket() as unknown as WebSocket; |
151 |
| - // const multiplexerDemultiplexer = mock< |
152 |
| - // Multiplexer & Demultiplexer & FrameHandler |
153 |
| - // >(); |
154 |
| - // const connection = new WebsocketDuplexConnection( |
155 |
| - // socket, |
156 |
| - // deserializer, |
157 |
| - // () => multiplexerDemultiplexer |
158 |
| - // ); |
159 |
| - // const onCloseCallback = jest.fn(); |
160 |
| - // |
161 |
| - // connection.onClose(onCloseCallback); |
162 |
| - // (socket as unknown as MockSocket).mock.close({}); |
163 |
| - // |
164 |
| - // expect(onCloseCallback).toBeCalledTimes(1); |
165 |
| - // expect(onCloseCallback).toHaveBeenCalledWith( |
166 |
| - // new Error("WebsocketDuplexConnection: Socket closed unexpectedly.") |
167 |
| - // ); |
168 |
| - // }); |
169 |
| - // |
170 |
| - // it("the onClose callback is called with an error when the socket is closed with an error", () => { |
171 |
| - // const socket = new MockSocket() as unknown as WebSocket; |
172 |
| - // const multiplexerDemultiplexer = mock< |
173 |
| - // Multiplexer & Demultiplexer & FrameHandler |
174 |
| - // >(); |
175 |
| - // const connection = new WebsocketDuplexConnection( |
176 |
| - // socket, |
177 |
| - // deserializer, |
178 |
| - // () => multiplexerDemultiplexer |
179 |
| - // ); |
180 |
| - // const onCloseCallback = jest.fn(); |
181 |
| - // const expectedError = new Error( |
182 |
| - // "WebsocketDuplexConnection: Test error 1" |
183 |
| - // ); |
184 |
| - // |
185 |
| - // connection.onClose(onCloseCallback); |
186 |
| - // (socket as unknown as MockSocket).mock.error({ error: expectedError }); |
187 |
| - // |
188 |
| - // expect(onCloseCallback).toBeCalledTimes(1); |
189 |
| - // expect(onCloseCallback).toHaveBeenCalledWith(expectedError); |
190 |
| - // }); |
| 131 | + |
| 132 | + it("subsequent calls to close result in only a single invocation of onClose", () => { |
| 133 | + // arrange |
| 134 | + const socketStub = mock<Duplex>(); |
| 135 | + const multiplexerDemultiplexer = mock< |
| 136 | + Multiplexer & Demultiplexer & FrameHandler |
| 137 | + >(); |
| 138 | + const frame = mock<Frame>(); |
| 139 | + const connection = new WebsocketDuplexConnection( |
| 140 | + socketStub, |
| 141 | + frame, |
| 142 | + () => multiplexerDemultiplexer |
| 143 | + ); |
| 144 | + const onCloseCallback = jest.fn(); |
| 145 | + const error = new Error(); |
| 146 | + connection.onClose(onCloseCallback); |
| 147 | + |
| 148 | + // act |
| 149 | + connection.close(error); |
| 150 | + connection.close(error); |
| 151 | + |
| 152 | + // assert |
| 153 | + expect(onCloseCallback).toBeCalledTimes(1); |
| 154 | + expect(onCloseCallback).toBeCalledWith(error); |
| 155 | + }); |
| 156 | + |
| 157 | + it("the onClose callback is called with an error when the socket is destroyed unexpectedly", () => { |
| 158 | + // arrange |
| 159 | + |
| 160 | + const socketStub = makeDuplexStub(); |
| 161 | + const multiplexerDemultiplexer = mock< |
| 162 | + Multiplexer & Demultiplexer & FrameHandler |
| 163 | + >(); |
| 164 | + const frame = mock<Frame>(); |
| 165 | + const connection = new WebsocketDuplexConnection( |
| 166 | + socketStub, |
| 167 | + frame, |
| 168 | + () => multiplexerDemultiplexer |
| 169 | + ); |
| 170 | + const onCloseCallback = jest.fn(); |
| 171 | + |
| 172 | + connection.onClose(onCloseCallback); |
| 173 | + (socketStub as unknown as Duplex).destroy(new Error("simulated error")); |
| 174 | + |
| 175 | + expect(onCloseCallback).toBeCalledTimes(1); |
| 176 | + expect(onCloseCallback).toHaveBeenCalledWith( |
| 177 | + new Error("WebsocketDuplexConnection: Socket closed unexpectedly.") |
| 178 | + ); |
| 179 | + }); |
191 | 180 | });
|
192 | 181 |
|
193 | 182 | // describe("send()", () => {
|
|
0 commit comments