Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

Commit

Permalink
fix: stop and start should not fail (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored Nov 19, 2019
1 parent c49fa92 commit eee2f61
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ class Network {
*/
start () {
if (this._running) {
throw errcode(new Error('Network is already running'), 'ERR_NETWORK_ALREADY_RUNNING')
return
}

// TODO add a way to check if switch has started or not
if (!this.dht.isStarted) {
throw errcode(new Error('Can not start network'), 'ERR_CANNOT_START_NETWORK')
Expand All @@ -59,7 +58,7 @@ class Network {
*/
stop () {
if (!this.dht.isStarted && !this.isStarted) {
throw errcode(new Error('Network is already stopped'), 'ERR_NETWORK_ALREADY_STOPPED')
return
}
this._running = false
this.dht.switch.removeListener('peer-mux-established', this._onPeerConnected)
Expand Down
21 changes: 4 additions & 17 deletions test/kad-dht.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,17 @@ describe('KadDHT', () => {
expect(dht.randomWalk.stop.calledOnce).to.equal(true) // Should be always disabled, as it can be started using the instance
})

// TODO: not fail!
it('fail when already started', async () => {
it('should not fail when already started', async () => {
const dht = createDHT(peerInfos[0])

await dht.start()
try {
await dht.start()
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to start when already registered')
await dht.start()
})

it('should fail to stop when was not started', () => {
it('should not fail to stop when was not started', () => {
const dht = createDHT(peerInfos[0])

try {
dht.stop()
} catch (err) {
expect(err).to.exist()
return
}
throw new Error('should fail to stop when was not started')
dht.stop()
})
})

Expand Down

0 comments on commit eee2f61

Please sign in to comment.