This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbase-test.js
153 lines (122 loc) · 4.02 KB
/
base-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* eslint-env mocha */
'use strict'
const chai = require('chai')
chai.use(require('chai-checkmark'))
const { expect } = chai
const pair = require('it-pair/duplex')
const pipe = require('it-pipe')
const { collect, map, consume } = require('streaming-iterables')
function close (stream) {
return pipe([], stream, consume)
}
async function closeAndWait (stream) {
await close(stream)
expect(true).to.be.true.mark()
}
/**
* A tick is considered valid if it happened between now
* and `ms` milliseconds ago
* @param {number} date Time in ticks
* @param {number} ms max milliseconds that should have expired
* @returns {boolean}
*/
function isValidTick (date, ms = 5000) {
const now = Date.now()
if (date > now - ms && date <= now) return true
return false
}
module.exports = (common) => {
describe('base', () => {
let Muxer
beforeEach(async () => {
Muxer = await common.setup()
})
it('Open a stream from the dialer', (done) => {
const p = pair()
const dialer = new Muxer()
const listener = new Muxer({
onStream: stream => {
expect(stream).to.exist.mark() // 1st check
expect(isValidTick(stream.timeline.open)).to.equal(true)
// Make sure the stream is being tracked
expect(listener.streams).to.include(stream)
close(stream)
},
onStreamEnd: stream => {
expect(stream).to.exist.mark() // 2nd check
expect(listener.streams).to.not.include(stream)
// Make sure the stream is removed from tracking
expect(isValidTick(stream.timeline.close)).to.equal(true)
}
})
pipe(p[0], dialer, p[0])
pipe(p[1], listener, p[1])
expect(3).checks(() => {
// ensure we have no streams left
expect(dialer.streams).to.have.length(0)
expect(listener.streams).to.have.length(0)
done()
})
const conn = dialer.newStream()
expect(dialer.streams).to.include(conn)
expect(isValidTick(conn.timeline.open)).to.equal(true)
closeAndWait(conn) // 3rd check
})
it('Open a stream from the listener', (done) => {
const p = pair()
const dialer = new Muxer(stream => {
expect(stream).to.exist.mark()
expect(isValidTick(stream.timeline.open)).to.equal(true)
closeAndWait(stream)
})
const listener = new Muxer()
pipe(p[0], dialer, p[0])
pipe(p[1], listener, p[1])
expect(3).check(done)
const conn = listener.newStream()
expect(listener.streams).to.include(conn)
expect(isValidTick(conn.timeline.open)).to.equal(true)
closeAndWait(conn)
})
it('Open a stream on both sides', (done) => {
const p = pair()
const dialer = new Muxer(stream => {
expect(stream).to.exist.mark()
closeAndWait(stream)
})
const listener = new Muxer(stream => {
expect(stream).to.exist.mark()
closeAndWait(stream)
})
pipe(p[0], dialer, p[0])
pipe(p[1], listener, p[1])
expect(6).check(done)
const listenerConn = listener.newStream()
const dialerConn = dialer.newStream()
closeAndWait(dialerConn)
closeAndWait(listenerConn)
})
it('Open a stream on one side, write, open a stream on the other side', (done) => {
const toString = map(c => c.slice().toString())
const p = pair()
const dialer = new Muxer()
const listener = new Muxer(stream => {
pipe(stream, toString, collect).then(chunks => {
expect(chunks).to.be.eql(['hey']).mark()
})
dialer.onStream = onDialerStream
const listenerConn = listener.newStream()
pipe(['hello'], listenerConn)
async function onDialerStream (stream) {
const chunks = await pipe(stream, toString, collect)
expect(chunks).to.be.eql(['hello']).mark()
}
})
pipe(p[0], dialer, p[0])
pipe(p[1], listener, p[1])
expect(2).check(done)
const dialerConn = dialer.newStream()
pipe(['hey'], dialerConn)
})
})
}