test: refactor test-dgram-send-callback-recursive
authorSantiago Gimeno <santiago.gimeno@gmail.com>
Thu, 4 Feb 2016 17:33:44 +0000 (18:33 +0100)
committerMyles Borins <mborins@us.ibm.com>
Wed, 2 Mar 2016 22:01:11 +0000 (14:01 -0800)
Just send 10 messages recursively and check that the send calls are
asynchronous by asserting that a `setImmediate` callback has been called
in-between. It avoids a race condition in the test when the recursive
limit is reached without having received at least 10 messages.

Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/5079

test/parallel/test-dgram-send-callback-recursive.js

index bd2a8e7d918c3d6e714bebb01b1dfd1043635867..63edd5e58716f351a9dbb0fa07d0a78f6ef48e84 100644 (file)
@@ -5,23 +5,25 @@ const assert = require('assert');
 const dgram = require('dgram');
 const client = dgram.createSocket('udp4');
 const chunk = 'abc';
-var recursiveCount = 0;
-var received = 0;
+let received = 0;
+let sent = 0;
 const limit = 10;
-const recursiveLimit = 100;
+let async = false;
 
 function onsend() {
-  if (recursiveCount > recursiveLimit) {
-    throw new Error('infinite loop detected');
-  }
-  if (received < limit) {
+  if (sent++ < limit) {
     client.send(
       chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend);
+  } else {
+    assert.strictEqual(async, true, 'Send should be asynchronous.');
   }
-  recursiveCount++;
 }
 
 client.on('listening', function() {
+  setImmediate(function() {
+    async = true;
+  });
+
   onsend();
 });