mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-06-26 23:41:34 +00:00
refactor: add js-libp2p-switch to the libp2p codebase (#388)
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai> Co-authored-by: Alan Shaw <alan@tableflip.io> Co-authored-by: Arnaud <arnaud.valensi@gmail.com> Co-authored-by: David Dias <daviddias.p@gmail.com> Co-authored-by: David Dias <mail@daviddias.me> Co-authored-by: Dmitriy Ryajov <dryajov@gmail.com> Co-authored-by: Francisco Baio Dias <xicombd@gmail.com> Co-authored-by: Friedel Ziegelmayer <dignifiedquire@gmail.com> Co-authored-by: Haad <haadcode@users.noreply.github.com> Co-authored-by: Hugo Dias <mail@hugodias.me> Co-authored-by: Hugo Dias <hugomrdias@gmail.com> Co-authored-by: Jacob Heun <jacobheun@gmail.com> Co-authored-by: Kevin Kwok <antimatter15@gmail.com> Co-authored-by: Kobi Gurkan <kobigurk@gmail.com> Co-authored-by: Maciej Krüger <mkg20001@gmail.com> Co-authored-by: Matteo Collina <matteo.collina@gmail.com> Co-authored-by: Michael Fakhry <fakhrimichael@live.com> Co-authored-by: Oli Evans <oli@tableflip.io> Co-authored-by: Pau Ramon Revilla <masylum@gmail.com> Co-authored-by: Pedro Teixeira <i@pgte.me> Co-authored-by: Pius Nyakoojo <piusnyakoojo@gmail.com> Co-authored-by: Richard Littauer <richard.littauer@gmail.com> Co-authored-by: Sid Harder <sideharder@gmail.com> Co-authored-by: Vasco Santos <vasco.santos@ua.pt> Co-authored-by: harrshasri <35241544+harrshasri@users.noreply.github.com> Co-authored-by: kumavis <kumavis@users.noreply.github.com> Co-authored-by: ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>
This commit is contained in:
74
test/switch/swarm-muxing+websockets.browser.js
Normal file
74
test/switch/swarm-muxing+websockets.browser.js
Normal file
@ -0,0 +1,74 @@
|
||||
/* eslint-env mocha */
|
||||
/* eslint max-nested-callbacks: ["error", 5] */
|
||||
'use strict'
|
||||
|
||||
const chai = require('chai')
|
||||
const dirtyChai = require('dirty-chai')
|
||||
const expect = chai.expect
|
||||
chai.use(dirtyChai)
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
const PeerInfo = require('peer-info')
|
||||
const WebSockets = require('libp2p-websockets')
|
||||
const mplex = require('pull-mplex')
|
||||
const spdy = require('libp2p-spdy')
|
||||
const PeerBook = require('peer-book')
|
||||
const tryEcho = require('./utils').tryEcho
|
||||
|
||||
const Switch = require('libp2p-switch')
|
||||
|
||||
describe('Switch (WebSockets)', () => {
|
||||
[
|
||||
mplex,
|
||||
spdy
|
||||
].forEach((muxer) => {
|
||||
describe(muxer.multicodec, () => {
|
||||
let sw
|
||||
let peerDst
|
||||
|
||||
before((done) => {
|
||||
PeerInfo.create((err, peerSrc) => {
|
||||
expect(err).to.not.exist()
|
||||
sw = new Switch(peerSrc, new PeerBook())
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
after(done => {
|
||||
sw.stop(done)
|
||||
})
|
||||
|
||||
it(`add muxer (${muxer.multicodec})`, () => {
|
||||
sw.connection.addStreamMuxer(muxer)
|
||||
sw.connection.reuse()
|
||||
})
|
||||
|
||||
it('add ws', () => {
|
||||
sw.transport.add('ws', new WebSockets())
|
||||
expect(Object.keys(sw.transports).length).to.equal(1)
|
||||
})
|
||||
|
||||
it('create Dst peer info', (done) => {
|
||||
PeerId.createFromJSON(require('./test-data/id-2.json'), (err, id) => {
|
||||
expect(err).to.not.exist()
|
||||
|
||||
peerDst = new PeerInfo(id)
|
||||
const ma = '/ip4/127.0.0.1/tcp/15347/ws'
|
||||
peerDst.multiaddrs.add(ma)
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('dial to warm a conn', (done) => {
|
||||
sw.dial(peerDst, done)
|
||||
})
|
||||
|
||||
it('dial on protocol, use warmed conn', (done) => {
|
||||
sw.dial(peerDst, '/echo/1.0.0', (err, conn) => {
|
||||
expect(err).to.not.exist()
|
||||
tryEcho(conn, done)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user