All files / src otp.spec.js

100% Statements 28/28
100% Branches 0/0
72.73% Functions 8/11
100% Lines 28/28
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179                                            1x 1x 1x   1x 1x 1x 1x   1x 1x   1x 1x 1x   1x 1x   1x 1x           1x 1x       1x 1x               1x 1x                               1x   1x                                                                     1x   1x                                                                   1x   1x                                                
import 'babel-polyfill';
import 'babel-core/register';
 
import test from 'ava';
 
import { delay } from 'redux-saga';
import { call, fork, race, cancel, join } from 'redux-saga/effects';
import { createMockTask } from 'redux-saga/utils';
 
import { checkGeneratorScenario as checkScenario } from './test-utils';
 
/* eslint-disable import/no-unresolved */
import { supervisor, ONE_FOR_ONE, ONE_FOR_MANY, PERMANENT, TEMPORARY } from 'build_TEMP';
import { ProcessSystem, States } from 'erlang-js'
 
 
// Data protocol examples
 
function* worker()    { yield call(delay, 1000); }
function* workerOne() { yield call(delay, 1000); }
function* workerTwo() { yield call(delay, 1000); }
 
const mockWorker         = createMockTask(),
  mockWorkerOne          = createMockTask(),
  mockWorkerTwo          = createMockTask(),
 
  permanentWorkerSpec    = { start: worker,    restart: PERMANENT, id: '0' },
  temporaryWorkerSpec    = { start: worker,    restart: TEMPORARY, id: '0' },
  firstWorkerSpec        = { start: workerOne, restart: PERMANENT, id: 'first' },
  secondWorkerSpec       = { start: workerTwo, restart: PERMANENT, id: 'second' },
 
  workerRace             = race({ '0': join(mockWorker) }),
  workersRace            = race({ 'first': join(mockWorkerOne), 'second': join(mockWorkerTwo) }),
 
  workerTerminated       = { '0': '0' },
  firstWorkerTerminated  = { 'first': 'first' },
  secondWorkerTerminated = { 'second': 'second' },
 
  waitedLongEnough       = { waitedLongEnough: true },
  restartFreqError       = { error: { message: 'Restart frequency limit reached' } },
 
  timedWorkerRace        = race({ waitedLongEnough: call(delay, 1000, true), '0': join(mockWorker) }),
  timedWorkersRace       = race({
    waitedLongEnough: call(delay, 1000, true),
    'first': join(mockWorkerOne),
    'second': join(mockWorkerTwo)
  });
 
let sys;
test.beforeEach(t => {
  sys = new ProcessSystem();
})
 
test('erl system works', t => {
  const pid = sys.spawn(function* () {
    yield 1;
  });
 
  t.is(sys.list().length, 2);
  t.is(sys.list()[1], pid)
});
 
test('supervisor: one-for-one, temporary child, simple', t => {
  const pid = sys.spawn(() => supervisor.init({
    supFlags: { strategy: ONE_FOR_ONE },
    childSpecs: [temporaryWorkerSpec]
  }));
 
  // initialization
 
  sys.send(pid, null)
  sys.receive(out => t.deepEqual(out, fork(worker)));
 
  sys.send(mockWorker);                   // spawns first (and only) worker
  sys.receive(out => t.deepEqual(out, timedWorkerRace));
  sys.receive(() => t.fail());
});
 
 
test('supervisor: one-for-one, max restart frequency', t => {
  // initialization
  const supervisorIter = supervisor.init({
    supFlags: { strategy: ONE_FOR_ONE, maxR: 1, maxT: 1 },
    childSpecs: [permanentWorkerSpec]
  });
 
  checkScenario(t, supervisorIter, [
    null,
 
    fork(worker), mockWorker,       // spawns first (and only) worker
 
                                    // main loop:
 
    race({                          // yields worker and delay race
      0: join(mockWorker),
      waitedLongEnough: call(delay, 1, true)
    }),
 
    waitedLongEnough,               // if waited long enough,
    workerRace,                     // workers race is yielded
 
    workerTerminated,               // when worker terminates,
    fork(worker), mockWorker,       // it gets restarted
 
 
    race({                          // yields worker and delay race
      0: join(mockWorker),
      waitedLongEnough: call(delay, 1, true)
    }),
    workerTerminated,               // if worker terminates more frequently than permitted,
    restartFreqError                // supervisor dies
 
  ]);
});
 
 
test('supervisor: one-for-one, multi', t => {
  // initialization
  const supervisorIter = supervisor.init({
    supFlags: { strategy: ONE_FOR_ONE },
    childSpecs: [firstWorkerSpec, secondWorkerSpec]
  });
 
  checkScenario(t, supervisorIter, [
    null,
 
    fork(workerOne), mockWorkerOne, // spawns first worker
    fork(workerTwo), mockWorkerTwo, // spawns second worker
 
                                    // main loop:
 
    timedWorkersRace,               // yields workers and delay race
    waitedLongEnough,               // if delay does trigger,
    workersRace,                    // yields workers race
 
    firstWorkerTerminated,          // when first worker terminates,
    fork(workerOne), mockWorkerOne, // it gets restarted
 
    timedWorkersRace,               // the same with worker 1
    waitedLongEnough,
    workersRace,
 
    secondWorkerTerminated,
    fork(workerTwo), mockWorkerTwo,
 
    timedWorkersRace,
    firstWorkerTerminated,          // if any worker terminates,
    restartFreqError                // supervisor dies
  ]);
});
 
 
test('supervisor: all-for-one, multi', t => {
  // initialization
  const supervisorIter = supervisor.init({
    supFlags: { strategy: ONE_FOR_MANY },
    childSpecs: [firstWorkerSpec, secondWorkerSpec]
  });
 
  checkScenario(t, supervisorIter, [
    null,
 
    fork(workerOne), mockWorkerOne, // initialization:
    fork(workerTwo), mockWorkerTwo, // spawns workers
 
                                    // main loop:
    timedWorkersRace,               // listens for workers termination
    waitedLongEnough,
    workersRace,
 
    firstWorkerTerminated,          // when first worker terminates:
    fork(workerOne), mockWorkerOne, // it gets restarted,
 
    cancel(mockWorkerTwo), null,    // and second gets restarted too
    fork(workerTwo), mockWorkerTwo,
  ]);
});