Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Re subscribe if the connection goes down #7

Merged
merged 2 commits into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 16, 2012
  1. @tellnes
  2. @tellnes

    add test for re subscribe

    tellnes authored
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 1 deletion.
  1. +7 −1 index.js
  2. +44 −0 test/resubscribe.js
View
8 index.js
@@ -36,7 +36,7 @@ exports.connect = function () {
up.on('down', self.emit.bind(self, 'down'));
up.on('reconnect', self.emit.bind(self, 'reconnect'));
- [ 'free', 'query', 'assume', 'get', 'service', 'subscribe' ]
+ [ 'free', 'query', 'assume', 'get', 'service' ]
.forEach(function (name) {
self[name] = function () {
var args = [].slice.call(arguments);
@@ -48,6 +48,12 @@ exports.connect = function () {
})
;
+ self.subscribe = function(eventName, cb) {
+ up.on('up', function () {
+ up.remote.subscribe(eventName, cb);
+ });
+ };
+
// Hook into `on` method and proxy listeners for `allocate`,
// `assume` and `free` events to the `subscribe` method instead.
var origOn = self.on;
View
44 test/resubscribe.js
@@ -0,0 +1,44 @@
+var seaport = require('../');
+var test = require('tap').test;
+
+test('resubscribe if connection dies', function(t) {
+ var serverPort = Math.floor(Math.random() * 5e4 + 1e4),
+ server = seaport.createServer(),
+ client = seaport.connect('localhost:' + serverPort, {reconnect: 10}),
+ allocatedPort = 1234;
+
+ t.plan(6);
+
+ var eventNames = ['allocate', 'assume', 'free'];
+
+ eventNames.forEach(function(eventName) {
+ client.subscribe(eventName, function() {
+ t.ok(true, eventName + ' emitted');
+ });
+ });
+
+ function emit() {
+ eventNames.forEach(function(eventName) {
+ server.emit(eventName, {});
+ });
+ }
+
+ server.listen(serverPort);
+
+ // Wait for client to connect
+ setTimeout(emit, 200)
+
+ // Wait for first tests to finish
+ setTimeout(function() {
+ client.up.conn.stream.end();
+ }, 400);
+
+ // Run second test
+ setTimeout(emit, 1600);
+
+ t.on('end', function() {
+ server.close();
+ client.close();
+ t.ok(true, 'closed server');
+ });
+});
Something went wrong with that request. Please try again.