• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3const common = require('../common');
4const dc = require('diagnostics_channel');
5const assert = require('assert');
6
7const channel = dc.tracingChannel('test');
8
9const expectedResult = { foo: 'bar' };
10const input = { foo: 'bar' };
11const thisArg = { baz: 'buz' };
12
13function check(found) {
14  assert.deepStrictEqual(found, input);
15}
16
17const handlers = {
18  start: common.mustCall(check, 2),
19  end: common.mustCall(check, 2),
20  asyncStart: common.mustCall((found) => {
21    check(found);
22    assert.strictEqual(found.error, undefined);
23    assert.deepStrictEqual(found.result, expectedResult);
24  }, 2),
25  asyncEnd: common.mustCall((found) => {
26    check(found);
27    assert.strictEqual(found.error, undefined);
28    assert.deepStrictEqual(found.result, expectedResult);
29  }, 2),
30  error: common.mustNotCall()
31};
32
33channel.subscribe(handlers);
34
35channel.traceCallback(function(cb, err, res) {
36  assert.deepStrictEqual(this, thisArg);
37  setImmediate(cb, err, res);
38}, 0, input, thisArg, common.mustCall((err, res) => {
39  assert.strictEqual(err, null);
40  assert.deepStrictEqual(res, expectedResult);
41}), null, expectedResult);
42
43channel.tracePromise(function(value) {
44  assert.deepStrictEqual(this, thisArg);
45  return Promise.resolve(value);
46}, input, thisArg, expectedResult).then(
47  common.mustCall((value) => {
48    assert.deepStrictEqual(value, expectedResult);
49  }),
50  common.mustNotCall()
51);
52
53let failed = false;
54try {
55  channel.traceCallback(common.mustNotCall(), 0, input, thisArg, 1, 2, 3);
56} catch (err) {
57  assert.ok(/"callback" argument must be of type function/.test(err.message));
58  failed = true;
59}
60assert.strictEqual(failed, true);
61