Skip to content

Commit

Permalink
test: use chai as promised
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Nov 29, 2019
1 parent 870d80d commit 6fd7d0e
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 131 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"abortable-iterator": "^2.1.0",
"aegir": "^20.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"chai-checkmark": "^1.0.1",
"cids": "^0.7.1",
"delay": "^4.3.0",
Expand Down
34 changes: 10 additions & 24 deletions test/dialing/direct.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const sinon = require('sinon')
const Transport = require('libp2p-tcp')
Expand Down Expand Up @@ -77,14 +78,9 @@ describe('Dialing (direct, TCP)', () => {
it('should fail to connect to an unsupported multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })

try {
await dialer.connectToMultiaddr(unsupportedAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToMultiaddr(unsupportedAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
})

it('should be able to connect to a given peer info', async () => {
Expand Down Expand Up @@ -121,14 +117,9 @@ describe('Dialing (direct, TCP)', () => {
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(unsupportedAddr)

try {
await dialer.connectToPeer(peerInfo)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToPeer(peerInfo))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED)
})

it('should abort dials on queue task timeout', async () => {
Expand All @@ -144,14 +135,9 @@ describe('Dialing (direct, TCP)', () => {
expect(options.signal.aborted).to.equal(true)
})

try {
await dialer.connectToMultiaddr(remoteAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToMultiaddr(remoteAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
})

it('should dial to the max concurrency', async () => {
Expand Down
34 changes: 10 additions & 24 deletions test/dialing/direct.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const sinon = require('sinon')
const pDefer = require('p-defer')
Expand Down Expand Up @@ -67,14 +68,9 @@ describe('Dialing (direct, WebSockets)', () => {
it('should fail to connect to an unsupported multiaddr', async () => {
const dialer = new Dialer({ transportManager: localTM })

try {
await dialer.connectToMultiaddr(unsupportedAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToMultiaddr(unsupportedAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_DIAL_FAILED)
})

it('should be able to connect to a given peer', async () => {
Expand All @@ -94,14 +90,9 @@ describe('Dialing (direct, WebSockets)', () => {
const peerInfo = new PeerInfo(peerId)
peerInfo.multiaddrs.add(unsupportedAddr)

try {
await dialer.connectToPeer(peerInfo)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_CONNECTION_FAILED)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToPeer(peerInfo))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_CONNECTION_FAILED)
})

it('should abort dials on queue task timeout', async () => {
Expand All @@ -117,14 +108,9 @@ describe('Dialing (direct, WebSockets)', () => {
expect(options.signal.aborted).to.equal(true)
})

try {
await dialer.connectToMultiaddr(remoteAddr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TIMEOUT)
return
}

expect.fail('Dial should have failed')
await expect(dialer.connectToMultiaddr(remoteAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TIMEOUT)
})

it('should dial to the max concurrency', async () => {
Expand Down
42 changes: 13 additions & 29 deletions test/dialing/relay.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const sinon = require('sinon')

Expand Down Expand Up @@ -87,13 +88,9 @@ describe('Dialing (via relay, TCP)', () => {
.encapsulate(`/p2p/${relayIdString}`)
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)

try {
await srcLibp2p.dial(dialAddr)
expect.fail('Dial should have failed')
} catch (err) {
expect(err).to.exist()
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
}
await expect(srcLibp2p.dial(dialAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
})

it('should not stay connected to a relay when not already connected and HOP fails', async () => {
Expand All @@ -104,13 +101,9 @@ describe('Dialing (via relay, TCP)', () => {
.encapsulate(`/p2p/${relayIdString}`)
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)

try {
await srcLibp2p.dial(dialAddr)
expect.fail('Dial should have failed')
} catch (err) {
expect(err).to.exist()
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
}
await expect(srcLibp2p.dial(dialAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)

// We should not be connected to the relay, because we weren't before the dial
const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
Expand All @@ -125,16 +118,11 @@ describe('Dialing (via relay, TCP)', () => {
.encapsulate(`/p2p/${relayIdString}`)
.encapsulate(`/p2p-circuit/p2p/${dstLibp2p.peerInfo.id.toString()}`)

// Connect to the relay first
await srcLibp2p.dial(relayAddr)

try {
await srcLibp2p.dial(dialAddr)
expect.fail('Dial should have failed')
} catch (err) {
expect(err).to.exist()
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
}
await expect(srcLibp2p.dial(dialAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)

const srcToRelayConn = srcLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
expect(srcToRelayConn).to.exist()
Expand All @@ -159,13 +147,9 @@ describe('Dialing (via relay, TCP)', () => {
buffer: Buffer.from('an invalid multiaddr')
}])

try {
await srcLibp2p.dial(dialAddr)
expect.fail('Dial should have failed')
} catch (err) {
expect(err).to.exist()
expect(err).to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)
}
await expect(srcLibp2p.dial(dialAddr))
.to.eventually.be.rejected()
.and.to.have.property('code', Errors.ERR_HOP_REQUEST_FAILED)

const dstToRelayConn = dstLibp2p.registrar.getConnection(relayLibp2p.peerInfo)
expect(dstToRelayConn).to.exist()
Expand Down
27 changes: 13 additions & 14 deletions test/identify/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const sinon = require('sinon')

Expand Down Expand Up @@ -98,20 +99,18 @@ describe('Identify', () => {
sinon.stub(localConnectionMock, 'newStream').returns({ stream: local, protocol: multicodecs.IDENTIFY })

// Run identify
try {
await Promise.all([
localIdentify.identify(localConnectionMock, localPeer.id),
remoteIdentify.handleMessage({
connection: remoteConnectionMock,
stream: remote,
protocol: multicodecs.IDENTIFY
})
])
expect.fail('should have thrown')
} catch (err) {
expect(err).to.exist()
expect(err.code).to.eql(Errors.ERR_INVALID_PEER)
}
const identifyPromise = Promise.all([
localIdentify.identify(localConnectionMock, localPeer.id),
remoteIdentify.handleMessage({
connection: remoteConnectionMock,
stream: remote,
protocol: multicodecs.IDENTIFY
})
])

await expect(identifyPromise)
.to.eventually.be.rejected()
.and.to.have.property('code', Errors.ERR_INVALID_PEER)
})

describe('push', () => {
Expand Down
16 changes: 2 additions & 14 deletions test/registrar/registrar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,14 @@ describe('registrar', () => {
})

it('should fail to register a protocol if no multicodec is provided', () => {
try {
registrar.register()
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to register a protocol if no multicodec is provided')
expect(() => registrar.register()).to.throw()
})

it('should fail to register a protocol if an invalid topology is provided', () => {
const fakeTopology = {
random: 1
}
try {
registrar.register()
} catch (err) {
expect(err).to.exist(fakeTopology)
return
}
throw new Error('should fail to register a protocol if an invalid topology is provided')
expect(() => registrar.register(fakeTopology)).to.throw()
})
})

Expand Down
48 changes: 22 additions & 26 deletions test/transports/transport-manager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const chai = require('chai')
chai.use(require('dirty-chai'))
chai.use(require('chai-as-promised'))
const { expect } = chai
const sinon = require('sinon')

Expand Down Expand Up @@ -39,21 +40,25 @@ describe('Transport Manager (WebSockets)', () => {
await tm.remove(Transport.prototype[Symbol.toStringTag])
})

it('should not be able to add a transport without a key', () => {
expect(() => {
it('should not be able to add a transport without a key', async () => {
// Chai as promised conflicts with normal `throws` validation,
// so wrap the call in an async function
await expect((async () => { // eslint-disable-line
tm.add(undefined, Transport)
}).to.throw().that.satisfies((err) => {
return err.code === ErrorCodes.ERR_INVALID_KEY
})
})())
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_INVALID_KEY)
})

it('should not be able to add a transport twice', () => {
it('should not be able to add a transport twice', async () => {
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
expect(() => {
// Chai as promised conflicts with normal `throws` validation,
// so wrap the call in an async function
await expect((async () => { // eslint-disable-line
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
}).to.throw().that.satisfies((err) => {
return err.code === ErrorCodes.ERR_DUPLICATE_TRANSPORT
})
})())
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_DUPLICATE_TRANSPORT)
})

it('should be able to dial', async () => {
Expand All @@ -67,27 +72,18 @@ describe('Transport Manager (WebSockets)', () => {
it('should fail to dial an unsupported address', async () => {
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
const addr = multiaddr('/ip4/127.0.0.1/tcp/0')
try {
await tm.dial(addr)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
return
}

expect.fail('Dial attempt should have failed')
await expect(tm.dial(addr))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_TRANSPORT_UNAVAILABLE)
})

it('should fail to listen with no valid address', async () => {
tm.add(Transport.prototype[Symbol.toStringTag], Transport)
const addrs = [multiaddr('/ip4/127.0.0.1/tcp/0')]
try {
await tm.listen(addrs)
} catch (err) {
expect(err).to.satisfy((err) => err.code === ErrorCodes.ERR_NO_VALID_ADDRESSES)
return
}

expect.fail('should have failed')

await expect(tm.listen(addrs))
.to.eventually.be.rejected()
.and.to.have.property('code', ErrorCodes.ERR_NO_VALID_ADDRESSES)
})
})

Expand Down

0 comments on commit 6fd7d0e

Please sign in to comment.