Skip to content
This repository was archived by the owner on Jun 26, 2023. It is now read-only.

Commit cc3130f

Browse files
dignifiedquiredaviddias
authored andcommitted
feat(pull): migration to pull streams. Upgrade tests to use mocha as
well
1 parent 2782765 commit cc3130f

15 files changed

+434
-415
lines changed

.gitignore

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
**/node_modules/
2+
**/*.log
3+
test/repo-tests*
4+
15
# Logs
26
logs
37
*.log
48

9+
coverage
10+
511
# Runtime data
612
pids
713
*.pid
@@ -19,9 +25,11 @@ coverage
1925
# node-waf configuration
2026
.lock-wscript
2127

22-
# Compiled binary addons (http://nodejs.org/api/addons.html)
23-
build/Release
28+
build
2429

2530
# Dependency directory
2631
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2732
node_modules
33+
34+
lib
35+
dist

.npmignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**/node_modules/
2+
**/*.log
3+
test/repo-tests*
4+
5+
# Logs
6+
logs
7+
*.log
8+
9+
coverage
10+
11+
# Runtime data
12+
pids
13+
*.pid
14+
*.seed
15+
16+
# Directory for instrumented libs generated by jscoverage/JSCover
17+
lib-cov
18+
19+
# Coverage directory used by tools like istanbul
20+
coverage
21+
22+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
23+
.grunt
24+
25+
# node-waf configuration
26+
.lock-wscript
27+
28+
build
29+
30+
# Dependency directory
31+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
32+
node_modules
33+
34+
test

.travis.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- "stable"
5+
6+
before_install:
7+
- npm install -g npm
8+
9+
script:
10+
- npm run lint

README.md

+21-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ interface-stream-muxer
22
=====================
33

44
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
5+
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
56
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
7+
[![Travis CI](https://travis-ci.org/ipfs/interface-stream-muxer.svg?branch=master)](https://travis-ci.org/ipfs/interface-stream-muxer)
8+
[![Dependency Status](https://david-dm.org/ipfs/interface-stream-muxer.svg?style=flat-square)](https://david-dm.org/ipfs/interface-stream-muxer) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
69

710
> A test suite and interface you can use to implement a stream muxer. "A one stop shop for all your muxing needs"
811
@@ -12,48 +15,46 @@ Publishing a test suite as a module lets multiple modules all ensure compatibili
1215

1316
The API is presented with both Node.js and Go primitives, however, there is not actual limitations for it to be extended for any other language, pushing forward the cross compatibility and interop through diferent stacks.
1417

15-
# Modules that implement the interface
18+
## Modules that implement the interface
1619

17-
- [JavaScript libp2p-spdy](https://github.com/diasdavid/js-libp2p-spdy)
18-
- [JavaScript libp2p-multiplex](https://github.com/diasdavid/js-libp2p-multiplex)
20+
- [JavaScript libp2p-spdy](https://github.com/libp2p/js-libp2p-spdy)
1921
- [Go spdy, muxado, yamux and multiplex](https://github.com/jbenet/go-stream-muxer)
2022

2123
Send a PR to add a new one if you happen to find or write one.
2224

23-
# Badge
25+
## Badge
2426

2527
Include this badge in your readme if you make a new module that uses interface-stream-muxer API.
2628

2729
![](/img/badge.png)
2830

29-
# How to use the battery tests
31+
## Usage
3032

31-
## Node.js
33+
### Node.js
3234

33-
Install interface-stream-muxer as one of the dependencies of your project and as a test file, using `tap`, `tape` or a test runner with compatible API, do:
35+
Install `interface-stream-muxer` as one of the dependencies of your project and as a test file. Then, using `mocha` (for Node.js) or a test runner with compatible API, do:
3436

35-
```
36-
var tape = require('tape')
37-
var tests = require('interface-stream-muxer/tests')
38-
var yourStreamMuxer = require('../src')
37+
```js
38+
const test = require('interface-stream-muxer')
3939

40-
var common = {
41-
setup: function (t, cb) {
42-
cb(null, yourStreamMuxer)
40+
const common = {
41+
setup (cb) {
42+
cb(null, yourMuxer)
4343
},
44-
teardown: function (t, cb) {
44+
teardown (cb) {
4545
cb()
4646
}
4747
}
4848

49-
tests(tape, common)
49+
// use all of the test suits
50+
test(common)
5051
```
5152

52-
## Go
53+
### Go
5354

54-
> WIP - being written
55+
> WIP
5556
56-
# API
57+
## API
5758

5859
A valid (read: that follows this abstraction) stream muxer, must implement the following API.
5960

@@ -86,7 +87,7 @@ In the Node.js case, if no callback is passed, stream will emit an 'ready' event
8687

8788
### Listen(wait/accept) a new incoming stream
8889

89-
- `Node.js` muxedConn.on('stream', function (stream) {})
90+
- `Node.js` muxedConn.on('stream', function (stream) {})
9091
- `Go` stream := muxedConn.Accept()
9192

9293
Each time a dialing peer initiates the new stream handshake, a new stream is created on the listening side.

package.json

+22-10
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
"name": "interface-stream-muxer",
33
"version": "0.3.1",
44
"description": "A test suite and interface you can use to implement a stream muxer.",
5-
"main": "tests/index.js",
6-
"directories": {
7-
"test": "tests"
8-
},
5+
"main": "src/index.js",
6+
"jsnext:main": "src/index.js",
97
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
8+
"test": "exit(0)",
9+
"build": "aegir-build --env node",
10+
"lint": "aegir-lint",
11+
"release": "aegir-release --env node",
12+
"release-minor": "aegir-release --env node --type minor",
13+
"release-major": "aegir-release --env node --type major",
14+
"coverage": "exit(0)",
15+
"coverage-publish": "exit(0)"
1116
},
1217
"repository": {
1318
"type": "git",
14-
"url": "https://github.com/diasdavid/interface-stream-muxer.git"
19+
"url": "https://github.com/libp2p/interface-stream-muxer.git"
1520
},
1621
"keywords": [
1722
"Streams",
@@ -22,11 +27,18 @@
2227
"author": "David Dias <[email protected]>",
2328
"license": "MIT",
2429
"bugs": {
25-
"url": "https://github.com/diasdavid/interface-stream-muxer/issues"
30+
"url": "https://github.com/libp2p/interface-stream-muxer/issues"
2631
},
27-
"homepage": "https://github.com/diasdavid/interface-stream-muxer",
32+
"homepage": "https://github.com/libp2p/interface-stream-muxer",
2833
"dependencies": {
29-
"stream-pair": "^1.0.3",
30-
"timed-tape": "^0.1.1"
34+
"async": "^2.0.1",
35+
"chai": "^3.5.0",
36+
"chai-checkmark": "^1.0.1",
37+
"pull-generate": "^2.2.0",
38+
"pull-pair": "^1.1.0",
39+
"pull-stream": "^3.4.3"
40+
},
41+
"devDependencies": {
42+
"aegir": "^6.0.1"
3143
}
3244
}

src/base-test.js

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const chai = require('chai')
5+
chai.use(require('chai-checkmark'))
6+
const expect = chai.expect
7+
const pair = require('pull-pair/duplex')
8+
const pull = require('pull-stream')
9+
10+
function closeAndWait (stream) {
11+
pull(
12+
pull.empty(),
13+
stream,
14+
pull.onEnd((err) => {
15+
expect(err).to.not.exist.mark()
16+
})
17+
)
18+
}
19+
20+
module.exports = (common) => {
21+
describe('base', () => {
22+
let muxer
23+
24+
beforeEach((done) => {
25+
common.setup((err, _muxer) => {
26+
if (err) return done(err)
27+
muxer = _muxer
28+
done()
29+
})
30+
})
31+
32+
it('Open a stream from the dialer', (done) => {
33+
const p = pair()
34+
const dialer = muxer.dial(p[0])
35+
const listener = muxer.listen(p[1])
36+
37+
expect(4).checks(done)
38+
39+
listener.on('stream', (stream) => {
40+
expect(stream).to.exist.mark()
41+
closeAndWait(stream)
42+
})
43+
44+
const conn = dialer.newStream((err) => {
45+
expect(err).to.not.exist.mark()
46+
})
47+
48+
closeAndWait(conn)
49+
})
50+
51+
it('Open a stream from the listener', (done) => {
52+
const p = pair()
53+
const dialer = muxer.dial(p[0])
54+
const listener = muxer.listen(p[1])
55+
56+
expect(4).check(done)
57+
58+
dialer.on('stream', (stream) => {
59+
expect(stream).to.exist.mark()
60+
closeAndWait(stream)
61+
})
62+
63+
const conn = listener.newStream((err) => {
64+
expect(err).to.not.exist.mark()
65+
})
66+
67+
closeAndWait(conn)
68+
})
69+
70+
it('Open a stream on both sides', (done) => {
71+
const p = pair()
72+
const dialer = muxer.dial(p[0])
73+
const listener = muxer.listen(p[1])
74+
75+
expect(8).check(done)
76+
77+
dialer.on('stream', (stream) => {
78+
expect(stream).to.exist.mark()
79+
closeAndWait(stream)
80+
})
81+
82+
const listenerConn = listener.newStream((err) => {
83+
expect(err).to.not.exist.mark()
84+
})
85+
86+
listener.on('stream', (stream) => {
87+
expect(stream).to.exist.mark()
88+
closeAndWait(stream)
89+
})
90+
91+
const dialerConn = dialer.newStream((err) => {
92+
expect(err).to.not.exist.mark()
93+
})
94+
95+
closeAndWait(dialerConn)
96+
closeAndWait(listenerConn)
97+
})
98+
99+
it('Open a stream on one side, write, open a stream in the other side', (done) => {
100+
const p = pair()
101+
const dialer = muxer.dial(p[0])
102+
const listener = muxer.listen(p[1])
103+
104+
expect(6).check(done)
105+
106+
const dialerConn = dialer.newStream((err) => {
107+
expect(err).to.not.exist.mark()
108+
})
109+
110+
pull(
111+
pull.values(['hey']),
112+
dialerConn
113+
)
114+
115+
listener.on('stream', (stream) => {
116+
pull(
117+
stream,
118+
pull.collect((err, chunks) => {
119+
expect(err).to.not.exist.mark()
120+
expect(chunks).to.be.eql([Buffer('hey')]).mark()
121+
})
122+
)
123+
124+
const listenerConn = listener.newStream((err) => {
125+
expect(err).to.not.exist.mark()
126+
})
127+
128+
pull(
129+
pull.values(['hello']),
130+
listenerConn
131+
)
132+
133+
dialer.on('stream', onDialerStream)
134+
function onDialerStream (stream) {
135+
pull(
136+
stream,
137+
pull.collect((err, chunks) => {
138+
expect(err).to.not.exist.mark()
139+
expect(chunks).to.be.eql([Buffer('hello')]).mark()
140+
})
141+
)
142+
}
143+
})
144+
})
145+
})
146+
}

src/index.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const baseTest = require('./base-test')
5+
const stressTest = require('./stress-test')
6+
const megaStressTest = require('./mega-stress-test')
7+
8+
module.exports = (common) => {
9+
describe('interface-stream-muxer', () => {
10+
baseTest(common)
11+
stressTest(common)
12+
megaStressTest(common)
13+
})
14+
}

src/mega-stress-test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const spawn = require('./spawner')
5+
6+
module.exports = (common) => {
7+
describe.skip('mega stress test', function () {
8+
this.timeout(100 * 200 * 1000)
9+
let muxer
10+
11+
beforeEach((done) => {
12+
common.setup((err, _muxer) => {
13+
if (err) return done(err)
14+
muxer = _muxer
15+
done()
16+
})
17+
})
18+
19+
it('10000 messages of 10000 streams', (done) => {
20+
spawn(muxer, 10000, 10000, done, 5000)
21+
})
22+
})
23+
}

0 commit comments

Comments
 (0)