summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-send-callback-recursive.js
blob: bd2a8e7d918c3d6e714bebb01b1dfd1043635867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'use strict';
const common = require('../common');
const assert = require('assert');

const dgram = require('dgram');
const client = dgram.createSocket('udp4');
const chunk = 'abc';
var recursiveCount = 0;
var received = 0;
const limit = 10;
const recursiveLimit = 100;

function onsend() {
  if (recursiveCount > recursiveLimit) {
    throw new Error('infinite loop detected');
  }
  if (received < limit) {
    client.send(
      chunk, 0, chunk.length, common.PORT, common.localhostIPv4, onsend);
  }
  recursiveCount++;
}

client.on('listening', function() {
  onsend();
});

client.on('message', function(buf, info) {
  received++;
  if (received === limit) {
    client.close();
  }
});

client.on('close', common.mustCall(function() {
  assert.equal(received, limit);
}));

client.bind(common.PORT);