11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst domain = require('domain');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// setImmediate should run clear its queued cbs once per event loop turn
81cb0ef41Sopenharmony_ci// but immediates queued while processing the current queue should happen
91cb0ef41Sopenharmony_ci// on the next turn of the event loop.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci// In addition, if any setImmediate throws, the rest of the queue should
121cb0ef41Sopenharmony_ci// be processed after all error handling is resolved, but that queue
131cb0ef41Sopenharmony_ci// should not include any setImmediate calls scheduled after the
141cb0ef41Sopenharmony_ci// processing of the queue started.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_cilet threw = false;
171cb0ef41Sopenharmony_cilet stage = -1;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst QUEUE = 10;
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst errObj = {
221cb0ef41Sopenharmony_ci  name: 'Error',
231cb0ef41Sopenharmony_ci  message: 'setImmediate Err'
241cb0ef41Sopenharmony_ci};
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ciprocess.once('uncaughtException', common.mustCall((err, errorOrigin) => {
271cb0ef41Sopenharmony_ci  assert.strictEqual(errorOrigin, 'uncaughtException');
281cb0ef41Sopenharmony_ci  assert.strictEqual(stage, 0);
291cb0ef41Sopenharmony_ci  common.expectsError(errObj)(err);
301cb0ef41Sopenharmony_ci}));
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ciconst d1 = domain.create();
331cb0ef41Sopenharmony_cid1.once('error', common.expectsError(errObj));
341cb0ef41Sopenharmony_cid1.once('error', () => assert.strictEqual(stage, 0));
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ciconst run = common.mustCall((callStage) => {
371cb0ef41Sopenharmony_ci  assert(callStage >= stage);
381cb0ef41Sopenharmony_ci  stage = callStage;
391cb0ef41Sopenharmony_ci  if (threw)
401cb0ef41Sopenharmony_ci    return;
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  setImmediate(run, 2);
431cb0ef41Sopenharmony_ci}, QUEUE * 3);
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cifor (let i = 0; i < QUEUE; i++)
461cb0ef41Sopenharmony_ci  setImmediate(run, 0);
471cb0ef41Sopenharmony_cisetImmediate(() => {
481cb0ef41Sopenharmony_ci  threw = true;
491cb0ef41Sopenharmony_ci  process.nextTick(() => assert.strictEqual(stage, 1));
501cb0ef41Sopenharmony_ci  throw new Error('setImmediate Err');
511cb0ef41Sopenharmony_ci});
521cb0ef41Sopenharmony_cid1.run(() => setImmediate(() => {
531cb0ef41Sopenharmony_ci  throw new Error('setImmediate Err');
541cb0ef41Sopenharmony_ci}));
551cb0ef41Sopenharmony_cifor (let i = 0; i < QUEUE; i++)
561cb0ef41Sopenharmony_ci  setImmediate(run, 1);
57