Skip to content

Commit

Permalink
wip: tests cleaup
Browse files Browse the repository at this point in the history
  • Loading branch information
dryajov committed Aug 17, 2017
1 parent 19ff1f4 commit f0d38e7
Show file tree
Hide file tree
Showing 7 changed files with 314 additions and 379 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Node extends EventEmitter {
this.swarm.connection.reuse()

// If muxer exists, we can use Relay for listening/dialing
this.swarm.connection.enableRelayDialing(_options.relay)
this.swarm.connection.enableCircuitRelay(_options.relay)

// Received incommind dial and muxer upgrade happened,
// reuse this muxed connection
Expand Down
139 changes: 69 additions & 70 deletions test/nodejs-bundle/circuit/dial-over-any-relay.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,87 +13,86 @@ chai.use(require('dirty-chai'))

const expect = chai.expect

describe('test connecting over any relay', function () {
this.timeout(500000)
describe(`dial over any relay`, function () {
describe('test connecting over any relay', function () {
this.timeout(500000)

let portBase = 9010 // TODO: randomize or mock sockets
let testNodes
let portBase = 9010 // TODO: randomize or mock sockets
let testNodes

function setUpNodes (muxer, done) {
utils.createNodes(
{
relayNode: {
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true
function setUpNodes (muxer, done) {
utils.createNodes(
{
relayNode: {
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true
}
}
}
},
nodeA: {
transports: [new TCP()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`
]
},
nodeB: {
transports: [new WS()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
]
}
},
nodeA: {
transports: [new TCP()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`
]
},
nodeB: {
transports: [new WS()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
]
}
},
(err, nodes) => {
if (err) {
return done(err)
}
(err, nodes) => {
if (err) {
return done(err)
}

testNodes = nodes
done()
})
}
testNodes = nodes
done()
})
}

beforeEach(function (done) {
setUpNodes(multiplex, () => {
let nodeA = testNodes['nodeA']
let nodeB = testNodes['nodeB']
let relayNode = testNodes['relayNode']
beforeEach(function (done) {
setUpNodes(multiplex, () => {
let nodeA = testNodes['nodeA']
let nodeB = testNodes['nodeB']
let relayNode = testNodes['relayNode']

waterfall([
(cb) => nodeA.dial(relayNode.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode.peerInfo, cb)
], () => setTimeout(done, 1000)) // WS needs some time to initialize
waterfall([
(cb) => nodeA.dial(relayNode.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode.peerInfo, cb)
], () => setTimeout(done, 1000)) // WS needs some time to initialize
})
})
})

afterEach(function circuitTests (done) {
utils.stopNodes(testNodes, () => done())
})
afterEach(function circuitTests (done) {
utils.stopNodes(testNodes, () => done())
})

it('dial to a node over a relay and write values', function (done) {
utils.dialAndReverse(
testNodes.nodeB,
testNodes.nodeA,
['hello', 'hello1', 'hello2', 'hello3'],
(err, result) => {
expect(err).to.be.null()
expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
expect(result[1]).to.equal('hello1'.split('').reverse('').join(''))
expect(result[2]).to.equal('hello2'.split('').reverse('').join(''))
expect(result[3]).to.equal('hello3'.split('').reverse('').join(''))
done()
})
it('dial to a node over a relay and write values', function (done) {
utils.dialAndReverse(
testNodes.nodeB,
testNodes.nodeA,
['hello'],
(err, result) => {
expect(err).to.be.null()
expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
done()
})
})
})
})
214 changes: 108 additions & 106 deletions test/nodejs-bundle/circuit/dial-over-specific-relay-transport.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,124 +18,126 @@ const expect = chai.expect
const nodeKeys = require('./fixtures/nodes')
const Circuit = require('libp2p-circuit')

describe(`listen on an explicit chained relay addr`, function () {
this.timeout(500000)

let portBase = 9030 // TODO: randomize or mock sockets
let testNodes

let relaySpy1
let relaySpy2

function setUpNodes (muxer, done) {
utils.createNodes(
{
relayNode1: {
id: nodeKeys.node1,
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true,
active: true
describe(`dial over specific relay and transport`, function () {
describe(`listen on an explicit chained relay addr`, function () {
this.timeout(500000)

let portBase = 9030 // TODO: randomize or mock sockets
let testNodes

let relaySpy1
let relaySpy2

function setUpNodes (muxer, done) {
utils.createNodes(
{
relayNode1: {
id: nodeKeys.node1,
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true,
active: true
}
}
}
}
},
relayNode2: {
id: nodeKeys.node2,
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true,
active: true
},
relayNode2: {
id: nodeKeys.node2,
transports: [new TCP(), new WS()],
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`,
`/ip4/0.0.0.0/tcp/${portBase++}/ws`
],
isCrypto: true,
config: {
relay: {
circuit: {
enabled: true,
active: true
}
}
}
},
nodeA: {
id: nodeKeys.node3,
transports: [new TCP()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`
]
},
nodeB: {
id: nodeKeys.node4,
transports: [new WS()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`
]
}
},
nodeA: {
id: nodeKeys.node3,
transports: [new TCP()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/${portBase++}`
]
},
nodeB: {
id: nodeKeys.node4,
transports: [new WS()],
isCrypto: true,
muxer: muxer,
addrs: [
`/ip4/0.0.0.0/tcp/9031/ws/ipfs/${nodeKeys.node1.id}/p2p-circuit`
]
}
},
(err, nodes) => {
if (err) {
return done(err)
}

testNodes = nodes
relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle')
relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle')

done()
(err, nodes) => {
if (err) {
return done(err)
}

testNodes = nodes
relaySpy1 = sinon.spy(testNodes['relayNode1'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle')
relaySpy2 = sinon.spy(testNodes['relayNode2'].swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle')

done()
})
}

beforeEach(function (done) {
setUpNodes(multiplex, () => {
let nodeA = testNodes['nodeA']
let nodeB = testNodes['nodeB']
let relayNode1 = testNodes['relayNode1']
let relayNode2 = testNodes['relayNode2']

waterfall([
(cb) => nodeA.dial(relayNode1.peerInfo, cb),
(conn, cb) => nodeA.dial(relayNode2.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode1.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode2.peerInfo, cb),
(conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb),
(conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb)
], () => setTimeout(done, 1000)) // WS needs some time to initialize
})
}

beforeEach(function (done) {
setUpNodes(multiplex, () => {
let nodeA = testNodes['nodeA']
let nodeB = testNodes['nodeB']
let relayNode1 = testNodes['relayNode1']
let relayNode2 = testNodes['relayNode2']

waterfall([
(cb) => nodeA.dial(relayNode1.peerInfo, cb),
(conn, cb) => nodeA.dial(relayNode2.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode1.peerInfo, cb),
(conn, cb) => nodeB.dial(relayNode2.peerInfo, cb),
(conn, cb) => relayNode1.dial(relayNode2.peerInfo, cb),
(conn, cb) => relayNode2.dial(relayNode1.peerInfo, cb)
], () => setTimeout(done, 1000)) // WS needs some time to initialize
})
})

afterEach(function circuitTests (done) {
relaySpy1.reset()
relaySpy2.reset()
utils.stopNodes(testNodes, done)
})
afterEach(function circuitTests (done) {
relaySpy1.reset()
relaySpy2.reset()
utils.stopNodes(testNodes, done)
})

it('dial over the correct chained relay addr', function (done) {
utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => {
expect(err).to.be.null()
expect(relaySpy1.called).to.be.ok()
expect(relaySpy2.called).to.be.ok()
it('dial over the correct chained relay addr', function (done) {
utils.dialAndReverse(testNodes['nodeA'], testNodes['nodeB'], ['hello'], (err, result) => {
expect(err).to.be.null()
expect(relaySpy1.called).to.be.ok()
expect(relaySpy2.called).to.be.ok()

expect(relaySpy1.args.some((a) => {
return a[0] &&
a[0].dstPeer &&
multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`
})).to.be.ok()
expect(relaySpy1.args.some((a) => {
return a[0] &&
a[0].dstPeer &&
multiaddr(a[0].dstPeer.addrs[0]).toString() === `/ipfs/${testNodes['nodeB'].peerInfo.id.toB58String()}`
})).to.be.ok()

expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
done(err)
expect(result[0]).to.equal('hello'.split('').reverse('').join(''))
done(err)
})
})
})
})
Loading

0 comments on commit f0d38e7

Please sign in to comment.