11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst common = require('../common');
41cb0ef41Sopenharmony_ciconst tmpdir = require('../common/tmpdir');
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks');
71cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks');
81cb0ef41Sopenharmony_ciconst fs = require('fs');
91cb0ef41Sopenharmony_ciconst path = require('path');
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciif (!common.isMainThread)
121cb0ef41Sopenharmony_ci  common.skip('Worker bootstrapping works differently -> different async IDs');
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_citmpdir.refresh();
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst file1 = path.join(tmpdir.path, 'file1');
171cb0ef41Sopenharmony_ciconst file2 = path.join(tmpdir.path, 'file2');
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst onchangex = (x) => (curr, prev) => {
201cb0ef41Sopenharmony_ci  console.log(`Watcher: ${x}`);
211cb0ef41Sopenharmony_ci  console.log('current stat data:', curr);
221cb0ef41Sopenharmony_ci  console.log('previous stat data:', prev);
231cb0ef41Sopenharmony_ci};
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst checkWatcherStart = (name, watcher) => {
261cb0ef41Sopenharmony_ci  assert.strictEqual(watcher.type, 'STATWATCHER');
271cb0ef41Sopenharmony_ci  assert.strictEqual(typeof watcher.uid, 'number');
281cb0ef41Sopenharmony_ci  assert.strictEqual(watcher.triggerAsyncId, 1);
291cb0ef41Sopenharmony_ci  checkInvocations(watcher, { init: 1 },
301cb0ef41Sopenharmony_ci                   `${name}: when started to watch file`);
311cb0ef41Sopenharmony_ci};
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ciconst hooks = initHooks();
341cb0ef41Sopenharmony_cihooks.enable();
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci// Install first file watcher.
371cb0ef41Sopenharmony_ciconst w1 = fs.watchFile(file1, { interval: 10 }, onchangex('w1'));
381cb0ef41Sopenharmony_cilet as = hooks.activitiesOfTypes('STATWATCHER');
391cb0ef41Sopenharmony_ciassert.strictEqual(as.length, 1);
401cb0ef41Sopenharmony_ci// Count all change events to account for all of them in checkInvocations.
411cb0ef41Sopenharmony_cilet w1HookCount = 0;
421cb0ef41Sopenharmony_ciw1.on('change', () => w1HookCount++);
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciconst statwatcher1 = as[0];
451cb0ef41Sopenharmony_cicheckWatcherStart('watcher1', statwatcher1);
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci// Install second file watcher
481cb0ef41Sopenharmony_ciconst w2 = fs.watchFile(file2, { interval: 10 }, onchangex('w2'));
491cb0ef41Sopenharmony_cias = hooks.activitiesOfTypes('STATWATCHER');
501cb0ef41Sopenharmony_ciassert.strictEqual(as.length, 2);
511cb0ef41Sopenharmony_ci// Count all change events to account for all of them in checkInvocations.
521cb0ef41Sopenharmony_cilet w2HookCount = 0;
531cb0ef41Sopenharmony_ciw2.on('change', () => w2HookCount++);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ciconst statwatcher2 = as[1];
561cb0ef41Sopenharmony_cicheckInvocations(statwatcher1, { init: 1 },
571cb0ef41Sopenharmony_ci                 'watcher1: when started to watch second file');
581cb0ef41Sopenharmony_cicheckWatcherStart('watcher2', statwatcher2);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_cisetTimeout(() => fs.writeFileSync(file1, 'foo++'),
611cb0ef41Sopenharmony_ci           common.platformTimeout(100));
621cb0ef41Sopenharmony_ciw1.on('change', common.mustCallAtLeast((curr, prev) => {
631cb0ef41Sopenharmony_ci  console.log('w1 change to', curr, 'from', prev);
641cb0ef41Sopenharmony_ci  // Wait until we get the write above.
651cb0ef41Sopenharmony_ci  if (prev.size !== 0 || curr.size !== 5)
661cb0ef41Sopenharmony_ci    return;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  setImmediate(() => {
691cb0ef41Sopenharmony_ci    checkInvocations(statwatcher1,
701cb0ef41Sopenharmony_ci                     { init: 1, before: w1HookCount, after: w1HookCount },
711cb0ef41Sopenharmony_ci                     'watcher1: when unwatched first file');
721cb0ef41Sopenharmony_ci    checkInvocations(statwatcher2, { init: 1 },
731cb0ef41Sopenharmony_ci                     'watcher2: when unwatched first file');
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci    setTimeout(() => fs.writeFileSync(file2, 'bar++'),
761cb0ef41Sopenharmony_ci               common.platformTimeout(100));
771cb0ef41Sopenharmony_ci    w2.on('change', common.mustCallAtLeast((curr, prev) => {
781cb0ef41Sopenharmony_ci      console.log('w2 change to', curr, 'from', prev);
791cb0ef41Sopenharmony_ci      // Wait until we get the write above.
801cb0ef41Sopenharmony_ci      if (prev.size !== 0 || curr.size !== 5)
811cb0ef41Sopenharmony_ci        return;
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci      setImmediate(() => {
841cb0ef41Sopenharmony_ci        checkInvocations(statwatcher1,
851cb0ef41Sopenharmony_ci                         { init: 1, before: w1HookCount, after: w1HookCount },
861cb0ef41Sopenharmony_ci                         'watcher1: when unwatched second file');
871cb0ef41Sopenharmony_ci        checkInvocations(statwatcher2,
881cb0ef41Sopenharmony_ci                         { init: 1, before: w2HookCount, after: w2HookCount },
891cb0ef41Sopenharmony_ci                         'watcher2: when unwatched second file');
901cb0ef41Sopenharmony_ci        fs.unwatchFile(file1);
911cb0ef41Sopenharmony_ci        fs.unwatchFile(file2);
921cb0ef41Sopenharmony_ci      });
931cb0ef41Sopenharmony_ci    }));
941cb0ef41Sopenharmony_ci  });
951cb0ef41Sopenharmony_ci}));
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ciprocess.once('exit', () => {
981cb0ef41Sopenharmony_ci  hooks.disable();
991cb0ef41Sopenharmony_ci  hooks.sanityCheck('STATWATCHER');
1001cb0ef41Sopenharmony_ci  checkInvocations(statwatcher1,
1011cb0ef41Sopenharmony_ci                   { init: 1, before: w1HookCount, after: w1HookCount },
1021cb0ef41Sopenharmony_ci                   'watcher1: when process exits');
1031cb0ef41Sopenharmony_ci  checkInvocations(statwatcher2,
1041cb0ef41Sopenharmony_ci                   { init: 1, before: w2HookCount, after: w2HookCount },
1051cb0ef41Sopenharmony_ci                   'watcher2: when process exits');
1061cb0ef41Sopenharmony_ci});
107