summaryrefslogtreecommitdiff
path: root/test/parallel/test-async-wrap-uid.js
blob: f16388cfe766f0959d77e1d37efd67aed922f673 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';

require('../common');
const fs = require('fs');
const assert = require('assert');
const async_wrap = process.binding('async_wrap');

// Give the event loop time to clear out the final uv_close().
let si_cntr = 3;
process.on('beforeExit', () => {
  if (--si_cntr > 0) setImmediate(() => {});
});

const storage = new Map();
async_wrap.setupHooks({ init, pre, post, destroy });
async_wrap.enable();

function init(uid) {
  storage.set(uid, {
    init: true,
    pre: false,
    post: false,
    destroy: false,
  });
}

function pre(uid) {
  storage.get(uid).pre = true;
}

function post(uid) {
  storage.get(uid).post = true;
}

function destroy(uid) {
  storage.get(uid).destroy = true;
}

fs.access(__filename, function(err) {
  assert.ifError(err);
});

fs.access(__filename, function(err) {
  assert.ifError(err);
});

async_wrap.disable();

process.once('exit', function() {
  assert.strictEqual(storage.size, 2);

  for (const item of storage) {
    const uid = item[0];
    const value = item[1];
    assert.strictEqual(typeof uid, 'number');
    assert.deepStrictEqual(value, {
      init: true,
      pre: true,
      post: true,
      destroy: true,
    });
  }
});