Skip to content

Commit

Permalink
chore: review addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Jul 11, 2019
1 parent e238d43 commit 4edf353
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
6 changes: 5 additions & 1 deletion src/pubsub.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ module.exports = (node) => {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}

if (!Buffer.isBuffer(data) && typeof data !== 'string') {
return nextTick(callback, errCode(new Error('data must be a Buffer or a String'), 'ERR_DATA_IS_NOT_VALID'))
}

if (!Buffer.isBuffer(data)) {
return nextTick(callback, errCode(new Error('data must be a Buffer'), 'ERR_DATA_IS_NOT_A_BUFFER'))
data = Buffer.from(data)
}

pubsub.publish(topic, data, callback)
Expand Down
22 changes: 7 additions & 15 deletions test/pubsub.node.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ describe('.pubsub', () => {
expect(2).checks(done)

let nodes
const data = Buffer.from('test')
const data = 'test'
const handler = (msg) => {
// verify the data is correct and mark the expect
expect(msg.data).to.eql(data).mark()
expect(msg.data.toString()).to.eql(data).mark()
}

series([
Expand All @@ -80,10 +80,6 @@ describe('.pubsub', () => {
(cb) => setTimeout(cb, 500),
// publish on the second
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// ls subscripts
(cb) => nodes[1].pubsub.ls(cb),
// get subscribed peers
(cb) => nodes[1].pubsub.peers('pubsub', cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe on the first
Expand Down Expand Up @@ -140,7 +136,7 @@ describe('.pubsub', () => {
expect(err).to.not.exist().mark()
})
})
it('publish should fail if data is not a buffer', (done) => {
it('publish should fail if data is not a buffer nor a string', (done) => {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
peerDiscovery: {
Expand All @@ -158,9 +154,9 @@ describe('.pubsub', () => {
node.start((err) => {
expect(err).to.not.exist()

node.pubsub.publish('pubsub', 'datastr', (err) => {
node.pubsub.publish('pubsub', 10, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER')
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')

done()
})
Expand Down Expand Up @@ -197,10 +193,6 @@ describe('.pubsub', () => {
(cb) => setTimeout(cb, 500),
// publish on the second
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// ls subscripts
(cb) => nodes[1].pubsub.ls(cb),
// get subscribed peers
(cb) => nodes[1].pubsub.peers('pubsub', cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe on the first
Expand Down Expand Up @@ -282,9 +274,9 @@ describe('.pubsub', () => {
node.start((err) => {
expect(err).to.not.exist()

node.pubsub.publish('pubsub', 'datastr', (err) => {
node.pubsub.publish('pubsub', 10, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER')
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')

done()
})
Expand Down

0 comments on commit 4edf353

Please sign in to comment.