forked from libp2p/js-libp2p
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request libp2p#31 from diasdavid/clean/browser-tests
WIP: clean browser tests, add badgers
- Loading branch information
Showing
7 changed files
with
199 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.