summaryrefslogtreecommitdiff
path: root/test/parallel/test-dgram-close.js
blob: 7dbeee1efb9b615dac5f17ed41a6331167fffe76 (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
'use strict';
// Ensure that if a dgram socket is closed before the DNS lookup completes, it
// won't crash.

const assert = require('assert');
const common = require('../common');
const dgram = require('dgram');

var buf = Buffer.alloc(1024, 42);

var socket = dgram.createSocket('udp4');
var handle = socket._handle;
var closeEvents = 0;
var closeCallbacks = 0;
socket.send(buf, 0, buf.length, common.PORT, 'localhost');
assert.strictEqual(socket.close(function() {
  ++closeCallbacks;
}), socket);
socket.on('close', function() {
  assert.equal(closeCallbacks, 1);
  ++closeEvents;
});
socket = null;

// Verify that accessing handle after closure doesn't throw
setImmediate(function() {
  setImmediate(function() {
    console.log('Handle fd is: ', handle.fd);
  });
});

process.on('exit', function() {
  assert.equal(closeEvents, 1);
  assert.equal(closeCallbacks, 1);
});