test: prevent flakey test on pi2
authorTrevor Norris <trev.norris@gmail.com>
Wed, 2 Mar 2016 22:23:55 +0000 (15:23 -0700)
committerMyles Borins <mborins@us.ibm.com>
Mon, 21 Mar 2016 19:57:58 +0000 (12:57 -0700)
Looping rapidly and making new connections causes problems on pi2.
Instead create a new connection when an old connection has already been
made. Running a stress test of 600 times and they all passed.

Fixes: https://github.com/nodejs/node/issues/5302
PR-URL: https://github.com/nodejs/node/pull/5537
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Alexis Campailla <orangemocha@nodejs.org>
test/parallel/test-process-getactivehandles.js [new file with mode: 0644]

diff --git a/test/parallel/test-process-getactivehandles.js b/test/parallel/test-process-getactivehandles.js
new file mode 100644 (file)
index 0000000..e257439
--- /dev/null
@@ -0,0 +1,47 @@
+'use strict';
+
+const common = require('../common');
+const assert = require('assert');
+const net = require('net');
+const NUM = 8;
+const connections = [];
+const clients = [];
+var clients_counter = 0;
+
+const server = net.createServer(function listener(c) {
+  connections.push(c);
+}).listen(common.PORT, makeConnection);
+
+
+function makeConnection() {
+  if (clients_counter >= NUM) return;
+  net.connect(common.PORT, function connected() {
+    clientConnected(this);
+    makeConnection();
+  });
+}
+
+
+function clientConnected(client) {
+  clients.push(client);
+  if (++clients_counter >= NUM)
+    checkAll();
+}
+
+
+function checkAll() {
+  const handles = process._getActiveHandles();
+
+  clients.forEach(function(item) {
+    assert.ok(handles.indexOf(item) > -1);
+    item.destroy();
+  });
+
+  connections.forEach(function(item) {
+    assert.ok(handles.indexOf(item) > -1);
+    item.end();
+  });
+
+  assert.ok(handles.indexOf(server) > -1);
+  server.close();
+}