Merge pull request #224 from murgatroid99/node_fix_test_hang2

Fixed Node tests hanging at the end again
diff --git a/src/node/test/call_test.js b/src/node/test/call_test.js
index 6e52ec8..b37c44a 100644
--- a/src/node/test/call_test.js
+++ b/src/node/test/call_test.js
@@ -34,8 +34,6 @@
 var assert = require('assert');
 var grpc = require('bindings')('grpc.node');
 
-var channel = new grpc.Channel('localhost:7070');
-
 /**
  * Helper function to return an absolute deadline given a relative timeout in
  * seconds.
@@ -49,6 +47,17 @@
 }
 
 describe('call', function() {
+  var channel;
+  var server;
+  before(function() {
+    server = new grpc.Server();
+    var port = server.addHttp2Port('localhost:0');
+    server.start();
+    channel = new grpc.Channel('localhost:' + port);
+  });
+  after(function() {
+    server.shutdown();
+  });
   describe('constructor', function() {
     it('should reject anything less than 3 arguments', function() {
       assert.throws(function() {
diff --git a/src/node/test/client_server_test.js b/src/node/test/client_server_test.js
index 99438a1..11cc511 100644
--- a/src/node/test/client_server_test.js
+++ b/src/node/test/client_server_test.js
@@ -86,14 +86,23 @@
 }
 
 describe('echo client', function() {
-  it('should receive echo responses', function(done) {
-    var server = new Server();
+  var server;
+  var channel;
+  before(function() {
+    server = new Server();
     var port_num = server.bind('0.0.0.0:0');
     server.register('echo', echoHandler);
+    server.register('error', errorHandler);
+    server.register('cancellation', cancelHandler);
     server.start();
 
+    channel = new grpc.Channel('localhost:' + port_num);
+  });
+  after(function() {
+    server.shutdown();
+  });
+  it('should receive echo responses', function(done) {
     var messages = ['echo1', 'echo2', 'echo3', 'echo4'];
-    var channel = new grpc.Channel('localhost:' + port_num);
     var stream = client.makeRequest(
         channel,
         'echo');
@@ -106,17 +115,10 @@
       index += 1;
     });
     stream.on('end', function() {
-      server.shutdown();
       done();
     });
   });
   it('should get an error status that the server throws', function(done) {
-    var server = new Server();
-    var port_num = server.bind('0.0.0.0:0');
-    server.register('error', errorHandler);
-    server.start();
-
-    var channel = new grpc.Channel('localhost:' + port_num);
     var stream = client.makeRequest(
         channel,
         'error',
@@ -129,17 +131,10 @@
     stream.on('status', function(status) {
       assert.equal(status.code, grpc.status.UNIMPLEMENTED);
       assert.equal(status.details, 'error details');
-      server.shutdown();
       done();
     });
   });
   it('should be able to cancel a call', function(done) {
-    var server = new Server();
-    var port_num = server.bind('0.0.0.0:0');
-    server.register('cancellation', cancelHandler);
-    server.start();
-
-    var channel = new grpc.Channel('localhost:' + port_num);
     var stream = client.makeRequest(
         channel,
         'cancellation',
@@ -149,7 +144,6 @@
     stream.cancel();
     stream.on('status', function(status) {
       assert.equal(status.code, grpc.status.CANCELLED);
-      server.shutdown();
       done();
     });
   });
@@ -157,7 +151,9 @@
 /* TODO(mlumish): explore options for reducing duplication between this test
  * and the insecure echo client test */
 describe('secure echo client', function() {
-  it('should recieve echo responses', function(done) {
+  var server;
+  var channel;
+  before(function(done) {
     fs.readFile(ca_path, function(err, ca_data) {
       assert.ifError(err);
       fs.readFile(key_path, function(err, key_data) {
@@ -169,34 +165,40 @@
                                                               key_data,
                                                               pem_data);
 
-          var server = new Server({'credentials' : server_creds});
+          server = new Server({'credentials' : server_creds});
           var port_num = server.bind('0.0.0.0:0', true);
           server.register('echo', echoHandler);
           server.start();
 
-          var messages = ['echo1', 'echo2', 'echo3', 'echo4'];
-          var channel = new grpc.Channel('localhost:' + port_num, {
+          channel = new grpc.Channel('localhost:' + port_num, {
             'grpc.ssl_target_name_override' : 'foo.test.google.com',
             'credentials' : creds
           });
-          var stream = client.makeRequest(
-              channel,
-              'echo');
-
-          _(messages).map(function(val) {
-            return new Buffer(val);
-          }).pipe(stream);
-          var index = 0;
-          stream.on('data', function(chunk) {
-            assert.equal(messages[index], chunk.toString());
-            index += 1;
-          });
-          stream.on('end', function() {
-            server.shutdown();
-            done();
-          });
+          done();
         });
       });
     });
   });
+  after(function() {
+    server.shutdown();
+  });
+  it('should recieve echo responses', function(done) {
+    var messages = ['echo1', 'echo2', 'echo3', 'echo4'];
+    var stream = client.makeRequest(
+        channel,
+        'echo');
+
+    _(messages).map(function(val) {
+      return new Buffer(val);
+    }).pipe(stream);
+    var index = 0;
+    stream.on('data', function(chunk) {
+      assert.equal(messages[index], chunk.toString());
+      index += 1;
+    });
+    stream.on('end', function() {
+      server.shutdown();
+      done();
+    });
+  });
 });
diff --git a/src/node/test/end_to_end_test.js b/src/node/test/end_to_end_test.js
index f7ccbcf..f8cb660 100644
--- a/src/node/test/end_to_end_test.js
+++ b/src/node/test/end_to_end_test.js
@@ -56,14 +56,21 @@
 }
 
 describe('end-to-end', function() {
+  var server;
+  var channel;
+  before(function() {
+    server = new grpc.Server();
+    var port_num = server.addHttp2Port('0.0.0.0:0');
+    server.start();
+    channel = new grpc.Channel('localhost:' + port_num);
+  });
+  after(function() {
+    server.shutdown();
+  });
   it('should start and end a request without error', function(complete) {
-    var server = new grpc.Server();
     var done = multiDone(function() {
       complete();
-      server.shutdown();
     }, 2);
-    var port_num = server.addHttp2Port('0.0.0.0:0');
-    var channel = new grpc.Channel('localhost:' + port_num);
     var deadline = new Date();
     deadline.setSeconds(deadline.getSeconds() + 3);
     var status_text = 'xyz';
@@ -81,7 +88,6 @@
       done();
     }, 0);
 
-    server.start();
     server.requestCall(function(event) {
       assert.strictEqual(event.type, grpc.completionType.SERVER_RPC_NEW);
       var server_call = event.call;
@@ -109,13 +115,10 @@
   it('should send and receive data without error', function(complete) {
     var req_text = 'client_request';
     var reply_text = 'server_response';
-    var server = new grpc.Server();
     var done = multiDone(function() {
       complete();
       server.shutdown();
     }, 6);
-    var port_num = server.addHttp2Port('0.0.0.0:0');
-    var channel = new grpc.Channel('localhost:' + port_num);
     var deadline = new Date();
     deadline.setSeconds(deadline.getSeconds() + 3);
     var status_text = 'success';
@@ -151,8 +154,6 @@
       assert.strictEqual(event.data.toString(), reply_text);
       done();
     });
-
-    server.start();
     server.requestCall(function(event) {
       assert.strictEqual(event.type, grpc.completionType.SERVER_RPC_NEW);
       var server_call = event.call;
diff --git a/src/node/test/server_test.js b/src/node/test/server_test.js
index 457d13d..d0ec155 100644
--- a/src/node/test/server_test.js
+++ b/src/node/test/server_test.js
@@ -65,17 +65,22 @@
 }
 
 describe('echo server', function() {
-  it('should echo inputs as responses', function(done) {
-    done = multiDone(done, 4);
-    var server = new Server();
+  var server;
+  var channel;
+  before(function() {
+    server = new Server();
     var port_num = server.bind('[::]:0');
     server.register('echo', echoHandler);
     server.start();
 
+    channel = new grpc.Channel('localhost:' + port_num);
+  });
+  it('should echo inputs as responses', function(done) {
+    done = multiDone(done, 4);
+
     var req_text = 'echo test string';
     var status_text = 'OK';
 
-    var channel = new grpc.Channel('localhost:' + port_num);
     var deadline = new Date();
     deadline.setSeconds(deadline.getSeconds() + 3);
     var call = new grpc.Call(channel,