Skip to content

Commit 9f31b2f

Browse files
authored
Merge pull request #5 from dadi/feature/better-send
Feature: better send
2 parents de11a78 + 3272c9e commit 9f31b2f

2 files changed

Lines changed: 70 additions & 8 deletions

File tree

index.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,22 @@ QueueWrapper.prototype.initialiseQueue = function () {
3535
}
3636

3737
// public send function
38-
QueueWrapper.prototype.send = function (message, done) {
39-
const send = () => {
40-
let options = {
41-
qname: this.options.name,
42-
message: message,
43-
delay: this.getDelay(message)
44-
}
38+
QueueWrapper.prototype.send = function (address, data, done) {
39+
const options = {
40+
qname: this.options.name
41+
}
4542

46-
console.log(options)
43+
if (typeof data === 'function') {
44+
done = data
45+
options.message = address
46+
} else {
47+
const serializedData = typeof data === 'object' ? JSON.stringify(data) : data
48+
const encodedData = new Buffer(serializedData).toString('base64')
49+
options.message = `${address}:[[${encodedData}]]`
50+
}
4751

52+
const send = () => {
53+
options.delay = this.getDelay(options.message)
4854
this.rsmq.sendMessage(options, done)
4955
}
5056

test/queue.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,62 @@ describe('QueueWrapper', function (done) {
100100
})
101101
})
102102

103+
it ('should send a message when a string is passed', function (done) {
104+
queueWrapper = new QueueWrapper({
105+
name: 'myqueue'
106+
})
107+
108+
fakeRsmq.emit('connect', () => {
109+
110+
})
111+
112+
// send is faked above, so the response should contain the options
113+
// created by the queueWrapper to be sent as the message to the real queue
114+
queueWrapper.send('worker:message', (response) => {
115+
should.exist(response.qname)
116+
should.exist(response.message)
117+
response.message.should.equal('worker:message')
118+
done()
119+
})
120+
})
121+
122+
it ('should start a message with address when an address and object are passed', function (done) {
123+
queueWrapper = new QueueWrapper({
124+
name: 'myqueue'
125+
})
126+
127+
fakeRsmq.emit('connect', () => {
128+
129+
})
130+
131+
queueWrapper.send('worker', { 'test': true }, (response) => {
132+
should.exist(response.qname)
133+
should.exist(response.message)
134+
response.message.substr(0, 7).should.equal('worker:')
135+
done()
136+
})
137+
})
138+
139+
it ('should encapsulate the data with [[ ]] when an address and object are passed', function (done) {
140+
queueWrapper = new QueueWrapper({
141+
name: 'myqueue'
142+
})
143+
144+
fakeRsmq.emit('connect', () => {
145+
146+
})
147+
148+
queueWrapper.send('worker', { 'test': true }, (response) => {
149+
should.exist(response.qname)
150+
should.exist(response.message)
151+
152+
const messageData = response.message.substr(7)
153+
messageData.startsWith('[[').should.equal(true)
154+
messageData.endsWith(']]').should.equal(true)
155+
done()
156+
})
157+
})
158+
103159
it ('should return error when not connected', function (done) {
104160
queueWrapper = new QueueWrapper({
105161
name: 'myqueue'

0 commit comments

Comments
 (0)