Skip to content

Commit

Permalink
Merge pull request libp2p#31 from diasdavid/clean/browser-tests
Browse files Browse the repository at this point in the history
WIP: clean browser tests, add badgers
  • Loading branch information
daviddias committed Mar 22, 2016
2 parents 72f205e + 8c6ef52 commit a2084f0
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 205 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ libp2p-swarm JavaScript implementation
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
[![Build Status](https://img.shields.io/travis/diasdavid/js-libp2p-swarm/master.svg?style=flat-square)](https://travis-ci.org/diasdavid/js-libp2p-swarm)
![](https://img.shields.io/badge/coverage-%3F-yellow.svg?style=flat-square)
[![Dependency Status](https://david-dm.org/ipfs/js-libp2p-swarm.svg?style=flat-square)](https://david-dm.org/ipfs/js-libp2p-swarm)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)

> libp2p swarm implementation in JavaScript
Expand Down
10 changes: 6 additions & 4 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const path = require('path')

module.exports = function (config) {
var path = require('path')
var nodeForgePath = path.resolve(__dirname, 'node_modules/peer-id/deps/forge.bundle.js')
const nodeForgePath = path.resolve(__dirname, 'node_modules/peer-id/deps/forge.bundle.js')

config.set({
basePath: '',
frameworks: ['mocha'],

files: [
nodeForgePath,
'tests/browser.js'
'tests/browser-nodejs/browser.js'
],

preprocessors: {
'tests/*': ['webpack']
'tests/*': ['webpack'],
'tests/browser-nodejs/*': ['webpack']
},

webpack: {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "libp2p-swarm",
"version": "0.9.0",
"description": "libp2p swarm implementation in Node.js",
"description": "libp2p swarm implementation in JavaScript",
"main": "src/index.js",
"scripts": {
"test:node": "mocha tests/*-test.js",
"test:browser": "node tests/karma.js",
"test:browser": "node tests/browser-nodejs/test.js",
"test": "npm run test:node && npm run test:browser",
"coverage": "istanbul cover --print both -- _mocha tests/*-test.js",
"lint": "standard"
Expand Down
123 changes: 123 additions & 0 deletions tests/browser-nodejs/browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* eslint-env mocha */

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

describe('basics', () => {
it('throws on missing peerInfo', (done) => {
expect(Swarm).to.throw(Error)
done()
})
})

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

var swarm

before((done) => {
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)

done()
})

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/websockets')

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((done) => {
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)

done()
})

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

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

it('create Dst peer info', (done) => {
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/websockets')
peerDst.multiaddr.add(ma)
done()
})

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)
})
})
})
65 changes: 65 additions & 0 deletions tests/browser-nodejs/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const Server = require('karma').Server
const path = require('path')

const Peer = require('peer-info')
const Id = require('peer-id')
const WebSockets = require('libp2p-websockets')
const Swarm = require('./../../src')
const multiaddr = require('multiaddr')

var swarmA
var swarmB

function createListeners (done) {
function createListenerA (cb) {
const b58IdA = 'QmWg2L4Fucx1x4KXJTfKHGixBJvveubzcd7DdhB2Mqwfh1'
const peerA = new Peer(Id.createFromB58String(b58IdA))
const maA = multiaddr('/ip4/127.0.0.1/tcp/9100/websockets')

peerA.multiaddr.add(maA)
swarmA = new Swarm(peerA)
swarmA.transport.add('ws', new WebSockets())
swarmA.transport.listen('ws', {}, echo, cb)
}

function createListenerB (cb) {
const b58IdB = 'QmRy1iU6BHmG5Hd8rnPhPL98cy1W1przUSTAMcGDq9yAAV'
const maB = multiaddr('/ip4/127.0.0.1/tcp/9200/websockets')
const peerB = new Peer(Id.createFromB58String(b58IdB))
peerB.multiaddr.add(maB)
swarmB = new Swarm(peerB)

swarmB.transport.add('ws', new WebSockets())
swarmB.transport.listen('ws', {}, null, cb)

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

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

createListenerA(ready)
createListenerB(ready)

function echo (conn) {
conn.pipe(conn)
}
}

function stop (done) {
var count = 0
const ready = () => ++count === 2 ? done() : null

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

function run (done) {
const karma = new Server({
configFile: path.join(__dirname, '../../karma.conf.js')
}, done)

karma.start()
}

createListeners(() => run(() => stop(() => null)))
133 changes: 0 additions & 133 deletions tests/browser.js

This file was deleted.

Loading

0 comments on commit a2084f0

Please sign in to comment.