node/test/parallel/test-dgram-close.js
James M Snell 85ab4a5f12 buffer: add .from(), .alloc() and .allocUnsafe()
Several changes:

* Soft-Deprecate Buffer() constructors
* Add `Buffer.from()`, `Buffer.alloc()`, and `Buffer.allocUnsafe()`
* Add `--zero-fill-buffers` command line option
* Add byteOffset and length to `new Buffer(arrayBuffer)` constructor
* buffer.fill('') previously had no effect, now zero-fills
* Update the docs

PR-URL: https://github.com/nodejs/node/pull/4682
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
2016-03-16 08:34:02 -07:00

36 lines
879 B
JavaScript

'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);
});