Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating multiple contexts causes real problems #38

Closed
pghalliday opened this issue Apr 24, 2013 · 6 comments
Closed

Creating multiple contexts causes real problems #38

pghalliday opened this issue Apr 24, 2013 · 6 comments

Comments

@pghalliday
Copy link

While investigating workarounds for issue #37 I tried to create multiple contexts pointing to the same server.

First I tried this:

var rabbitJS = require('rabbit.js');
var context0 = rabbitJS.createContext('amqp://localhost');
context0.on('ready', function() {
  var pub = context0.socket('PUB');
  pub.connect('events', function() {
    var context1 = rabbitJS.createContext('amqp://localhost');
    context1.on('ready', function() {
      var sub1 = context1.socket('SUB');
      sub1.setEncoding('utf8');
      sub1.on('data', function(data) {
        console.log('subscriber 1');
        console.log(data);
      });
      sub1.connect('events', function() {
        var context2 = rabbitJS.createContext('amqp://localhost');
        context2.on('ready', function() {
          var sub2 = context2.socket('SUB');
          sub2.setEncoding('utf8');
          sub2.on('data', function(data) {
            console.log('subscriber 2');
            console.log(data);
          });
          sub2.connect('events', function() {
            console.log('publish');
            pub.write(JSON.stringify({welcome: 'rabbit.js', source: 'pub'}), 'utf8');
          });
        });
      });
    });
  });
});

with the following result:

Unhandled connection error: COMMAND_INVALID - second 'channel.open' seen

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: COMMAND_INVALID - second 'channel.open' seen
    at Connection._onMethod (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:1155:15)
    at AMQPParser.parser.onMethod (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:897:12)
    at AMQPParser._parseMethodFrame (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:451:10)
    at frameEnd (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:192:16)
    at frame (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:177:14)
    at AMQPParser.header [as parse] (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:163:14)
    at AMQPParser.execute (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:236:21)
    at Connection.<anonymous> (/home/pghalliday/Development/testRabbitMQ/node_modules/rabbit.js/node_modules/amqp/amqp.js:935:14)
    at Connection.EventEmitter.emit (events.js:95:17)
    at Connection.<anonymous> (_stream_readable.js:710:14)

I'm guessing multiple contexts are not supported (although I think they may be useful/vital when I get round to creating unit and integration tests)

I then tried a similar experiment with just 2 contexts:

var rabbitJS = require('rabbit.js');
var context0 = rabbitJS.createContext('amqp://localhost');
context0.on('ready', function() {
  var pub = context0.socket('PUB');
  pub.connect('events', function() {
    var context1 = rabbitJS.createContext('amqp://localhost');
    context1.on('ready', function() {
      var sub = context1.socket('SUB');
      sub.setEncoding('utf8');
      sub.on('data', function(data) {
        console.log('subscriber 1');
        console.log(data);
      });
      sub.connect('events', function() {
        console.log('publish');
        pub.write(JSON.stringify({welcome: 'rabbit.js', source: 'pub'}), 'utf8');
      });
    });
  });
});

This time I got the following result:

publish
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
{"welcome":"rabbit.js","source":"pub"}
subscriber 1
... etc forever ...

This was clearly an infinite loop. Again am I misunderstanding something about how RabbitMQ works. This seems like a pretty serious issue too but possibly not as bad as the last #37

@squaremo
Copy link
Owner

OK I think that second thing is fixed in #35. Specifically, with "works" meaning it prints out things just once, it

  • always works with Node.JS v0.8
  • works with Node.JS and rabbit.js master
  • does not work with Node.JS v0.10.4 and rabbit.js after git checkout f493e78

I'll look into the first thing ..

@squaremo
Copy link
Owner

I'll look into the first thing ..

The first test case above also appears to be due to EventEmitter not working the way it used to. (It used to work by coincidence; now it's broken by coincidence).

If you have a moment would you check with a master checkout? Thanks!

@pghalliday
Copy link
Author

i'll take a look

@pghalliday
Copy link
Author

Indeed this works with master :)

@pghalliday
Copy link
Author

looking forward to the next release then :D

@squaremo
Copy link
Owner

Ha, yes good point, well made. Yeah I ought to do a trawl for any other breakages, and post a new release. It's been a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants