• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --test-udp-no-try-send
2'use strict';
3
4const common = require('../common');
5const assert = require('assert');
6const initHooks = require('./init-hooks');
7const { checkInvocations } = require('./hook-checks');
8const dgram = require('dgram');
9
10const hooks = initHooks();
11
12hooks.enable();
13let send;
14
15const sock = dgram
16  .createSocket('udp4')
17  .on('listening', common.mustCall(onlistening))
18  .bind();
19
20function onlistening() {
21  sock.send(
22    Buffer.alloc(2), 0, 2, sock.address().port,
23    undefined, common.mustCall(onsent));
24
25  // Init not called synchronously because dns lookup always wraps
26  // callback in a next tick even if no lookup is needed
27  // TODO (trevnorris) submit patch to fix creation of tick objects and instead
28  // create the send wrap synchronously.
29  assert.strictEqual(hooks.activitiesOfTypes('UDPSENDWRAP').length, 0);
30}
31
32function onsent() {
33  const as = hooks.activitiesOfTypes('UDPSENDWRAP');
34  send = as[0];
35
36  assert.strictEqual(as.length, 1);
37  assert.strictEqual(send.type, 'UDPSENDWRAP');
38  assert.strictEqual(typeof send.uid, 'number');
39  assert.strictEqual(typeof send.triggerAsyncId, 'number');
40  checkInvocations(send, { init: 1, before: 1 }, 'when message sent');
41
42  sock.close(common.mustCall(onsockClosed));
43}
44
45function onsockClosed() {
46  checkInvocations(send, { init: 1, before: 1, after: 1 }, 'when sock closed');
47}
48
49process.on('exit', onexit);
50
51function onexit() {
52  hooks.disable();
53  hooks.sanityCheck('UDPSENDWRAP');
54  checkInvocations(send, { init: 1, before: 1, after: 1, destroy: 1 },
55                   'when process exits');
56}
57