|
1 | 1 | import { defaultLogger } from '@libp2p/logger' |
2 | 2 | import { expect } from 'aegir/chai' |
3 | | -import delay from 'delay' |
4 | 3 | import * as lengthPrefixed from 'it-length-prefixed' |
5 | | -import { bytes } from 'multiformats' |
6 | | -import { pEvent } from 'p-event' |
7 | 4 | import { stubInterface } from 'sinon-ts' |
8 | 5 | import { MAX_MESSAGE_SIZE, PROTOBUF_OVERHEAD } from '../src/constants.js' |
9 | 6 | import { Message } from '../src/private-to-public/pb/message.js' |
10 | 7 | import { createStream } from '../src/stream.js' |
11 | 8 | import { isFirefox } from '../src/util.ts' |
12 | | -import { RTCPeerConnection } from '../src/webrtc/index.js' |
13 | | -import { receiveFinAck, receiveRemoteCloseWrite } from './util.js' |
14 | | -import type { WebRTCStream } from '../src/stream.js' |
15 | | -import type { Stream } from '@libp2p/interface' |
16 | 9 |
|
17 | 10 | describe('Max message size', () => { |
18 | 11 | it(`sends messages smaller or equal to ${MAX_MESSAGE_SIZE} bytes in one`, async () => { |
@@ -84,180 +77,3 @@ describe('Max message size', () => { |
84 | 77 | } |
85 | 78 | }) |
86 | 79 | }) |
87 | | - |
88 | | -const TEST_MESSAGE = 'test_message' |
89 | | - |
90 | | -async function setup (): Promise<{ peerConnection: RTCPeerConnection, dataChannel: RTCDataChannel, stream: WebRTCStream }> { |
91 | | - const peerConnection = new RTCPeerConnection() |
92 | | - const dataChannel = peerConnection.createDataChannel('whatever', { negotiated: true, id: 91 }) |
93 | | - |
94 | | - await pEvent(dataChannel, 'open', { |
95 | | - rejectionEvents: [ |
96 | | - 'close', |
97 | | - 'error' |
98 | | - ] |
99 | | - }) |
100 | | - |
101 | | - const stream = createStream({ |
102 | | - channel: dataChannel, |
103 | | - direction: 'outbound', |
104 | | - closeTimeout: 1, |
105 | | - log: defaultLogger().forComponent('test') |
106 | | - }) |
107 | | - |
108 | | - return { peerConnection, dataChannel, stream } |
109 | | -} |
110 | | - |
111 | | -function generatePbByFlag (flag?: Message.Flag): Uint8Array { |
112 | | - const buf = Message.encode({ |
113 | | - flag, |
114 | | - message: bytes.fromString(TEST_MESSAGE) |
115 | | - }) |
116 | | - |
117 | | - return lengthPrefixed.encode.single(buf).subarray() |
118 | | -} |
119 | | - |
120 | | -describe.skip('Stream Stats', () => { |
121 | | - let stream: WebRTCStream |
122 | | - let peerConnection: RTCPeerConnection |
123 | | - let dataChannel: RTCDataChannel |
124 | | - |
125 | | - beforeEach(async () => { |
126 | | - ({ stream, peerConnection, dataChannel } = await setup()) |
127 | | - }) |
128 | | - |
129 | | - afterEach(() => { |
130 | | - if (peerConnection != null) { |
131 | | - peerConnection.close() |
132 | | - } |
133 | | - }) |
134 | | - |
135 | | - it('can construct', () => { |
136 | | - expect(stream.timeline.close).to.not.exist() |
137 | | - }) |
138 | | - |
139 | | - it('close marks it closed', async () => { |
140 | | - expect(stream.timeline.close).to.not.exist() |
141 | | - expect(stream.writeStatus).to.equal('writable') |
142 | | - |
143 | | - receiveFinAck(dataChannel) |
144 | | - receiveRemoteCloseWrite(dataChannel) |
145 | | - |
146 | | - await Promise.all([ |
147 | | - pEvent(stream, 'close'), |
148 | | - stream.close() |
149 | | - ]) |
150 | | - |
151 | | - expect(stream.timeline.close).to.be.a('number') |
152 | | - expect(stream.writeStatus).to.equal('closed') |
153 | | - }) |
154 | | - |
155 | | - it('closeRead marks it read-closed only', async () => { |
156 | | - expect(stream.timeline.close).to.not.exist() |
157 | | - await stream.closeRead() |
158 | | - |
159 | | - expect(stream).to.have.property('writeStatus', 'writable') |
160 | | - expect(stream).to.have.property('readStatus', 'closed') |
161 | | - }) |
162 | | - |
163 | | - it('closeWrite marks it write-closed only', async () => { |
164 | | - expect(stream.timeline.close).to.not.exist() |
165 | | - |
166 | | - receiveFinAck(dataChannel) |
167 | | - await stream.close() |
168 | | - |
169 | | - expect(stream).to.have.property('writeStatus', 'closed') |
170 | | - expect(stream).to.have.property('readStatus', 'readable') |
171 | | - }) |
172 | | - |
173 | | - it('abort = close', () => { |
174 | | - expect(stream.timeline.close).to.not.exist() |
175 | | - stream.abort(new Error('Oh no!')) |
176 | | - expect(stream.timeline.close).to.be.a('number') |
177 | | - }) |
178 | | - |
179 | | - it('reset = close', () => { |
180 | | - expect(stream.timeline.close).to.not.exist() |
181 | | - stream.onRemoteReset() // only resets the write side |
182 | | - expect(stream.timeline.close).to.be.a('number') |
183 | | - expect(stream.timeline.close).to.be.greaterThanOrEqual(stream.timeline.open) |
184 | | - }) |
185 | | -}) |
186 | | - |
187 | | -// TODO: move to transport interface compliance suite |
188 | | -describe.skip('Stream Read Stats Transition By Incoming Flag', () => { |
189 | | - let dataChannel: RTCDataChannel |
190 | | - let stream: Stream |
191 | | - let peerConnection: RTCPeerConnection |
192 | | - |
193 | | - beforeEach(async () => { |
194 | | - ({ dataChannel, stream, peerConnection } = await setup()) |
195 | | - }) |
196 | | - |
197 | | - afterEach(() => { |
198 | | - if (peerConnection != null) { |
199 | | - peerConnection.close() |
200 | | - } |
201 | | - }) |
202 | | - |
203 | | - it('no flag, no transition', () => { |
204 | | - expect(stream.timeline.close).to.not.exist() |
205 | | - const data = generatePbByFlag() |
206 | | - dataChannel.onmessage?.(new MessageEvent('message', { data })) |
207 | | - |
208 | | - expect(stream.timeline.close).to.not.exist() |
209 | | - }) |
210 | | - |
211 | | - it('open to read-close by flag:FIN', async () => { |
212 | | - const data = generatePbByFlag(Message.Flag.FIN) |
213 | | - dataChannel.dispatchEvent(new MessageEvent('message', { data })) |
214 | | - |
215 | | - await delay(100) |
216 | | - |
217 | | - expect(stream.readStatus).to.equal('closed') |
218 | | - }) |
219 | | - |
220 | | - it('read-close to close by flag:STOP_SENDING', async () => { |
221 | | - const data = generatePbByFlag(Message.Flag.STOP_SENDING) |
222 | | - dataChannel.dispatchEvent(new MessageEvent('message', { data })) |
223 | | - |
224 | | - await delay(100) |
225 | | - |
226 | | - expect(stream.remoteReadStatus).to.equal('closed') |
227 | | - }) |
228 | | -}) |
229 | | - |
230 | | -// TODO: move to transport interface compliance suite |
231 | | -describe.skip('Stream Write Stats Transition By Incoming Flag', () => { |
232 | | - let dataChannel: RTCDataChannel |
233 | | - let stream: Stream |
234 | | - let peerConnection: RTCPeerConnection |
235 | | - |
236 | | - beforeEach(async () => { |
237 | | - ({ dataChannel, stream, peerConnection } = await setup()) |
238 | | - }) |
239 | | - |
240 | | - afterEach(() => { |
241 | | - if (peerConnection != null) { |
242 | | - peerConnection.close() |
243 | | - } |
244 | | - }) |
245 | | - |
246 | | - it('open to write-close by flag:STOP_SENDING', async () => { |
247 | | - const data = generatePbByFlag(Message.Flag.STOP_SENDING) |
248 | | - dataChannel.dispatchEvent(new MessageEvent('message', { data })) |
249 | | - |
250 | | - await delay(100) |
251 | | - |
252 | | - expect(stream.remoteReadStatus).to.equal('closed') |
253 | | - }) |
254 | | - |
255 | | - it('write-close to close by flag:FIN', async () => { |
256 | | - const data = generatePbByFlag(Message.Flag.FIN) |
257 | | - dataChannel.dispatchEvent(new MessageEvent('message', { data })) |
258 | | - |
259 | | - await delay(100) |
260 | | - |
261 | | - expect(stream.remoteWriteStatus).to.equal('closed') |
262 | | - }) |
263 | | -}) |
0 commit comments