• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const assert = require('assert');
5const dns = require('dns');
6const { PerformanceObserver } = require('perf_hooks');
7
8const entries = [];
9const obs = new PerformanceObserver((items) => {
10  entries.push(...items.getEntries());
11});
12
13obs.observe({ type: 'dns' });
14
15let count = 0;
16
17function inc() {
18  count++;
19}
20
21// If DNS resolution fails, skip it
22// https://github.com/nodejs/node/issues/44003
23dns.lookup('localhost', common.mustCall((err) => { !err && inc(); }));
24dns.lookupService('127.0.0.1', 80, common.mustCall((err) => { !err && inc(); }));
25dns.resolveAny('localhost', common.mustCall((err) => { !err && inc(); }));
26
27dns.promises.lookup('localhost').then(inc).catch(() => {});
28dns.promises.lookupService('127.0.0.1', 80).then(inc).catch(() => {});
29dns.promises.resolveAny('localhost').then(inc).catch(() => {});
30
31process.on('exit', () => {
32  assert.strictEqual(entries.length, count);
33  entries.forEach((entry) => {
34    assert.strictEqual(!!entry.name, true);
35    assert.strictEqual(entry.entryType, 'dns');
36    assert.strictEqual(typeof entry.startTime, 'number');
37    assert.strictEqual(typeof entry.duration, 'number');
38    assert.strictEqual(typeof entry.detail, 'object');
39    switch (entry.name) {
40      case 'lookup':
41        assert.strictEqual(typeof entry.detail.hostname, 'string');
42        assert.strictEqual(typeof entry.detail.family, 'number');
43        assert.strictEqual(typeof entry.detail.hints, 'number');
44        assert.strictEqual(typeof entry.detail.verbatim, 'boolean');
45        assert.strictEqual(Array.isArray(entry.detail.addresses), true);
46        break;
47      case 'lookupService':
48        assert.strictEqual(typeof entry.detail.host, 'string');
49        assert.strictEqual(typeof entry.detail.port, 'number');
50        assert.strictEqual(typeof entry.detail.hostname, 'string');
51        assert.strictEqual(typeof entry.detail.service, 'string');
52        break;
53      case 'queryAny':
54        assert.strictEqual(typeof entry.detail.host, 'string');
55        assert.strictEqual(typeof entry.detail.ttl, 'boolean');
56        assert.strictEqual(Array.isArray(entry.detail.result), true);
57        break;
58    }
59  });
60});
61