forked from adonisjs/adonis-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cluster): write tests around cluster messaging
- Loading branch information
1 parent
3ec21a9
commit 205773c
Showing
9 changed files
with
240 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,14 @@ | ||
'use strict' | ||
|
||
/** | ||
* Enable it, since some tests rely on debug | ||
* statements | ||
* | ||
* @type {String} | ||
*/ | ||
process.env.DEBUG = 'adonis:websocket' | ||
process.env.DEBUG_COLORS = false | ||
process.env.DEBUG_HIDE_DATE = true | ||
|
||
const cli = require('japa/cli') | ||
cli.run('test/**/*.spec.js') |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
/** | ||
* adonis-websocket | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const ChannelsManager = require('../Channel/Manager') | ||
const debug = require('debug')('adonis:websocket') | ||
|
||
/** | ||
* Delivers the message from process to the channel | ||
* | ||
* @method deliverMessage | ||
* | ||
* @param {String} handle | ||
* @param {String} topic | ||
* @param {String} payload | ||
* | ||
* @return {void} | ||
*/ | ||
function deliverMessage (handle, topic, payload) { | ||
if (handle === 'broadcast') { | ||
const channel = ChannelsManager.resolve(topic) | ||
|
||
if (!channel) { | ||
return debug('broadcast topic %s cannot be handled by any channel', topic) | ||
} | ||
|
||
channel.clusterBroadcast(topic, payload) | ||
return | ||
} | ||
|
||
debug('dropping packet, since %s handle is not allowed', handle) | ||
} | ||
|
||
/** | ||
* Handles the messages received on a given process | ||
* | ||
* @method handleProcessMessage | ||
* | ||
* @param {String} message | ||
* | ||
* @return {void} | ||
*/ | ||
module.exports = function handleProcessMessage (message) { | ||
let decoded = null | ||
|
||
/** | ||
* Decoding the JSON message | ||
*/ | ||
try { | ||
decoded = JSON.parse(message) | ||
} catch (error) { | ||
debug('dropping packet, since not valid json') | ||
return | ||
} | ||
|
||
/** | ||
* Ignoring packet when there is no handle | ||
*/ | ||
if (!decoded.handle) { | ||
debug('dropping packet, since handle is missing') | ||
return | ||
} | ||
|
||
/** | ||
* Safely trying to deliver cluster messages | ||
*/ | ||
try { | ||
deliverMessage(decoded.handle, decoded.topic, decoded.payload) | ||
} catch (error) { | ||
debug('unable to process cluster message with error %o', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
/** | ||
* adonis-websocket | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
const debug = require('debug')('adonis:websocket') | ||
|
||
module.exports = function (handle, topic, payload) { | ||
try { | ||
process.send(JSON.stringify({ handle, topic, payload })) | ||
} catch (error) { | ||
debug('cluster.send error %o', error) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
'use strict' | ||
|
||
/** | ||
* adonis-websocket | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
const test = require('japa') | ||
const clusterReceiver = require('../../src/ClusterHop/receiver') | ||
const ChannelManager = require('../../src/Channel/Manager') | ||
const stderr = require('test-console').stderr | ||
|
||
test.group('Cluster Receiver', (group) => { | ||
test('ignore message when it\'s not json', (assert) => { | ||
assert.plan(1) | ||
const inspect = stderr.inspect() | ||
|
||
clusterReceiver({ name: 'virk' }) | ||
|
||
inspect.restore() | ||
assert.equal(inspect.output[0].trim(), 'adonis:websocket dropping packet, since not valid json') | ||
}) | ||
|
||
test('ignore message when handle is missing', (assert) => { | ||
assert.plan(1) | ||
const inspect = stderr.inspect() | ||
|
||
clusterReceiver(JSON.stringify({ topic: 'chat' })) | ||
|
||
inspect.restore() | ||
assert.equal(inspect.output[0].trim(), 'adonis:websocket dropping packet, since handle is missing') | ||
}) | ||
|
||
test('ignore message when handle is not one of the allowed handles', (assert) => { | ||
assert.plan(1) | ||
const inspect = stderr.inspect() | ||
|
||
clusterReceiver(JSON.stringify({ topic: 'chat', handle: 'foo' })) | ||
|
||
inspect.restore() | ||
assert.equal(inspect.output[0].trim(), 'adonis:websocket dropping packet, since foo handle is not allowed') | ||
}) | ||
|
||
test('ignore message when topic cannot be handled by any channel', (assert) => { | ||
assert.plan(1) | ||
const inspect = stderr.inspect() | ||
|
||
clusterReceiver(JSON.stringify({ topic: 'chat', handle: 'broadcast' })) | ||
|
||
inspect.restore() | ||
assert.equal(inspect.output[0].trim(), 'adonis:websocket broadcast topic chat cannot be handled by any channel') | ||
}) | ||
|
||
test('send message to channel responsible for handling the topic', (assert, done) => { | ||
assert.plan(2) | ||
const channel = ChannelManager.add('chat', () => {}) | ||
|
||
channel.clusterBroadcast = function (topic, payload) { | ||
assert.equal(topic, 'chat') | ||
assert.equal(payload, 'hello') | ||
done() | ||
} | ||
|
||
clusterReceiver(JSON.stringify({ topic: 'chat', handle: 'broadcast', payload: 'hello' })) | ||
ChannelManager.clear() | ||
}) | ||
}) |