@mark1russell7/service 0.6.2 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -4
- package/build/{builder-0OlfomVU.d.ts → builder-bXJqMe2p.d.ts} +3 -2
- package/build/{core-Iy9rIXOT.d.ts → core-B1zbS4WY.d.ts} +4 -3
- package/build/{dial-DLT7bTlA.d.ts → dial-CSGKVlZe.d.ts} +2 -2
- package/build/grpc.d.ts +3 -3
- package/build/grpc.js +1 -1
- package/build/host.d.ts +556 -0
- package/build/host.js +1738 -0
- package/build/host.js.map +1 -0
- package/build/http-client.d.ts +3 -3
- package/build/http-client.js +1 -1
- package/build/http.d.ts +1 -1
- package/build/index.d.ts +4 -3
- package/build/index.js +5 -749
- package/build/index.js.map +1 -1
- package/build/lifecycle-Ks9Yd5CM.d.ts +387 -0
- package/build/{lifecycle-DXK880vG.d.ts → logger-DhFgh48y.d.ts} +2 -385
- package/build/{machine-Dq2OlzCG.js → machine-DKWLZOqu.js} +3 -2
- package/build/{machine-Dq2OlzCG.js.map → machine-DKWLZOqu.js.map} +1 -1
- package/build/mongodb.d.ts +3 -3
- package/build/mongodb.js +1 -1
- package/build/operation-DL5RxFds.js +25 -0
- package/build/operation-DL5RxFds.js.map +1 -0
- package/build/pg.d.ts +2 -2
- package/build/pg.js +1 -1
- package/build/ports-CZLzYK5d.d.ts +125 -0
- package/build/process-DG_im2zc.js +171 -0
- package/build/process-DG_im2zc.js.map +1 -0
- package/build/{queue-CvBNNZZJ.d.ts → queue-BjiGodqh.d.ts} +3 -2
- package/build/rabbitmq.d.ts +4 -4
- package/build/rabbitmq.js +1 -1
- package/build/redis.d.ts +2 -2
- package/build/redis.js +1 -1
- package/build/requests-BIq9Tr4P.js +586 -0
- package/build/requests-BIq9Tr4P.js.map +1 -0
- package/build/{retrier-CRda6z9G.js → retrier-Bzkme_yM.js} +2 -24
- package/build/retrier-Bzkme_yM.js.map +1 -0
- package/build/testing.d.ts +136 -4
- package/build/testing.js +485 -13
- package/build/testing.js.map +1 -1
- package/build/websocket.d.ts +3 -3
- package/build/websocket.js +1 -1
- package/package.json +7 -2
- package/build/retrier-CRda6z9G.js.map +0 -1
package/build/testing.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { realClock } from "./clock.js";
|
|
2
2
|
import { a as recoverSpan, r as failSpan } from "./timing-DGRi3XCI.js";
|
|
3
|
+
import { s as ownStream } from "./knobs-D2ClI68I.js";
|
|
4
|
+
import { t as tracker } from "./requests-BIq9Tr4P.js";
|
|
3
5
|
import { valueIn } from "@mark1russell7/quantity";
|
|
4
6
|
import { createServer } from "node:net";
|
|
7
|
+
import { mulberry32 } from "@mark1russell7/rigor-generators";
|
|
8
|
+
import "node:events";
|
|
5
9
|
//#region src/fakes.ts
|
|
6
10
|
/** This function lets the promises that are ready continue, before the clock moves again. */
|
|
7
11
|
const settle = async () => {
|
|
@@ -136,17 +140,11 @@ const fakeProcess = (options = {}) => {
|
|
|
136
140
|
}
|
|
137
141
|
};
|
|
138
142
|
};
|
|
139
|
-
/**
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
let t = state;
|
|
145
|
-
t = Math.imul(t ^ t >>> 15, t | 1);
|
|
146
|
-
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
|
147
|
-
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
148
|
-
};
|
|
149
|
-
};
|
|
143
|
+
/**
|
|
144
|
+
* This function gives a seeded random number generator (mulberry32), with numbers in [0, 1). It is
|
|
145
|
+
* `mulberry32` of @mark1russell7/rigor-generators.
|
|
146
|
+
*/
|
|
147
|
+
const seeded = (seed) => mulberry32(seed);
|
|
150
148
|
//#endregion
|
|
151
149
|
//#region src/contract.ts
|
|
152
150
|
const ms = (amount) => valueIn(amount, "ms");
|
|
@@ -327,6 +325,64 @@ const contractCases = (declare, options = {}) => {
|
|
|
327
325
|
return cases;
|
|
328
326
|
};
|
|
329
327
|
//#endregion
|
|
328
|
+
//#region src/http/fake-server.ts
|
|
329
|
+
/** This function makes a pushed source, a stream of a work for the tests of a stream. */
|
|
330
|
+
const pushedSource = () => {
|
|
331
|
+
const queue = [];
|
|
332
|
+
let wake = null;
|
|
333
|
+
let failure = null;
|
|
334
|
+
let returned = false;
|
|
335
|
+
const poke = () => {
|
|
336
|
+
wake?.();
|
|
337
|
+
wake = null;
|
|
338
|
+
};
|
|
339
|
+
return {
|
|
340
|
+
iterable: { [Symbol.asyncIterator]: () => ({
|
|
341
|
+
async next() {
|
|
342
|
+
for (;;) {
|
|
343
|
+
if (failure !== null) throw failure;
|
|
344
|
+
if (returned) return {
|
|
345
|
+
value: void 0,
|
|
346
|
+
done: true
|
|
347
|
+
};
|
|
348
|
+
const step = queue.shift();
|
|
349
|
+
if (step !== void 0) return step;
|
|
350
|
+
await new Promise((resolve) => wake = resolve);
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
async return() {
|
|
354
|
+
returned = true;
|
|
355
|
+
poke();
|
|
356
|
+
return {
|
|
357
|
+
value: void 0,
|
|
358
|
+
done: true
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}) },
|
|
362
|
+
push: (value) => {
|
|
363
|
+
queue.push({
|
|
364
|
+
value,
|
|
365
|
+
done: false
|
|
366
|
+
});
|
|
367
|
+
poke();
|
|
368
|
+
},
|
|
369
|
+
finish: () => {
|
|
370
|
+
queue.push({
|
|
371
|
+
value: void 0,
|
|
372
|
+
done: true
|
|
373
|
+
});
|
|
374
|
+
poke();
|
|
375
|
+
},
|
|
376
|
+
fail: (error) => {
|
|
377
|
+
failure = error;
|
|
378
|
+
poke();
|
|
379
|
+
},
|
|
380
|
+
get returned() {
|
|
381
|
+
return returned;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
};
|
|
385
|
+
//#endregion
|
|
330
386
|
//#region src/conformance.ts
|
|
331
387
|
/**
|
|
332
388
|
* This function gives a detached lifecycle: a lifecycle with no service, for the start of a
|
|
@@ -359,6 +415,32 @@ const detachedLifecycle = () => {
|
|
|
359
415
|
keepAlive: () => true
|
|
360
416
|
};
|
|
361
417
|
};
|
|
418
|
+
/** These names are the controller and the route of each call of the stream cases. */
|
|
419
|
+
const CALLS = {
|
|
420
|
+
controller: "conformance",
|
|
421
|
+
route: "call"
|
|
422
|
+
};
|
|
423
|
+
/**
|
|
424
|
+
* This function gives a lifecycle with request tracking for the stream cases, and its tracker. The
|
|
425
|
+
* kit uses the tracker for the stages of the shutdown, as the lifecycle of a service does. Its
|
|
426
|
+
* requests are not idempotent, and it admits each request until the stop of its intake.
|
|
427
|
+
*/
|
|
428
|
+
const trackedLifecycle = (clock) => {
|
|
429
|
+
const requests = tracker([{
|
|
430
|
+
name: CALLS.controller,
|
|
431
|
+
requires: [],
|
|
432
|
+
routes: { [CALLS.route]: { idempotent: false } }
|
|
433
|
+
}], clock, () => null);
|
|
434
|
+
const life = {
|
|
435
|
+
...detachedLifecycle(),
|
|
436
|
+
request: (controller, route, work, options) => ownStream(requests.request(controller, route, work, options), life),
|
|
437
|
+
inFlight: (controller) => requests.inFlight(controller)
|
|
438
|
+
};
|
|
439
|
+
return {
|
|
440
|
+
life,
|
|
441
|
+
requests
|
|
442
|
+
};
|
|
443
|
+
};
|
|
362
444
|
const realNow = () => performance.now();
|
|
363
445
|
const turn = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
364
446
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
@@ -408,6 +490,16 @@ const track = (promise) => {
|
|
|
408
490
|
* past it. It gives the cooperative abort to the work, and it does not throw.
|
|
409
491
|
* - `close` ends the work in flight in its bound, and a second close has no effect.
|
|
410
492
|
* - The stages in their sequence, as the lifecycle does them.
|
|
493
|
+
*
|
|
494
|
+
* The option `streams` adds the cases of the streams (S6 D4). Each request is a stream, and a
|
|
495
|
+
* unary call is a stream of one item. The kit gives the transport a lifecycle with request
|
|
496
|
+
* tracking. It also does the stages of the shutdown on the tracker, as the lifecycle does them.
|
|
497
|
+
* - A unary call gives one value, and its end comes as the status of the answer.
|
|
498
|
+
* - A stream stays in flight until its end, not until its first item.
|
|
499
|
+
* - A failure after the first item ends the stream with an error line in the body, not a status.
|
|
500
|
+
* A failure before the first item gets a status.
|
|
501
|
+
* - `drain` waits for an open stream. The cooperative abort ends the stream, and its caller gets the end.
|
|
502
|
+
* - At the deadline of the drain, the cut ends an open stream, and its caller gets the end.
|
|
411
503
|
*/
|
|
412
504
|
const transportConformance = (make, options = {}) => {
|
|
413
505
|
const name = options.name ?? "transport";
|
|
@@ -463,7 +555,7 @@ const transportConformance = (make, options = {}) => {
|
|
|
463
555
|
check(inBound, `close did not end in its bound of ${closeBound} ms`);
|
|
464
556
|
check(!closed.failed, `close threw: ${String(closed.error)}`);
|
|
465
557
|
};
|
|
466
|
-
|
|
558
|
+
const cases = [
|
|
467
559
|
each("nothing is in flight after the start, and it takes new work", async (subject) => {
|
|
468
560
|
check(subject.impl.inFlight() === 0, `inFlight() gives ${subject.impl.inFlight()} after the start, not 0`);
|
|
469
561
|
check(await subject.offer(), "the transport refused new work after its start");
|
|
@@ -596,6 +688,386 @@ const transportConformance = (make, options = {}) => {
|
|
|
596
688
|
await count(subject, 0, "close left work in flight");
|
|
597
689
|
})
|
|
598
690
|
];
|
|
691
|
+
if (!(options.streams ?? false)) return cases;
|
|
692
|
+
const eachStream = (title, body) => ({
|
|
693
|
+
name: `${name}: ${title}`,
|
|
694
|
+
async run() {
|
|
695
|
+
const time = timeOf();
|
|
696
|
+
const subject = await make(time.clock);
|
|
697
|
+
const { life, requests } = trackedLifecycle(time.clock);
|
|
698
|
+
try {
|
|
699
|
+
await subject.impl.start(new AbortController().signal, life);
|
|
700
|
+
check(subject.call !== void 0, "the option streams needs a subject that gives call()");
|
|
701
|
+
await body(subject, time, requests);
|
|
702
|
+
} finally {
|
|
703
|
+
requests.stopIntake();
|
|
704
|
+
requests.cooperativeAbort();
|
|
705
|
+
try {
|
|
706
|
+
await subject.impl.close();
|
|
707
|
+
} finally {
|
|
708
|
+
await subject.dispose?.();
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
const call = (subject, given) => Promise.resolve(subject.call(given));
|
|
714
|
+
const openStream = async (subject) => {
|
|
715
|
+
const source = pushedSource();
|
|
716
|
+
let started = false;
|
|
717
|
+
let signal = null;
|
|
718
|
+
const view = await call(subject, {
|
|
719
|
+
...CALLS,
|
|
720
|
+
stream: true,
|
|
721
|
+
work: (given) => {
|
|
722
|
+
started = true;
|
|
723
|
+
signal = given;
|
|
724
|
+
return source.iterable;
|
|
725
|
+
}
|
|
726
|
+
});
|
|
727
|
+
check(await within(slack, () => started), "the work of a stream call did not start");
|
|
728
|
+
return {
|
|
729
|
+
view,
|
|
730
|
+
push: (value) => source.push(value),
|
|
731
|
+
finish: () => source.finish(),
|
|
732
|
+
fail: (error) => source.fail(error),
|
|
733
|
+
get aborted() {
|
|
734
|
+
return signal?.aborted === true;
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
};
|
|
738
|
+
const pushes = async (fed, value, what) => {
|
|
739
|
+
const before = fed.view.items.length;
|
|
740
|
+
fed.push(value);
|
|
741
|
+
check(await within(slack, () => fed.view.items.length > before), `${what}: the caller did not get the item ${value}`);
|
|
742
|
+
};
|
|
743
|
+
const ends = async (view, kind, inBand, what) => {
|
|
744
|
+
check(await within(slack, () => view.end !== null), `${what}: the caller got no end`);
|
|
745
|
+
const end = view.end;
|
|
746
|
+
const where = (body) => body ? "in the body" : "as a status";
|
|
747
|
+
check(end.kind === kind && end.inBand === inBand, `${what}: the caller got the end ${end.kind} ${where(end.inBand)}, not ${kind} ${where(inBand)}`);
|
|
748
|
+
};
|
|
749
|
+
/** This function stops the intake of the tracker and of the transport, as the stage `stop intake` does. */
|
|
750
|
+
const stopBoth = async (subject, requests) => {
|
|
751
|
+
requests.stopIntake();
|
|
752
|
+
await subject.impl.stopIntake();
|
|
753
|
+
};
|
|
754
|
+
/**
|
|
755
|
+
* This function starts the drain of the transport and of the tracker, as the stage `drain` does.
|
|
756
|
+
* The abort of `signal` is also the cooperative abort of the tracker. It tracks the drain of the transport.
|
|
757
|
+
*/
|
|
758
|
+
const drainBoth = (subject, requests, deadline, signal) => {
|
|
759
|
+
if (signal.aborted) requests.cooperativeAbort();
|
|
760
|
+
else signal.addEventListener("abort", () => requests.cooperativeAbort(), { once: true });
|
|
761
|
+
requests.drain(deadline);
|
|
762
|
+
return track(attempt(() => subject.impl.drain(deadline, signal)));
|
|
763
|
+
};
|
|
764
|
+
const failure = () => /* @__PURE__ */ new Error("the failure of the conformance kit");
|
|
765
|
+
const streamCases = [
|
|
766
|
+
eachStream("a unary call is a stream of one item: one value, and its end as the status", async (subject) => {
|
|
767
|
+
let started = false;
|
|
768
|
+
let give = () => {};
|
|
769
|
+
const view = await call(subject, {
|
|
770
|
+
...CALLS,
|
|
771
|
+
stream: false,
|
|
772
|
+
work: () => {
|
|
773
|
+
started = true;
|
|
774
|
+
return new Promise((resolve) => {
|
|
775
|
+
give = resolve;
|
|
776
|
+
});
|
|
777
|
+
}
|
|
778
|
+
});
|
|
779
|
+
check(await within(slack, () => started), "the work of a unary call did not start");
|
|
780
|
+
await count(subject, 1, "a unary call is in flight");
|
|
781
|
+
give(7);
|
|
782
|
+
await ends(view, "done", false, "a unary call");
|
|
783
|
+
check(view.items.length === 1 && view.items[0] === 7, `a unary call gave the items ${JSON.stringify(view.items)}, not [7]`);
|
|
784
|
+
await count(subject, 0, "a unary call ended");
|
|
785
|
+
}),
|
|
786
|
+
eachStream("a stream stays in flight until its end, not until its first item", async (subject) => {
|
|
787
|
+
const fed = await openStream(subject);
|
|
788
|
+
await count(subject, 1, "a stream is open");
|
|
789
|
+
for (const value of [1, 2]) {
|
|
790
|
+
await pushes(fed, value, "an open stream");
|
|
791
|
+
await count(subject, 1, `a stream is open after its item ${value}`);
|
|
792
|
+
}
|
|
793
|
+
check(fed.view.end === null, "the caller got an end before the end of the stream");
|
|
794
|
+
fed.finish();
|
|
795
|
+
await ends(fed.view, "done", true, "a stream that ended");
|
|
796
|
+
check(JSON.stringify(fed.view.items) === "[1,2]", `a stream gave the items ${JSON.stringify(fed.view.items)}, not [1,2]`);
|
|
797
|
+
await count(subject, 0, "a stream ended");
|
|
798
|
+
}),
|
|
799
|
+
eachStream("a failure after the first item ends the stream with an error line, not a status", async (subject) => {
|
|
800
|
+
const fed = await openStream(subject);
|
|
801
|
+
await pushes(fed, 1, "a stream before its failure");
|
|
802
|
+
fed.fail(failure());
|
|
803
|
+
await ends(fed.view, "failed", true, "a stream that failed after its first item");
|
|
804
|
+
await count(subject, 0, "a stream failed");
|
|
805
|
+
}),
|
|
806
|
+
eachStream("a failure before the first item gets a status, not an error line", async (subject) => {
|
|
807
|
+
const fed = await openStream(subject);
|
|
808
|
+
fed.fail(failure());
|
|
809
|
+
await ends(fed.view, "failed", false, "a stream that failed before its first item");
|
|
810
|
+
check(fed.view.items.length === 0, `a stream that failed before its first item gave ${fed.view.items.length} items`);
|
|
811
|
+
await count(subject, 0, "a stream failed");
|
|
812
|
+
}),
|
|
813
|
+
eachStream("drain waits for an open stream, and the cooperative abort ends the stream with its end", async (subject, time, requests) => {
|
|
814
|
+
const fed = await openStream(subject);
|
|
815
|
+
await pushes(fed, 1, "a stream before the drain");
|
|
816
|
+
await stopBoth(subject, requests);
|
|
817
|
+
const abort = new AbortController();
|
|
818
|
+
const deadline = time.clock.now() + cap;
|
|
819
|
+
const drained = drainBoth(subject, requests, deadline, abort.signal);
|
|
820
|
+
await time.pass(cap / 4);
|
|
821
|
+
await quiet();
|
|
822
|
+
check(!drained.settled, "drain ended while a stream was open, before its deadline");
|
|
823
|
+
check(fed.view.end === null, "the caller got an end before the cooperative abort");
|
|
824
|
+
await count(subject, 1, "a stream is open in the drain");
|
|
825
|
+
abort.abort(/* @__PURE__ */ new Error("the cooperative abort of the conformance kit"));
|
|
826
|
+
check(await within(slack, () => fed.aborted), "the work of the stream did not get the cooperative abort");
|
|
827
|
+
await ends(fed.view, "cut", true, "a stream after the cooperative abort");
|
|
828
|
+
check(await within(slack, () => drained.settled), "drain did not end when the stream ended");
|
|
829
|
+
check(!drained.failed, `drain threw: ${String(drained.error)}`);
|
|
830
|
+
check(time.clock.now() < deadline, "drain waited until its deadline after the stream ended");
|
|
831
|
+
await count(subject, 0, "the cooperative abort left a stream in flight");
|
|
832
|
+
}),
|
|
833
|
+
eachStream("drain cuts an open stream at its deadline, and the caller gets the end of the cut", async (subject, time, requests) => {
|
|
834
|
+
const fed = await openStream(subject);
|
|
835
|
+
await pushes(fed, 1, "a stream before the drain");
|
|
836
|
+
await stopBoth(subject, requests);
|
|
837
|
+
const drained = drainBoth(subject, requests, time.clock.now() + cap, new AbortController().signal);
|
|
838
|
+
await time.pass(cap);
|
|
839
|
+
await ends(fed.view, "cut", true, "a stream at the deadline of the drain");
|
|
840
|
+
check(await within(slack, () => drained.settled), "drain did not end after the cut of the stream");
|
|
841
|
+
check(!drained.failed, `drain threw at its deadline: ${String(drained.error)}`);
|
|
842
|
+
await count(subject, 0, "the cut left a stream in flight");
|
|
843
|
+
})
|
|
844
|
+
];
|
|
845
|
+
return [...cases, ...streamCases];
|
|
846
|
+
};
|
|
847
|
+
//#endregion
|
|
848
|
+
//#region src/host/fakes.ts
|
|
849
|
+
const encoder = new TextEncoder();
|
|
850
|
+
const decoder = new TextDecoder();
|
|
851
|
+
const bytesOf = (data) => typeof data === "string" ? encoder.encode(data) : data;
|
|
852
|
+
/** This function makes a fake host. Give its `ports` to the host functions in a test. */
|
|
853
|
+
const fakeHost = (options = {}) => {
|
|
854
|
+
const clock = fakeClock();
|
|
855
|
+
const wallStart = options.wallStart ?? 0xe8d4a51000;
|
|
856
|
+
const wall = () => wallStart + clock.now();
|
|
857
|
+
const self = options.self ?? {
|
|
858
|
+
pid: 100,
|
|
859
|
+
startedAt: wallStart
|
|
860
|
+
};
|
|
861
|
+
const files = new Map(Object.entries(options.files ?? {}));
|
|
862
|
+
const commands = [];
|
|
863
|
+
const signals = [];
|
|
864
|
+
const children = [];
|
|
865
|
+
const processes = /* @__PURE__ */ new Map([[self.pid, self.startedAt]]);
|
|
866
|
+
const live = /* @__PURE__ */ new Map();
|
|
867
|
+
let nextPid = 1e3;
|
|
868
|
+
const check = (operation, path) => {
|
|
869
|
+
if (host.failFiles?.(operation, path) === true) throw new Error(`the fake ${operation} of ${path} failed`);
|
|
870
|
+
};
|
|
871
|
+
const politeStop = (pid) => {
|
|
872
|
+
const child = live.get(pid);
|
|
873
|
+
if (host.polite && child !== void 0) queueMicrotask(() => child.exit(0));
|
|
874
|
+
};
|
|
875
|
+
const hardStop = (pid, signal) => {
|
|
876
|
+
const child = live.get(pid);
|
|
877
|
+
if (child !== void 0) queueMicrotask(() => child.exit(null, signal));
|
|
878
|
+
else processes.delete(pid);
|
|
879
|
+
};
|
|
880
|
+
const spawn = (request) => {
|
|
881
|
+
const pid = host.failSpawn?.(request) === true ? null : nextPid++;
|
|
882
|
+
const outputs = [];
|
|
883
|
+
const exits = [];
|
|
884
|
+
const sent = [];
|
|
885
|
+
let end = null;
|
|
886
|
+
const child = {
|
|
887
|
+
request,
|
|
888
|
+
pid,
|
|
889
|
+
ipc: request.ipc,
|
|
890
|
+
sent,
|
|
891
|
+
get ended() {
|
|
892
|
+
return end !== null;
|
|
893
|
+
},
|
|
894
|
+
onOutput(handler) {
|
|
895
|
+
outputs.push(handler);
|
|
896
|
+
},
|
|
897
|
+
onExit(handler) {
|
|
898
|
+
exits.push(handler);
|
|
899
|
+
if (end !== null) handler(end.code, end.signal);
|
|
900
|
+
},
|
|
901
|
+
send(message) {
|
|
902
|
+
if (!request.ipc || end !== null) return false;
|
|
903
|
+
sent.push(message);
|
|
904
|
+
if (pid !== null) politeStop(pid);
|
|
905
|
+
return true;
|
|
906
|
+
},
|
|
907
|
+
exit(code, signal = null) {
|
|
908
|
+
if (end !== null) return;
|
|
909
|
+
end = {
|
|
910
|
+
code,
|
|
911
|
+
signal
|
|
912
|
+
};
|
|
913
|
+
if (pid !== null) {
|
|
914
|
+
processes.delete(pid);
|
|
915
|
+
live.delete(pid);
|
|
916
|
+
}
|
|
917
|
+
for (const handler of exits) handler(code, signal);
|
|
918
|
+
},
|
|
919
|
+
output(stream, chunk) {
|
|
920
|
+
for (const handler of outputs) handler(stream, chunk);
|
|
921
|
+
}
|
|
922
|
+
};
|
|
923
|
+
children.push(child);
|
|
924
|
+
if (pid === null) queueMicrotask(() => child.exit(null));
|
|
925
|
+
else {
|
|
926
|
+
processes.set(pid, wall());
|
|
927
|
+
live.set(pid, child);
|
|
928
|
+
}
|
|
929
|
+
return child;
|
|
930
|
+
};
|
|
931
|
+
const signal = (pid, sig, group) => {
|
|
932
|
+
if (!processes.has(pid)) return false;
|
|
933
|
+
signals.push({
|
|
934
|
+
pid,
|
|
935
|
+
signal: sig,
|
|
936
|
+
group
|
|
937
|
+
});
|
|
938
|
+
if (sig === "SIGKILL") hardStop(pid, "SIGKILL");
|
|
939
|
+
else politeStop(pid);
|
|
940
|
+
return true;
|
|
941
|
+
};
|
|
942
|
+
const defaultAnswer = (command) => {
|
|
943
|
+
if (command.file !== "taskkill") return {
|
|
944
|
+
code: 0,
|
|
945
|
+
out: ""
|
|
946
|
+
};
|
|
947
|
+
const at = command.args.indexOf("/PID");
|
|
948
|
+
const pid = Number(command.args[at + 1]);
|
|
949
|
+
if (at < 0 || !processes.has(pid)) return {
|
|
950
|
+
code: 128,
|
|
951
|
+
out: `ERROR: The process "${command.args[at + 1] ?? ""}" not found.`
|
|
952
|
+
};
|
|
953
|
+
if (command.args.includes("/F")) {
|
|
954
|
+
hardStop(pid, null);
|
|
955
|
+
return {
|
|
956
|
+
code: 0,
|
|
957
|
+
out: `SUCCESS: The process with PID ${pid} has been terminated.`
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
if (host.polite && live.has(pid)) {
|
|
961
|
+
politeStop(pid);
|
|
962
|
+
return {
|
|
963
|
+
code: 0,
|
|
964
|
+
out: `SUCCESS: Sent termination signal to the process with PID ${pid}.`
|
|
965
|
+
};
|
|
966
|
+
}
|
|
967
|
+
return {
|
|
968
|
+
code: 1,
|
|
969
|
+
out: `ERROR: The process with PID ${pid} could not be terminated.`
|
|
970
|
+
};
|
|
971
|
+
};
|
|
972
|
+
const ports = {
|
|
973
|
+
platform: options.platform ?? "linux",
|
|
974
|
+
commands: { async run(file, args, commandOptions) {
|
|
975
|
+
const command = {
|
|
976
|
+
file,
|
|
977
|
+
args: [...args],
|
|
978
|
+
options: commandOptions
|
|
979
|
+
};
|
|
980
|
+
commands.push(command);
|
|
981
|
+
return options.answer?.(command) ?? defaultAnswer(command);
|
|
982
|
+
} },
|
|
983
|
+
files: {
|
|
984
|
+
async read(path) {
|
|
985
|
+
check("read", path);
|
|
986
|
+
const data = files.get(path);
|
|
987
|
+
return data === void 0 ? null : typeof data === "string" ? data : decoder.decode(data);
|
|
988
|
+
},
|
|
989
|
+
async write(path, data) {
|
|
990
|
+
check("write", path);
|
|
991
|
+
files.set(path, data);
|
|
992
|
+
},
|
|
993
|
+
async create(path, data) {
|
|
994
|
+
check("create", path);
|
|
995
|
+
if (files.has(path)) return false;
|
|
996
|
+
files.set(path, data);
|
|
997
|
+
return true;
|
|
998
|
+
},
|
|
999
|
+
async append(path, data) {
|
|
1000
|
+
check("append", path);
|
|
1001
|
+
const before = files.get(path);
|
|
1002
|
+
if (before === void 0) files.set(path, data);
|
|
1003
|
+
else if (typeof before === "string" && typeof data === "string") files.set(path, before + data);
|
|
1004
|
+
else {
|
|
1005
|
+
const head = bytesOf(before);
|
|
1006
|
+
const tail = bytesOf(data);
|
|
1007
|
+
const joined = new Uint8Array(head.length + tail.length);
|
|
1008
|
+
joined.set(head, 0);
|
|
1009
|
+
joined.set(tail, head.length);
|
|
1010
|
+
files.set(path, joined);
|
|
1011
|
+
}
|
|
1012
|
+
},
|
|
1013
|
+
async size(path) {
|
|
1014
|
+
check("size", path);
|
|
1015
|
+
const data = files.get(path);
|
|
1016
|
+
return data === void 0 ? null : bytesOf(data).length;
|
|
1017
|
+
},
|
|
1018
|
+
async rename(from, to) {
|
|
1019
|
+
check("rename", from);
|
|
1020
|
+
const data = files.get(from);
|
|
1021
|
+
if (data === void 0) throw new Error(`the fake file ${from} does not exist`);
|
|
1022
|
+
files.delete(from);
|
|
1023
|
+
files.set(to, data);
|
|
1024
|
+
},
|
|
1025
|
+
async remove(path) {
|
|
1026
|
+
check("remove", path);
|
|
1027
|
+
files.delete(path);
|
|
1028
|
+
}
|
|
1029
|
+
},
|
|
1030
|
+
processes: {
|
|
1031
|
+
alive: (pid) => processes.has(pid),
|
|
1032
|
+
startOf: async (pid) => processes.get(pid) ?? null,
|
|
1033
|
+
signal: (pid, sig) => signal(pid, sig, false),
|
|
1034
|
+
signalGroup: (pid, sig) => ports.platform !== "win32" && live.has(pid) && signal(pid, sig, true),
|
|
1035
|
+
spawn
|
|
1036
|
+
},
|
|
1037
|
+
net: {
|
|
1038
|
+
httpStatus: async (url) => host.http.get(url) ?? null,
|
|
1039
|
+
tcpConnect: async (hostName, port) => host.tcp.has(`${hostName}:${port}`)
|
|
1040
|
+
},
|
|
1041
|
+
clock,
|
|
1042
|
+
wall,
|
|
1043
|
+
self,
|
|
1044
|
+
env: options.env ?? {},
|
|
1045
|
+
homeDir: options.homeDir ?? "/home/test",
|
|
1046
|
+
user: () => options.user ?? "test",
|
|
1047
|
+
...options.logger === void 0 ? {} : { logger: options.logger }
|
|
1048
|
+
};
|
|
1049
|
+
const host = {
|
|
1050
|
+
ports,
|
|
1051
|
+
clock,
|
|
1052
|
+
files,
|
|
1053
|
+
commands,
|
|
1054
|
+
signals,
|
|
1055
|
+
children,
|
|
1056
|
+
processes,
|
|
1057
|
+
http: /* @__PURE__ */ new Map(),
|
|
1058
|
+
tcp: /* @__PURE__ */ new Set(),
|
|
1059
|
+
polite: false,
|
|
1060
|
+
failFiles: null,
|
|
1061
|
+
failSpawn: null,
|
|
1062
|
+
text(path) {
|
|
1063
|
+
const data = files.get(path);
|
|
1064
|
+
return data === void 0 ? null : typeof data === "string" ? data : decoder.decode(data);
|
|
1065
|
+
},
|
|
1066
|
+
async settle() {
|
|
1067
|
+
for (let turn = 0; turn < 2e3; turn += 1) await Promise.resolve();
|
|
1068
|
+
}
|
|
1069
|
+
};
|
|
1070
|
+
return host;
|
|
599
1071
|
};
|
|
600
1072
|
//#endregion
|
|
601
1073
|
//#region src/testing.ts
|
|
@@ -687,6 +1159,6 @@ const quietServer = async () => {
|
|
|
687
1159
|
};
|
|
688
1160
|
};
|
|
689
1161
|
//#endregion
|
|
690
|
-
export { contractCases, detachedLifecycle, fakeClock, fakeProcess, quietServer, scriptedCheck, seeded, testLogger, transportConformance };
|
|
1162
|
+
export { contractCases, detachedLifecycle, fakeClock, fakeHost, fakeProcess, quietServer, scriptedCheck, seeded, testLogger, transportConformance };
|
|
691
1163
|
|
|
692
1164
|
//# sourceMappingURL=testing.js.map
|