Skip to content
This repository
Browse code

Sending explicit badmessage event when cannot deserialize.

Needed so that sequencer doesn't get stuck.
  • Loading branch information...
commit 6adbd98a483cb1a322eebb6431e785e0eb6923ec 1 parent 0ea9664
Chris Tavares authored March 15, 2013
21  lib/servicebusconnector.js
@@ -51,8 +51,10 @@ ServiceBusConnector.prototype.start = function () {
51 51
 
52 52
       if (!err) {
53 53
         var msg = self.unpackMessage(receivedMessage);
54  
-        if (msg !== null) {
  54
+        if (msg.args !== null) {
55 55
           self.emit('message', msg.nodeId, msg.name, msg.args, msg.seq);
  56
+        } else {
  57
+          self.emit('badmessage', msg.nodeId, msg.name, msg.seq);
56 58
         }
57 59
       }
58 60
 
@@ -100,16 +102,19 @@ ServiceBusConnector.prototype.packMessage = function(name, args) {
100 102
 }
101 103
 
102 104
 ServiceBusConnector.prototype.unpackMessage = function(message) {
  105
+  var result = {
  106
+    name: message.brokerProperties.Label,
  107
+    nodeId: message.brokerProperties.CorrelationId,
  108
+    seq: +message.brokerProperties.SequenceNumber,
  109
+    args: null
  110
+  };
  111
+
103 112
   try {
104  
-    return {
105  
-      name: message.brokerProperties.Label,
106  
-      nodeId: message.brokerProperties.CorrelationId,
107  
-      args: JSON.parse(message.body),
108  
-      seq: +message.brokerProperties.SequenceNumber
109  
-    };
  113
+    result.args = JSON.parse(message.body);
  114
+    return result;
110 115
   } catch (ex) {
111 116
     // Issue unpacking the message, assume it's bad and toss it
112  
-    return null;
  117
+    return result;
113 118
   }
114 119
 }
115 120
 
20  test/servicebusconnector-tests.js
@@ -209,15 +209,31 @@ describe('Service Bus connection layer', function () {
209 209
       done();
210 210
     });
211 211
 
212  
-    it('should not raise event and repoll on undeserializable message', function (done) {
  212
+    it('should not raise message event and repoll on undeserializable message', function (done) {
213 213
       connector.on('message', function (nodeId, name, args, seq) {
214 214
         done(new Error('Message received when deserialization fails. This should not happen.'));
215 215
       });
216 216
 
217  
-      receive(null, 'This is not valid JSON');
  217
+      var msg = packMessage(connector, 'anotherNode', 'aMessage', null, 12);
  218
+      msg.body = 'This is not valid JSON';
  219
+      receive(null, msg);
218 220
       sb.receiveSubscriptionMessage.calledTwice.should.be.true;
219 221
       done();
220 222
     });
  223
+
  224
+    it('should raise badmessage event on undeserializable message', function (done) {
  225
+      connector.on('badmessage', function (nodeId, name, seq) {
  226
+        nodeId.should.equal('anotherNode');
  227
+        name.should.equal('aMessage');
  228
+        seq.should.equal(12);
  229
+        done();
  230
+      });
  231
+
  232
+      var msg = packMessage(connector, 'anotherNode', 'aMessage', null, 12);
  233
+      msg.body = 'This is not valid JSON';
  234
+      receive(null, msg);
  235
+      sb.receiveSubscriptionMessage.calledTwice.should.be.true;      
  236
+    });
221 237
   });
222 238
 
223 239
   describe('when receiving with multiple receives at a time', function () {

0 notes on commit 6adbd98

Please sign in to comment.
Something went wrong with that request. Please try again.