1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const cp = require('child_process'); 5const path = require('path'); 6const tmpdir = require('../common/tmpdir'); 7const fs = require('fs'); 8const util = require('util'); 9 10if (!common.isMainThread) 11 common.skip('process.chdir is not available in Workers'); 12 13const traceFile = 'node_trace.1.log'; 14 15tmpdir.refresh(); 16process.chdir(tmpdir.path); 17 18const test_str = 'const dns = require("dns");' + 19'const options = {' + 20' family: 4,' + 21' hints: dns.ADDRCONFIG | dns.V4MAPPED,' + 22'};'; 23 24const tests = { 25 'lookup': 'dns.lookup("example.com", options, (err, address, family) => {});', 26 'lookupService': 'dns.lookupService("127.0.0.1", 22, ' + 27 '(err, hostname, service) => {});', 28 'reverse': 'dns.reverse("8.8.8.8", (err, hostnames) => {});', 29 'resolveAny': 'dns.resolveAny("example.com", (err, res) => {});', 30 'resolve4': 'dns.resolve4("example.com", (err, res) => {});', 31 'resolve6': 'dns.resolve6("example.com", (err, res) => {});', 32 'resolveCaa': 'dns.resolveCaa("example.com", (err, res) => {});', 33 'resolveCname': 'dns.resolveCname("example.com", (err, res) => {});', 34 'resolveMx': 'dns.resolveMx("example.com", (err, res) => {});', 35 'resolveNs': 'dns.resolveNs("example.com", (err, res) => {});', 36 'resolveTxt': 'dns.resolveTxt("example.com", (err, res) => {});', 37 'resolveSrv': 'dns.resolveSrv("example.com", (err, res) => {});', 38 'resolvePtr': 'dns.resolvePtr("example.com", (err, res) => {});', 39 'resolveNaptr': 'dns.resolveNaptr("example.com", (err, res) => {});', 40 'resolveSoa': 'dns.resolveSoa("example.com", (err, res) => {});' 41}; 42 43for (const tr in tests) { 44 const proc = cp.spawnSync(process.execPath, 45 [ '--trace-event-categories', 46 'node.dns.native', 47 '-e', 48 test_str + tests[tr], 49 ], 50 { encoding: 'utf8' }); 51 52 // Make sure the operation is successful. 53 // Don't use assert with a custom message here. Otherwise the 54 // inspection in the message is done eagerly and wastes a lot of CPU 55 // time. 56 if (proc.status !== 0) { 57 throw new Error(`${tr}:\n${util.inspect(proc)}`); 58 } 59 60 const file = path.join(tmpdir.path, traceFile); 61 62 const data = fs.readFileSync(file); 63 const traces = JSON.parse(data.toString()).traceEvents 64 .filter((trace) => trace.cat !== '__metadata'); 65 66 assert(traces.length > 0); 67 68 // DNS native trace events should be generated. 69 assert(traces.some((trace) => { 70 return (trace.name === tr && trace.pid === proc.pid); 71 })); 72} 73