Skip to content
This repository has been archived by the owner on Aug 23, 2019. It is now read-only.

Commit

Permalink
restructure and add spdy to browser tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 22, 2016
1 parent 09acdab commit 3cd5cbb
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 108 deletions.
16 changes: 13 additions & 3 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const Id = require('peer-id')
const WebSockets = require('libp2p-websockets')

const Swarm = require('./src')
// const spdy = require('libp2p-spdy')
const multiaddr = require('multiaddr')

const sigServer = require('libp2p-webrtc-star/src/signalling-server')

let swarmA
let swarmB
let sigS

gulp.task('test:browser:before', (done) => {
function createListenerA (cb) {
Expand All @@ -31,16 +35,21 @@ gulp.task('test:browser:before', (done) => {
swarmB = new Swarm(peerB)

swarmB.transport.add('ws', new WebSockets())
swarmB.transport.listen('ws', {}, null, cb)
swarmB.transport.listen('ws', {}, null, () => {
// swarmB.connection.addStreamMuxer(spdy)
// swarmB.connection.reuse()
cb()
})

swarmB.handle('/echo/1.0.0', echo)
}

let count = 0
const ready = () => ++count === 2 ? done() : null
const ready = () => ++count === 3 ? done() : null

createListenerA(ready)
createListenerB(ready)
sigS = sigServer.start(15555, ready)

function echo (conn) {
conn.pipe(conn)
Expand All @@ -49,10 +58,11 @@ gulp.task('test:browser:before', (done) => {

gulp.task('test:browser:after', (done) => {
let count = 0
const ready = () => ++count === 2 ? done() : null
const ready = () => ++count === 3 ? done() : null

swarmA.transport.close('ws', ready)
swarmB.transport.close('ws', ready)
sigS.stop(ready)
})

require('aegir/gulp')(gulp)
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,20 @@
"node": "^4.3.0"
},
"devDependencies": {
"aegir": "^3.0.4",
"bl": "^1.1.2",
"buffer-loader": "0.0.1",
"chai": "^3.5.0",
"aegir": "^3.0.4",
"gulp": "^3.9.1",
"istanbul": "^0.4.3",
"libp2p-multiplex": "^0.2.1",
"libp2p-spdy": "^0.5.0",
"libp2p-spdy": "^0.6.0",
"libp2p-tcp": "^0.5.1",
"libp2p-webrtc-star": "^0.1.0",
"libp2p-websockets": "^0.5.0",
"pre-commit": "^1.1.2",
"stream-pair": "^1.0.3"
"stream-pair": "^1.0.3",
"webrtcsupport": "^2.2.0"
},
"dependencies": {
"babel-runtime": "^6.6.1",
Expand Down Expand Up @@ -79,4 +81,4 @@
"Richard Littauer <[email protected]>",
"dignifiedquire <[email protected]>"
]
}
}
48 changes: 48 additions & 0 deletions test/browser-00-transport-websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
const bl = require('bl')

const Swarm = require('../src')

describe('transport - websockets', function () {
this.timeout(10000)

var swarm

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

it('add', (done) => {
swarm.transport.add('ws', new WebSockets(), () => {
expect(Object.keys(swarm.transports).length).to.equal(1)
done()
})
})

it('dial', (done) => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9100/ws')

const conn = swarm.transport.dial('ws', ma, (err, conn) => {
expect(err).to.not.exist
})

conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()
})
})
49 changes: 49 additions & 0 deletions test/browser-01-transport-webrtc-star.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* eslint-env mocha */
'use strict'

/*
const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
const bl = require('bl')
const Swarm = require('../src')
describe('transport - webrtc-star', function () {
this.timeout(10000)
var swarm
before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})
it('add', (done) => {
swarm.transport.add('ws', new WebSockets(), () => {
expect(Object.keys(swarm.transports).length).to.equal(1)
done()
})
})
it('dial', (done) => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9100/ws')
const conn = swarm.transport.dial('ws', ma, (err, conn) => {
expect(err).to.not.exist
})
conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()
})
})
*/
73 changes: 73 additions & 0 deletions test/browser-02-swarm-with-muxing-plus-websockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* eslint-env mocha */
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
// const spdy = require('libp2p-spdy')

const Swarm = require('../src')

describe('high level API (swarm with spdy + websockets)', function () {
this.timeout(10000)

var swarm
var peerDst

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

it('add spdy', () => {
// swarm.connection.addStreamMuxer(spdy)
// swarm.connection.reuse()
})

it('add ws', () => {
swarm.transport.add('ws', new WebSockets())
expect(Object.keys(swarm.transports).length).to.equal(1)
})

it('create Dst peer info', () => {
const b58IdDst = 'QmRy1iU6BHmG5Hd8rnPhPL98cy1W1przUSTAMcGDq9yAAV'
// use a pre generated Id to save time
const idDst = Id.createFromB58String(b58IdDst)
peerDst = new Peer(idDst)

const ma = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
peerDst.multiaddr.add(ma)
})

it('dial to warm a conn', (done) => {
swarm.dial(peerDst, (err) => {
expect(err).to.not.exist
done()
})
})

it('dial on protocol, use warmed conn', (done) => {
swarm.dial(peerDst, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist
conn.end()
conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})

it('close', (done) => {
// cause CI is slow
setTimeout(() => {
swarm.close(done)
}, 1000)
})

// TODO - test that the listener (node.js peer) can dial back
// do that by dialing on a protocol to activate that behaviour
// like libp2p-spdy tests
})
Empty file.
Empty file.
111 changes: 10 additions & 101 deletions test/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@
'use strict'

const expect = require('chai').expect
const multiaddr = require('multiaddr')
const Id = require('peer-id')
const Peer = require('peer-info')
const WebSockets = require('libp2p-websockets')
const bl = require('bl')
const w = require('webrtcsupport')

const Swarm = require('../src')

Expand All @@ -17,102 +13,15 @@ describe('basics', () => {
})
})

describe('transport - websockets', function () {
this.timeout(10000)
require('./browser-00-transport-websockets.js')

var swarm
if (w.support) {
require('./browser-01-transport-webrtc-star.js')
}

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

it('add', (done) => {
swarm.transport.add('ws', new WebSockets(), () => {
expect(Object.keys(swarm.transports).length).to.equal(1)
done()
})
})

it('dial', (done) => {
const ma = multiaddr('/ip4/127.0.0.1/tcp/9100/ws')
require('./browser-02-swarm-with-muxing-plus-websockets.js')

const conn = swarm.transport.dial('ws', ma, (err, conn) => {
expect(err).to.not.exist
})

conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()
})
})

describe('high level API - 1st without stream multiplexing (on websockets)', function () {
this.timeout(10000)

var swarm
var peerDst

before(() => {
const b58IdSrc = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idSrc = Id.createFromB58String(b58IdSrc)
const peerSrc = new Peer(idSrc)
swarm = new Swarm(peerSrc)
})

after((done) => {
swarm.close(done)
})

it('add ws', () => {
swarm.transport.add('ws', new WebSockets())
expect(Object.keys(swarm.transports).length).to.equal(1)
})

it('create Dst peer info', () => {
const b58IdDst = 'QmYzgdesgjdvD3okTPGZT9NPmh1BuH5FfTVNKjsvaAprhb'
// use a pre generated Id to save time
const idDst = Id.createFromB58String(b58IdDst)
peerDst = new Peer(idDst)

const ma = multiaddr('/ip4/127.0.0.1/tcp/9200/ws')
peerDst.multiaddr.add(ma)
})

it('dial on protocol', (done) => {
swarm.dial(peerDst, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist
conn.pipe(bl((err, data) => {
expect(err).to.not.exist
expect(data.toString()).to.equal('hey')
done()
}))
conn.write('hey')
conn.end()
})
})

it('dial to warm a conn', (done) => {
swarm.dial(peerDst, (err) => {
expect(err).to.not.exist
done()
})
})

it('dial on protocol, reuse warmed conn', (done) => {
swarm.dial(peerDst, '/echo/1.0.0', (err, conn) => {
expect(err).to.not.exist
conn.end()
conn.on('data', () => {}) // let it flow.. let it flooooow
conn.on('end', done)
})
})
})
if (w.support) {
require('./browser-03-swarm-with-muxing-plus-webrtc-star.js')
require('./browser-04-swarm-with-muxing-plus-websockets-and-webrtc-star.js')
}

0 comments on commit 3cd5cbb

Please sign in to comment.