Server-run engine: every call answers, and a crashing load gets its own worker
A load whose networks did not all check out could hang its session for good, a worker could spin on a job it could never run, a session whose load crashed its worker never got a worker of its own, and an unload followed at once by a load (a language change) lost the session's place or never answered. Fixed: - Worker: a load call is checked whole before it touches anything. One that names no network, or one twice, is refused (ERR) and every realm stays as it was. A network without a save now answers err (broken) like one whose save does not load, and the others load. A network marked loading with no load under way answers NO_REALM instead of waiting for ever. - Worker: it goes on only while something ran. If a job were ever still waiting when nothing could run, it is answered (ERR, logged) rather than left pending, and the worker no longer busy-loops on it. - Engine: a session is on its worker from the moment its load is sent. A worker exit counts against the sessions whose load was under way (else against every session it held), so a save that crashes its worker gets a worker of its own after two exits and is given up (GAVE_UP) after a third, while the sessions that only shared the worker lose their realms but not their place in the pool. Loads under way count for placement. - Engine: an unload no longer drops the placement of a session whose next load is under way, so an unload and a load sent back to back stay on one worker; a worker of its own is retired only when no call is pending on it (a pending call there used to never settle). - Engine: a message that cannot be sent rejects (ERR) and leaves nothing pending. - Realm: a what-if puts UI.hotMap back too (teSlop's trial left its own). - Realm: a network in a language this server has no dictionary for plays in English, as the page does, instead of loading broken. Tests (tests/mp-sim-engine.test.js): a malformed load changes nothing and every call of the session answers within 3 s, with the workers idle after; an unload and a load back to back keep the session on its worker; a session whose load takes its worker down twice gets a worker of its own, then GAVE_UP, while the bystander is never blamed; an ask leaves UI.hotMap as it was; a network in an unknown language loads in English. Each fails on the engine before this change (the worker's alone: the hang; the engine's alone: the placement and the exits; the realm's alone: teSlop's hotMap and the language). The two drafts no test played are played now (tests/mp-sim-acts.test.js): a premium border crossing into Hong Kong (buildLink with cnPrem) and the AI's own script deployed as it wrote it while it drops most of the traffic (teDeploy with slop, on a small network with one capped transit port), each compared byte for byte with the page's clicks, and both read in the DRAFTS check (nothing is left unplayed). Runs, each file alone under an 8G cap: mp-sim-engine 16/16 (11.6 s), mp-sim-contract 8/8, mp-sim-acts 5 + 1 todo (10.9 s), mp-sim-determinism 6/6 (53.4 s).
This commit is contained in:
parent
c5c7ea9508
commit
72f31e809b
6 changed files with 204 additions and 45 deletions
|
|
@ -3,7 +3,8 @@
|
|||
// contract's shapes (tests/lib/mp-sim-shape.js); the capture table turns each kind of effect into an fx; failures go back
|
||||
// to the mark (a throw in place, a timeout into a fresh realm, an exiting worker rejects everything pending); asks put
|
||||
// everything back; actions report only the UI they changed; a session's day holds back loads; unwatched networks stay
|
||||
// bounded; and realms do not share their intrinsics.
|
||||
// bounded; realms do not share their intrinsics; every call is answered, whatever a load holds; and a session whose load
|
||||
// takes its worker down gets a worker of its own, then is given up, while the others keep their places.
|
||||
const test = require('node:test');
|
||||
const assert = require('node:assert/strict');
|
||||
const { createEngine, snapWith } = require('../scripts/lib/mp-sim-engine');
|
||||
|
|
@ -18,6 +19,9 @@ const on = (X) => ({
|
|||
});
|
||||
const { day, ev } = on(E);
|
||||
const updOf = (r) => JSON.parse(r.upd);
|
||||
const wait = (ms) => new Promise((ok) => setTimeout(ok, ms));
|
||||
// A call's answer within ms, or a failure that says it never came (a hung call would hang the test instead).
|
||||
const within = (p, ms = 3000) => { let t; return Promise.race([p, new Promise((ok, no) => { t = setTimeout(() => no(new Error(`no answer within ${ms} ms`)), ms); })]).finally(() => clearTimeout(t)); };
|
||||
const HOST = { clock: fixture('clock'), roster: fixture('roster'), since: { days: 0, again: 0 } };
|
||||
|
||||
test('a new network: begin, a snapshot, its welcome, actions, days, a what-if, views, prefs, a save and a card', async () => {
|
||||
|
|
@ -166,8 +170,8 @@ test('an ask leaves S (the same object), R, the dice and uidN as they were, also
|
|||
checked((await E.load(code, [{ nid, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'en', prefs: {} }]))[0]);
|
||||
await ev(code, nid, "const a=ddosAttack(true);a.until=S.day+5;S.te={on:true,day:S.day,script:''};compute();return 1;");
|
||||
checked(await E.pull(code, nid, { snap: true })); checked(await E.pull(code, nid, {}));
|
||||
const before = "window.__S0=S;window.__R0=R;window.__rng=S.rng;window.__u=uidN;window.__j=JSON.stringify(S);return 1;";
|
||||
const same = 'return [S===__S0,R===__R0,S.rng===__rng,uidN===__u,JSON.stringify(S)===__j,R.ddos.some(m=>m.attack)&&R.ddos.every(m=>!m.attack||m.attack===ddosFind(m.attack.id))]';
|
||||
const before = "window.__S0=S;window.__R0=R;window.__rng=S.rng;window.__u=uidN;window.__j=JSON.stringify(S);window.__hot=UI.hotMap;return 1;";
|
||||
const same = 'return [S===__S0,R===__R0,S.rng===__rng,uidN===__u,JSON.stringify(S)===__j,R.ddos.some(m=>m.attack)&&R.ddos.every(m=>!m.attack||m.attack===ddosFind(m.attack.id)),UI.hotMap===__hot]';
|
||||
const lid = await ev(code, nid, 'return S.links.find(l=>!linkDown(l)).id');
|
||||
const script = await ev(code, nid, 'return TE_EXAMPLE');
|
||||
for (const [act, args] of [['teSlop', {}], ['teCheck', { script }], ['linkCheck', { id: lid }], ['teSlop', {}]]) {
|
||||
|
|
@ -176,7 +180,7 @@ test('an ask leaves S (the same object), R, the dice and uidN as they were, also
|
|||
assert.ok(r.modal && r.modal.h.length > 20, `${act} answers with its window`);
|
||||
if (act !== 'linkCheck') assert.equal(typeof r.ui.teDraft, 'string', `${act}: the draft`);
|
||||
if (act === 'teSlop') assert.equal(r.ui.slop, r.ui.teDraft, 'the AI\'s script is the draft');
|
||||
assert.deepEqual(await ev(code, nid, same), [true, true, true, true, true, true], act);
|
||||
assert.deepEqual(await ev(code, nid, same), [true, true, true, true, true, true, true], act);
|
||||
}
|
||||
assert.equal(checked(await E.pull(code, nid, {})).upd, undefined, 'and nothing to send');
|
||||
await E.unload(code);
|
||||
|
|
@ -268,11 +272,12 @@ test('alone, an auto-pause stops the realm\'s clock as the page\'s does: a secon
|
|||
});
|
||||
|
||||
test('a network whose save does not load is broken; the others of the session load', async () => {
|
||||
const code = 'BR0K3N', ok = 'okok0001', bad = 'badd0002';
|
||||
const L = await E.load(code, [{ nid: ok, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'de', prefs: {} }, { nid: bad, env: { s: '{"v":1,"pops":', uidN: 1 }, lang: 'en', prefs: {} }]);
|
||||
const code = 'BR0K3N', ok = 'okok0001', bad = 'badd0002', zz = 'zzzz0003';
|
||||
const L = await E.load(code, [{ nid: ok, env: { s: bigScenic(), uidN: 1000, rt: null }, lang: 'de', prefs: {} }, { nid: bad, env: { s: '{"v":1,"pops":', uidN: 1 }, lang: 'en', prefs: {} }, { ...begin(zz), lang: 'zz' }]);
|
||||
L.forEach(checked);
|
||||
assert.deepEqual([L[0].err, typeof L[1].err], [undefined, 'string']);
|
||||
assert.deepEqual([L[0].err, typeof L[1].err, L[2].err], [undefined, 'string', undefined]);
|
||||
assert.deepEqual(Object.keys(L[1]).sort(), ['err', 'nid', 't']);
|
||||
assert.equal(await ev(code, zz, 'return LANG'), 'en', 'a language without a dictionary here plays in English');
|
||||
// The network's language is the realm's: the log speaks German.
|
||||
checked(await E.act(code, ok, msg('buildPop', { city: 'dus' })));
|
||||
assert.match(await ev(code, ok, 'return LANG+":"+S.log[0].t'), /^de:/);
|
||||
|
|
@ -280,6 +285,90 @@ test('a network whose save does not load is broken; the others of the session lo
|
|||
await E.unload(code);
|
||||
});
|
||||
|
||||
test('a load call with a malformed network changes nothing, one without a save is broken, and every call still answers', async () => {
|
||||
const code = 'MA1F0R', a = 'mala0001', b = 'malb0002';
|
||||
const [A] = await E.load(code, [begin(a)]);
|
||||
checked(A);
|
||||
const env = { s: A.saved.txt, uidN: A.saved.uidN, rt: A.saved.rt };
|
||||
// A network without a nid, or one twice: the call is refused whole, and a keeps the realm it had.
|
||||
await assert.rejects(within(E.load(code, [{ nid: a, env, lang: 'en', prefs: {} }, { env, lang: 'en', prefs: {} }])), (e) => e.code === 'ERR');
|
||||
assert.equal(checked(await within(E.pull(code, a, {}))).epoch, A.epoch, 'a as it was');
|
||||
await assert.rejects(within(E.load(code, [begin(b), begin(b)])), (e) => e.code === 'ERR' && /twice/.test(e.message));
|
||||
assert.equal(checked(await within(E.act(code, a, msg('launchBB', { city: 'ams' })))).epoch, A.epoch);
|
||||
// a again with b, whose checkpoint the host could not read: b is broken, a loads (a new epoch).
|
||||
const L = await within(E.load(code, [{ nid: a, env, lang: 'en', prefs: {} }, { nid: b, env: null, lang: 'en', prefs: {} }]));
|
||||
L.forEach(checked);
|
||||
assert.deepEqual([L[0].err, L[1].err], [undefined, 'no save to load']);
|
||||
assert.notEqual(L[0].epoch, A.epoch);
|
||||
// Every call of the session answers at once: a plays, b has no realm, unloads and loads go through.
|
||||
assert.equal(checked(await within(E.act(code, a, msg('launchBB', { city: 'fra' })))).outcome, 'applied');
|
||||
assert.equal(checked(await within(E.day(code, { d: 1, nets: [a, b] }))).nets[a].day, 1);
|
||||
await assert.rejects(within(E.pull(code, b, {})), (e) => e.code === 'NO_REALM');
|
||||
checked(await within(E.unload(code, a)));
|
||||
checked((await within(E.load(code, [begin(a)])))[0]);
|
||||
checked(await within(E.unload(code)));
|
||||
// Nothing is left waiting, so the workers are idle (a job that can never run once kept one at 100% of a core).
|
||||
await wait(100);
|
||||
const c0 = process.cpuUsage();
|
||||
await wait(1000);
|
||||
const c = process.cpuUsage(c0), ms = (c.user + c.system) / 1000;
|
||||
assert.ok(ms < 400, `${ms.toFixed(0)} ms of CPU over 1 s of quiet`);
|
||||
});
|
||||
|
||||
test('an unload and a load sent back to back (a language change) keep the session on its worker', async () => {
|
||||
const code = 'GERM4N', nid = 'lang0001';
|
||||
checked((await E.load(code, [begin(nid)]))[0]);
|
||||
const w = E.workerOf(code);
|
||||
const u = E.unload(code), l = E.load(code, [{ ...begin(nid), lang: 'de' }]);
|
||||
checked(await within(u));
|
||||
checked((await within(l))[0]);
|
||||
assert.equal(E.workerOf(code), w);
|
||||
assert.equal(checked(await within(E.pull(code, nid, {}))).nid, nid, 'the network answers where it is');
|
||||
checked((await E.load(code, [begin('lang0002')]))[0]);
|
||||
assert.equal(E.workerOf(code), w, 'and the session\'s next network goes there too');
|
||||
checked(await within(E.pull(code, nid, {})));
|
||||
await E.unload(code);
|
||||
});
|
||||
|
||||
test('a session whose load takes its worker down: twice, a worker of its own; once more, given up; the others keep their places', async () => {
|
||||
const W = createEngine({ workers: 2, test: TEST, timeouts: ROOMY });
|
||||
try {
|
||||
const code = 'P01S0N', nid = 'pois0001', by = { BYST01: 'byst0001', BYST02: 'byst0002' };
|
||||
for (const [c, n] of Object.entries(by)) checked((await W.load(c, [begin(n)]))[0]);
|
||||
assert.deepEqual([W.workerOf('BYST01'), W.workerOf('BYST02')], [0, 1]);
|
||||
// Its load crashes the worker (as a save that overflows the heap would): the worker it shares with BYST01.
|
||||
const lost = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const p = W.load(code, [begin(nid)]);
|
||||
assert.deepEqual([W.workerOf(code), W.dedicated(code)], [0, false], `attempt ${i + 1}: in the pool`);
|
||||
W.killOf(code);
|
||||
await assert.rejects(p, (e) => e.code === 'EXIT');
|
||||
for (const [c, n] of Object.entries(by)) {
|
||||
if (!(await W.pull(c, n, {}).then(() => false, (e) => e.code === 'NO_REALM'))) continue;
|
||||
lost.push(c);
|
||||
checked((await W.load(c, [begin(n)]))[0]);
|
||||
assert.equal(W.dedicated(c), false, `${c} is not blamed: it goes back to the pool`);
|
||||
}
|
||||
}
|
||||
assert.deepEqual(lost, ['BYST01', 'BYST01'], 'the bystander on that worker lost its realm each time, the other never');
|
||||
// Twice within the hour: a worker of its own. An unload and a load back to back stay there, and answer.
|
||||
checked((await W.load(code, [begin(nid)]))[0]);
|
||||
assert.deepEqual([W.dedicated(code), W.stats().workers], [true, 3]);
|
||||
const u = W.unload(code), l = W.load(code, [begin(nid)]);
|
||||
checked(await within(u));
|
||||
checked((await within(l))[0]);
|
||||
checked(await within(W.pull(code, nid, {})));
|
||||
assert.equal(W.dedicated(code), true);
|
||||
// Once more, on its own worker: given up until a restart; its worker is not replaced, and the others play on.
|
||||
const p = W.load(code, [begin('pois0002')]);
|
||||
W.killOf(code);
|
||||
await assert.rejects(p, (e) => e.code === 'EXIT');
|
||||
await assert.rejects(W.load(code, [begin(nid)]), (e) => e.code === 'GAVE_UP');
|
||||
for (const [c, n] of Object.entries(by)) checked(await within(W.pull(c, n, {})));
|
||||
assert.deepEqual([W.stats().workers, W.dedicated('BYST01')], [2, false]);
|
||||
} finally { await W.close(); }
|
||||
});
|
||||
|
||||
test('a polluted prototype stays in its own realm', async () => {
|
||||
const code = 'P0WWVT', a = 'poll0001', b = 'poll0002';
|
||||
(await E.load(code, [begin(a), begin(b)])).forEach(checked);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue