onjs 2.5.0 → 2.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -87,3 +87,11 @@ else
87
87
  createRabbitContainer
88
88
  sleep 5
89
89
  fi
90
+
91
+ function installDelayedMessageExchangePlugin() {
92
+ docker exec ${RABBIT_CONTAINER_NAME} sh -c "wget -q https://github.com/rabbitmq/rabbitmq-delayed-message-exchange/releases/download/v3.13.0/rabbitmq_delayed_message_exchange-3.13.0.ez -O /opt/rabbitmq/plugins/rabbitmq_delayed_message_exchange-3.13.0.ez"
93
+ docker exec ${RABBIT_CONTAINER_NAME} rabbitmq-plugins enable rabbitmq_delayed_message_exchange
94
+ }
95
+
96
+ echo "Ensuring the delayed-message exchange plugin is installed"
97
+ installDelayedMessageExchangePlugin
package/bin/test CHANGED
@@ -22,4 +22,5 @@ docker run --rm --name onjs_test_runner \
22
22
  --network onjs_test \
23
23
  -v ${PWD}:/app -w /app \
24
24
  -e "NODE_ENV=test" \
25
+ -e "ONJS_DELAYED_PLUGIN_TESTS=true" \
25
26
  rever/node:22 npm test
package/lib/on.js CHANGED
@@ -338,6 +338,13 @@ class On extends EventEmitter {
338
338
  }
339
339
  }
340
340
 
341
+ async _initDelayedQueues(delayedQueues, options) {
342
+ const queueNames = delayedQueues.map((queue) => queue.eventName)
343
+ this._log('info', 'Listening for delayedQueueNames', queueNames)
344
+ const listener = await this.queueInstance.listen(queueNames, options)
345
+ return listener
346
+ }
347
+
341
348
  async _onTaskReceived (task, data, options = {}) {
342
349
  this._eventLog('info', task.eventName, 'Executing task callback for task')
343
350
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onjs",
3
- "version": "2.5.0",
3
+ "version": "2.5.1",
4
4
  "description": "On.js is an event library based on RabbitMQ that helps you control events on a microservices arquitecture",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -14,6 +14,7 @@ describe('Queue', function () {
14
14
  var config = {
15
15
  mq: {
16
16
  exchange_name: 'onjs_test',
17
+ delayed_exchange_name: 'onjs_test_delayed',
17
18
  url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
18
19
  connectMaxAttempts: 1,
19
20
  delayMS: 10,
@@ -97,6 +98,38 @@ describe('Queue', function () {
97
98
  superpower: 'Fly'
98
99
  })
99
100
  })
101
+
102
+ it("Should initialize and process a delayed queue task", async function () {
103
+ // The delayed round trip needs the delayed-message exchange plugin,
104
+ // installed by bin/prepare-test-environment. CI runs the stock image
105
+ // without the plugin: bin/test sets ONJS_DELAYED_PLUGIN_TESTS=true so
106
+ // this test runs there, and CI skips it (the routing regression test
107
+ // still guards the delayed path on every broker).
108
+ if (process.env.ONJS_DELAYED_PLUGIN_TESTS !== 'true') this.skip()
109
+
110
+ var executed = null
111
+
112
+ function execution(incomingData) {
113
+ executed = incomingData
114
+ return
115
+ }
116
+
117
+ on
118
+ .taskReceived('delayedQueueRegression', { isDelayed: true })
119
+ .andProcess(execution)
120
+
121
+ await on.init()
122
+
123
+ await sender.send('delayedQueueRegression', { name: 'Delayed Superman' }, 300)
124
+
125
+ var waited = 0
126
+ while (!executed && waited < 5000) {
127
+ await new Promise((resolve) => { setTimeout(resolve, 100) })
128
+ waited += 100
129
+ }
130
+
131
+ executed.should.have.property('name', 'Delayed Superman')
132
+ })
100
133
  })
101
134
 
102
135
  describe('Queue with manual ack', function () {
@@ -105,6 +138,7 @@ describe('Queue with manual ack', function () {
105
138
  var config = {
106
139
  mq: {
107
140
  exchange_name: 'onjs_test_manual_ack',
141
+ delayed_exchange_name: 'onjs_test_manual_ack_delayed',
108
142
  url: 'amqp://rabbitmq:rabbitmq@rabbitmq:5672/',
109
143
  connectMaxAttempts: 1,
110
144
  delayMS: 10,
@@ -150,6 +184,7 @@ describe('Queue with manual ack', function () {
150
184
  var listenCalls = []
151
185
  var listenManualCalls = []
152
186
  var listenManualHandlers = []
187
+ var listenOptions = []
153
188
 
154
189
  onInstance.mqInstance.createChannel = async function () {
155
190
  return channel
@@ -158,8 +193,9 @@ describe('Queue with manual ack', function () {
158
193
  onInstance.queueInstance = {
159
194
  setChannel: function () {},
160
195
  on: function () {},
161
- listen: async function (queueNames) {
196
+ listen: async function (queueNames, options) {
162
197
  listenCalls.push(queueNames)
198
+ listenOptions.push(options)
163
199
  },
164
200
  listenManual: async function (queueNames, handler) {
165
201
  listenManualCalls.push(queueNames)
@@ -167,7 +203,7 @@ describe('Queue with manual ack', function () {
167
203
  }
168
204
  }
169
205
 
170
- return { onInstance: onInstance, channel: channel, listenCalls: listenCalls, listenManualCalls: listenManualCalls, listenManualHandlers: listenManualHandlers }
206
+ return { onInstance: onInstance, channel: channel, listenCalls: listenCalls, listenOptions: listenOptions, listenManualCalls: listenManualCalls, listenManualHandlers: listenManualHandlers }
171
207
  }
172
208
 
173
209
  it('Should route auto ack tasks to listen and manual ack tasks to the manual consumer', async function () {
@@ -193,6 +229,23 @@ describe('Queue with manual ack', function () {
193
229
  stubs.listenManualCalls[0].should.not.deepEqual(stubs.listenCalls[0])
194
230
  })
195
231
 
232
+ it('Should route delayed tasks through the auto-ack listen with isDelayed', async function () {
233
+ var stubs = createStubbedOn()
234
+
235
+ function execution (incomingData) {
236
+ return
237
+ }
238
+
239
+ stubs.onInstance
240
+ .taskReceived('delayedQueue', { isDelayed: true })
241
+ .andProcess(execution)
242
+
243
+ await stubs.onInstance._startEventListeners()
244
+
245
+ stubs.listenCalls.should.deepEqual([['delayedQueue']])
246
+ stubs.listenOptions[0].should.have.property('isDelayed', true)
247
+ })
248
+
196
249
  it('Should await the actions and ack the message after processing', async function () {
197
250
  var stubs = createStubbedOn()
198
251
  var executed = []