11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { executionAsyncResource, createHook } = require('async_hooks');
61cb0ef41Sopenharmony_ciconst { createServer, get } = require('http');
71cb0ef41Sopenharmony_ciconst sym = Symbol('cls');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Tests continuation local storage with the executionAsyncResource API
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciassert.ok(executionAsyncResource());
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cicreateHook({
141cb0ef41Sopenharmony_ci  init(asyncId, type, triggerAsyncId, resource) {
151cb0ef41Sopenharmony_ci    const cr = executionAsyncResource();
161cb0ef41Sopenharmony_ci    resource[sym] = cr[sym];
171cb0ef41Sopenharmony_ci  }
181cb0ef41Sopenharmony_ci}).enable();
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ciconst server = createServer(function(req, res) {
211cb0ef41Sopenharmony_ci  executionAsyncResource()[sym] = { state: req.url };
221cb0ef41Sopenharmony_ci  setTimeout(function() {
231cb0ef41Sopenharmony_ci    const { state } = executionAsyncResource()[sym];
241cb0ef41Sopenharmony_ci    res.setHeader('content-type', 'application/json');
251cb0ef41Sopenharmony_ci    res.end(JSON.stringify({ state }));
261cb0ef41Sopenharmony_ci  }, 10);
271cb0ef41Sopenharmony_ci});
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_cifunction test(n) {
301cb0ef41Sopenharmony_ci  get(`http://localhost:${server.address().port}/${n}`, common.mustCall(function(res) {
311cb0ef41Sopenharmony_ci    res.setEncoding('utf8');
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci    let body = '';
341cb0ef41Sopenharmony_ci    res.on('data', function(chunk) {
351cb0ef41Sopenharmony_ci      body += chunk;
361cb0ef41Sopenharmony_ci    });
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci    res.on('end', common.mustCall(function() {
391cb0ef41Sopenharmony_ci      assert.deepStrictEqual(JSON.parse(body), { state: `/${n}` });
401cb0ef41Sopenharmony_ci    }));
411cb0ef41Sopenharmony_ci  }));
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() {
451cb0ef41Sopenharmony_ci  server.unref();
461cb0ef41Sopenharmony_ci  for (let i = 0; i < 10; i++) {
471cb0ef41Sopenharmony_ci    test(i);
481cb0ef41Sopenharmony_ci  }
491cb0ef41Sopenharmony_ci}));
50