@cratis/chronicle 6.61.0 → 6.62.0
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/connection/ChronicleConnection.ts +7 -1
- package/connection/ChronicleServices.ts +2 -0
- package/dist/connection/ChronicleConnection.d.ts +1 -0
- package/dist/connection/ChronicleConnection.d.ts.map +1 -1
- package/dist/connection/ChronicleConnection.js +6 -2
- package/dist/connection/ChronicleConnection.js.map +1 -1
- package/dist/connection/ChronicleServices.d.ts +2 -1
- package/dist/connection/ChronicleServices.d.ts.map +1 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts +21 -1
- package/dist/eventSequences/EventForEventSourceId.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.d.ts +9 -0
- package/dist/eventSequences/EventSequence.d.ts.map +1 -1
- package/dist/eventSequences/EventSequence.js +18 -0
- package/dist/eventSequences/EventSequence.js.map +1 -1
- package/dist/eventSequences/IEventSequence.d.ts +30 -0
- package/dist/eventSequences/IEventSequence.d.ts.map +1 -1
- package/dist/imports/EventImport.d.ts +29 -0
- package/dist/imports/EventImport.d.ts.map +1 -0
- package/dist/imports/EventImport.js +248 -0
- package/dist/imports/EventImport.js.map +1 -0
- package/dist/imports/EventImportSettings.d.ts +32 -0
- package/dist/imports/EventImportSettings.d.ts.map +1 -0
- package/dist/imports/EventImportSettings.js +4 -0
- package/dist/imports/EventImportSettings.js.map +1 -0
- package/dist/imports/IEventImport.d.ts +50 -0
- package/dist/imports/IEventImport.d.ts.map +1 -0
- package/dist/imports/IEventImport.js +4 -0
- package/dist/imports/IEventImport.js.map +1 -0
- package/dist/imports/ImportChunkRefused.d.ts +12 -0
- package/dist/imports/ImportChunkRefused.d.ts.map +1 -0
- package/dist/imports/ImportChunkRefused.js +19 -0
- package/dist/imports/ImportChunkRefused.js.map +1 -0
- package/dist/imports/ImportCouldNotBegin.d.ts +10 -0
- package/dist/imports/ImportCouldNotBegin.d.ts.map +1 -0
- package/dist/imports/ImportCouldNotBegin.js +16 -0
- package/dist/imports/ImportCouldNotBegin.js.map +1 -0
- package/dist/imports/ImportOptions.d.ts +26 -0
- package/dist/imports/ImportOptions.d.ts.map +1 -0
- package/dist/imports/ImportOptions.js +4 -0
- package/dist/imports/ImportOptions.js.map +1 -0
- package/dist/imports/ImportProgress.d.ts +20 -0
- package/dist/imports/ImportProgress.d.ts.map +1 -0
- package/dist/imports/ImportProgress.js +4 -0
- package/dist/imports/ImportProgress.js.map +1 -0
- package/dist/imports/ImportRejection.d.ts +10 -0
- package/dist/imports/ImportRejection.d.ts.map +1 -0
- package/dist/imports/ImportRejection.js +4 -0
- package/dist/imports/ImportRejection.js.map +1 -0
- package/dist/imports/ImportResult.d.ts +30 -0
- package/dist/imports/ImportResult.d.ts.map +1 -0
- package/dist/imports/ImportResult.js +4 -0
- package/dist/imports/ImportResult.js.map +1 -0
- package/dist/imports/ImportState.d.ts +18 -0
- package/dist/imports/ImportState.d.ts.map +1 -0
- package/dist/imports/ImportState.js +21 -0
- package/dist/imports/ImportState.js.map +1 -0
- package/dist/imports/ImportTarget.d.ts +16 -0
- package/dist/imports/ImportTarget.d.ts.map +1 -0
- package/dist/imports/ImportTarget.js +4 -0
- package/dist/imports/ImportTarget.js.map +1 -0
- package/dist/imports/ImportWasAbandoned.d.ts +11 -0
- package/dist/imports/ImportWasAbandoned.d.ts.map +1 -0
- package/dist/imports/ImportWasAbandoned.js +16 -0
- package/dist/imports/ImportWasAbandoned.js.map +1 -0
- package/dist/imports/ImportsNotSupported.d.ts +5 -0
- package/dist/imports/ImportsNotSupported.d.ts.map +1 -0
- package/dist/imports/ImportsNotSupported.js +10 -0
- package/dist/imports/ImportsNotSupported.js.map +1 -0
- package/dist/imports/OnInvalidEvent.d.ts +8 -0
- package/dist/imports/OnInvalidEvent.d.ts.map +1 -0
- package/dist/imports/OnInvalidEvent.js +11 -0
- package/dist/imports/OnInvalidEvent.js.map +1 -0
- package/dist/imports/abortable.d.ts +23 -0
- package/dist/imports/abortable.d.ts.map +1 -0
- package/dist/imports/abortable.js +60 -0
- package/dist/imports/abortable.js.map +1 -0
- package/dist/imports/beginEventImport.d.ts +16 -0
- package/dist/imports/beginEventImport.d.ts.map +1 -0
- package/dist/imports/beginEventImport.js +74 -0
- package/dist/imports/beginEventImport.js.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts +144 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.d.ts.map +1 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js +132 -0
- package/dist/imports/for_EventImport/given/a_kernel_import_session.js.map +1 -0
- package/dist/imports/importEvents.d.ts +13 -0
- package/dist/imports/importEvents.d.ts.map +1 -0
- package/dist/imports/importEvents.js +35 -0
- package/dist/imports/importEvents.js.map +1 -0
- package/dist/imports/index.d.ts +12 -0
- package/dist/imports/index.d.ts.map +1 -0
- package/dist/imports/index.js +9 -0
- package/dist/imports/index.js.map +1 -0
- package/dist/imports/isTransientImportFailure.d.ts +7 -0
- package/dist/imports/isTransientImportFailure.d.ts.map +1 -0
- package/dist/imports/isTransientImportFailure.js +13 -0
- package/dist/imports/isTransientImportFailure.js.map +1 -0
- package/dist/imports/sizeOfImportEvent.d.ts +9 -0
- package/dist/imports/sizeOfImportEvent.d.ts.map +1 -0
- package/dist/imports/sizeOfImportEvent.js +25 -0
- package/dist/imports/sizeOfImportEvent.js.map +1 -0
- package/dist/imports/toImportEvent.d.ts +14 -0
- package/dist/imports/toImportEvent.d.ts.map +1 -0
- package/dist/imports/toImportEvent.js +54 -0
- package/dist/imports/toImportEvent.js.map +1 -0
- package/dist/imports/toImportProgress.d.ts +9 -0
- package/dist/imports/toImportProgress.d.ts.map +1 -0
- package/dist/imports/toImportProgress.js +20 -0
- package/dist/imports/toImportProgress.js.map +1 -0
- package/dist/imports/toImportResult.d.ts +9 -0
- package/dist/imports/toImportResult.d.ts.map +1 -0
- package/dist/imports/toImportResult.js +33 -0
- package/dist/imports/toImportResult.js.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/observation/index.d.ts +2 -0
- package/dist/observation/index.d.ts.map +1 -1
- package/dist/observation/index.js +1 -0
- package/dist/observation/index.js.map +1 -1
- package/dist/testing/BatchPartitionOutcome.d.ts +7 -0
- package/dist/testing/BatchPartitionOutcome.d.ts.map +1 -0
- package/dist/testing/BatchPartitionOutcome.js +4 -0
- package/dist/testing/BatchPartitionOutcome.js.map +1 -0
- package/dist/testing/InProcessEventSequence.d.ts +4 -0
- package/dist/testing/InProcessEventSequence.d.ts.map +1 -1
- package/dist/testing/InProcessEventSequence.js +2 -0
- package/dist/testing/InProcessEventSequence.js.map +1 -1
- package/dist/testing/ReactorDeliveryResult.d.ts +2 -0
- package/dist/testing/ReactorDeliveryResult.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.d.ts +46 -10
- package/dist/testing/ReactorScenario.d.ts.map +1 -1
- package/dist/testing/ReactorScenario.js +120 -29
- package/dist/testing/ReactorScenario.js.map +1 -1
- package/dist/testing/deliverToBatchMethods.d.ts +35 -0
- package/dist/testing/deliverToBatchMethods.d.ts.map +1 -0
- package/dist/testing/deliverToBatchMethods.js +55 -0
- package/dist/testing/deliverToBatchMethods.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/eventSequences/EventForEventSourceId.ts +25 -1
- package/eventSequences/EventSequence.ts +28 -0
- package/eventSequences/IEventSequence.ts +33 -0
- package/imports/EventImport.ts +263 -0
- package/imports/EventImportSettings.ts +46 -0
- package/imports/IEventImport.ts +58 -0
- package/imports/ImportChunkRefused.ts +15 -0
- package/imports/ImportCouldNotBegin.ts +14 -0
- package/imports/ImportOptions.ts +36 -0
- package/imports/ImportProgress.ts +29 -0
- package/imports/ImportRejection.ts +14 -0
- package/imports/ImportResult.ts +43 -0
- package/imports/ImportState.ts +26 -0
- package/imports/ImportTarget.ts +23 -0
- package/imports/ImportWasAbandoned.ts +16 -0
- package/imports/ImportsNotSupported.ts +10 -0
- package/imports/OnInvalidEvent.ts +11 -0
- package/imports/abortable.ts +57 -0
- package/imports/beginEventImport.ts +85 -0
- package/imports/for_EventImport/given/a_kernel_import_session.ts +160 -0
- package/imports/for_EventImport/when_a_chunk_fails.spec.ts +65 -0
- package/imports/for_EventImport/when_abandoning.spec.ts +52 -0
- package/imports/for_EventImport/when_cancelling_with_an_abort_signal.spec.ts +59 -0
- package/imports/for_EventImport/when_completing.spec.ts +71 -0
- package/imports/for_EventImport/when_cutting_events_into_chunks.spec.ts +70 -0
- package/imports/for_EventImport/when_keeping_chunks_in_flight.spec.ts +49 -0
- package/imports/for_beginEventImport/when_reattaching.spec.ts +82 -0
- package/imports/for_beginEventImport/when_the_kernel_does_not_serve_imports.spec.ts +55 -0
- package/imports/for_importEvents/when_importing_in_one_call.spec.ts +99 -0
- package/imports/for_toImportEvent/when_converting_an_event.spec.ts +74 -0
- package/imports/importEvents.ts +44 -0
- package/imports/index.ts +14 -0
- package/imports/isTransientImportFailure.ts +15 -0
- package/imports/sizeOfImportEvent.ts +25 -0
- package/imports/toImportEvent.ts +60 -0
- package/imports/toImportProgress.ts +24 -0
- package/imports/toImportResult.ts +36 -0
- package/index.ts +3 -0
- package/observation/index.ts +2 -0
- package/package.json +7 -2
- package/projections/for_childrenOrderingSupport/when_detecting_support_in_the_pinned_contracts.spec.ts +1 -1
- package/testing/BatchPartitionOutcome.ts +10 -0
- package/testing/InProcessEventSequence.ts +4 -0
- package/testing/ReactorDeliveryResult.ts +2 -0
- package/testing/ReactorScenario.ts +147 -39
- package/testing/deliverToBatchMethods.ts +90 -0
- package/testing/for_ReactorScenario/when_the_reactor_has_a_batch_method.spec.ts +143 -9
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Waits for a while, unless aborted first.
|
|
6
|
+
* @param milliseconds - How long to wait.
|
|
7
|
+
* @param signal - Rejects the wait with its reason when aborted.
|
|
8
|
+
* @returns A promise that resolves after the wait.
|
|
9
|
+
*/
|
|
10
|
+
export function delay(milliseconds: number, signal?: AbortSignal): Promise<void> {
|
|
11
|
+
return untilAborted(new Promise<void>(resolve => {
|
|
12
|
+
const timer = setTimeout(resolve, milliseconds);
|
|
13
|
+
signal?.addEventListener('abort', () => clearTimeout(timer), { once: true });
|
|
14
|
+
}), signal);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Waits for a promise, unless aborted first. Aborting does not stop the work the promise stands for.
|
|
19
|
+
* @param promise - The promise to wait for.
|
|
20
|
+
* @param signal - Rejects the wait with its reason when aborted.
|
|
21
|
+
* @returns The value of the promise.
|
|
22
|
+
*/
|
|
23
|
+
export function untilAborted<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
|
|
24
|
+
if (!signal) return promise;
|
|
25
|
+
if (signal.aborted) return Promise.reject(signal.reason);
|
|
26
|
+
return new Promise<T>((resolve, reject) => {
|
|
27
|
+
const onAbort = () => reject(signal.reason);
|
|
28
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
29
|
+
promise.then(
|
|
30
|
+
value => { signal.removeEventListener('abort', onAbort); resolve(value); },
|
|
31
|
+
error => { signal.removeEventListener('abort', onAbort); reject(error); });
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Reads a sync or async iterable lazily, stopping as soon as a signal aborts, even while the source is waiting for
|
|
37
|
+
* its next value.
|
|
38
|
+
* @param source - The iterable to read.
|
|
39
|
+
* @param signal - Stops reading when aborted.
|
|
40
|
+
* @returns The values of the source.
|
|
41
|
+
*/
|
|
42
|
+
export async function* readUntilAborted<T>(source: AsyncIterable<T> | Iterable<T>, signal: AbortSignal): AsyncGenerator<T> {
|
|
43
|
+
const iterator: AsyncIterator<T> | Iterator<T> = Symbol.asyncIterator in source
|
|
44
|
+
? (source as AsyncIterable<T>)[Symbol.asyncIterator]()
|
|
45
|
+
: (source as Iterable<T>)[Symbol.iterator]();
|
|
46
|
+
let done = false;
|
|
47
|
+
try {
|
|
48
|
+
while (true) {
|
|
49
|
+
const next = await untilAborted(Promise.resolve(iterator.next()), signal);
|
|
50
|
+
if (next.done) { done = true; return; }
|
|
51
|
+
yield next.value;
|
|
52
|
+
}
|
|
53
|
+
} finally {
|
|
54
|
+
// Let the source release what it holds; a source still waiting for its next value is not waited for.
|
|
55
|
+
if (!done) void Promise.resolve(iterator.return?.()).catch(() => undefined);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import { OnInvalidEvent as ContractsOnInvalidEvent } from '@cratis/chronicle.contracts';
|
|
6
|
+
import { ClientError, Status } from 'nice-grpc-common';
|
|
7
|
+
import { fromContractsGuid, toContractsGuid } from '../connection/Guid.js';
|
|
8
|
+
import { ensureCommandResponse, ensureQuerySuccess } from '../connection/callResults.js';
|
|
9
|
+
import { EventImport } from './EventImport.js';
|
|
10
|
+
import type { IEventImport } from './IEventImport.js';
|
|
11
|
+
import { ImportCouldNotBegin } from './ImportCouldNotBegin.js';
|
|
12
|
+
import type { ImportOptions } from './ImportOptions.js';
|
|
13
|
+
import { ImportsNotSupported } from './ImportsNotSupported.js';
|
|
14
|
+
import type { ImportTarget } from './ImportTarget.js';
|
|
15
|
+
import { OnInvalidEvent } from './OnInvalidEvent.js';
|
|
16
|
+
import { toImportEvent } from './toImportEvent.js';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Begins a new import session, or reattaches to an existing one by its identifier.
|
|
20
|
+
* @param target - The {@link ImportTarget}.
|
|
21
|
+
* @param importIdOrOptions - The identifier of an import to reattach to, or the options for a new one.
|
|
22
|
+
* @param options - The options, when reattaching.
|
|
23
|
+
* @returns The {@link IEventImport}.
|
|
24
|
+
* @throws {@link ImportsNotSupported} when the kernel does not serve imports.
|
|
25
|
+
* @throws {@link ImportCouldNotBegin} when the kernel refuses the import, or the import to reattach to does not exist
|
|
26
|
+
* or targets another event sequence.
|
|
27
|
+
*/
|
|
28
|
+
export async function beginEventImport(
|
|
29
|
+
target: ImportTarget,
|
|
30
|
+
importIdOrOptions?: string | Guid | ImportOptions,
|
|
31
|
+
options?: ImportOptions
|
|
32
|
+
): Promise<IEventImport> {
|
|
33
|
+
const reattachTo = typeof importIdOrOptions === 'string' || importIdOrOptions instanceof Guid ? Guid.as(importIdOrOptions) : undefined;
|
|
34
|
+
const selected = (reattachTo ? options : importIdOrOptions as ImportOptions | undefined) ?? {};
|
|
35
|
+
selected.signal?.throwIfAborted();
|
|
36
|
+
try {
|
|
37
|
+
return reattachTo ? await reattach(target, reattachTo, selected) : await begin(target, selected);
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (error instanceof ClientError && error.code === Status.UNIMPLEMENTED) throw new ImportsNotSupported();
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function begin(target: ImportTarget, options: ImportOptions): Promise<IEventImport> {
|
|
45
|
+
const response = ensureCommandResponse('begin import', await target.imports.beginImport({
|
|
46
|
+
EventStore: target.eventStore,
|
|
47
|
+
Namespace: target.namespace,
|
|
48
|
+
EventSequenceId: target.eventSequenceId,
|
|
49
|
+
OnInvalid: (options.onInvalid ?? OnInvalidEvent.Reject) as number as ContractsOnInvalidEvent,
|
|
50
|
+
ImportId: options.importId === undefined ? undefined : toContractsGuid(Guid.as(options.importId))
|
|
51
|
+
}, { signal: options.signal }));
|
|
52
|
+
if (response.Error) throw new ImportCouldNotBegin(response.Error);
|
|
53
|
+
return create(target, options, fromContractsGuid(response.ImportId), response.MaxChunkEvents, response.MaxChunkBytes, 0, 0);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function reattach(target: ImportTarget, importId: Guid, options: ImportOptions): Promise<IEventImport> {
|
|
57
|
+
const progress = ensureQuerySuccess('get import', await target.imports.getImport({
|
|
58
|
+
EventStore: target.eventStore,
|
|
59
|
+
Namespace: target.namespace,
|
|
60
|
+
ImportId: toContractsGuid(importId)
|
|
61
|
+
}, { signal: options.signal }));
|
|
62
|
+
if (!progress) throw new ImportCouldNotBegin(`Import '${importId.toString()}' does not exist.`);
|
|
63
|
+
if (progress.EventSequenceId !== target.eventSequenceId) {
|
|
64
|
+
throw new ImportCouldNotBegin(
|
|
65
|
+
`Import '${importId.toString()}' imports into event sequence '${progress.EventSequenceId}', not '${target.eventSequenceId}'.`);
|
|
66
|
+
}
|
|
67
|
+
return create(target, { ...options, onInvalid: progress.OnInvalid as number as OnInvalidEvent, importId },
|
|
68
|
+
importId, progress.MaxChunkEvents, progress.MaxChunkBytes, progress.AcknowledgedChunks, Number(progress.AcknowledgedEvents));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function create(target: ImportTarget, options: ImportOptions, importId: Guid, maxChunkEvents: number, maxChunkBytes: number,
|
|
72
|
+
acknowledgedChunks: number, acknowledgedEvents: number): IEventImport {
|
|
73
|
+
return new EventImport({
|
|
74
|
+
imports: target.imports,
|
|
75
|
+
eventStore: target.eventStore,
|
|
76
|
+
namespace: target.namespace,
|
|
77
|
+
importId,
|
|
78
|
+
options,
|
|
79
|
+
maxChunkEvents,
|
|
80
|
+
maxChunkBytes,
|
|
81
|
+
convert: event => toImportEvent(event, target.eventSources),
|
|
82
|
+
acknowledgedChunks,
|
|
83
|
+
acknowledgedEvents
|
|
84
|
+
});
|
|
85
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import {
|
|
6
|
+
ImportState as ContractsImportState,
|
|
7
|
+
type AddImportChunkRequest,
|
|
8
|
+
type ImportEvent,
|
|
9
|
+
type ImportProgressResponse,
|
|
10
|
+
type ImportsClient
|
|
11
|
+
} from '@cratis/chronicle.contracts';
|
|
12
|
+
import { toContractsGuid } from '../../../connection/Guid.js';
|
|
13
|
+
import type { EventForEventSourceId } from '../../../eventSequences/EventForEventSourceId.js';
|
|
14
|
+
import { EventImport } from '../../EventImport.js';
|
|
15
|
+
import type { EventImportSettings } from '../../EventImportSettings.js';
|
|
16
|
+
|
|
17
|
+
/** A chunk the fake kernel received. */
|
|
18
|
+
export interface ReceivedChunk {
|
|
19
|
+
readonly chunkIndex: number;
|
|
20
|
+
readonly firstEventIndex: bigint;
|
|
21
|
+
readonly contents: readonly string[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A fake of the kernel's Imports service that records the calls an import session makes. */
|
|
25
|
+
export class a_kernel_import_session {
|
|
26
|
+
readonly importId = Guid.create();
|
|
27
|
+
readonly chunks: ReceivedChunk[] = [];
|
|
28
|
+
readonly completions: { ExpectedChunks: number; ExpectedEvents: bigint }[] = [];
|
|
29
|
+
readonly abandons: number[] = [];
|
|
30
|
+
inFlight = 0;
|
|
31
|
+
maxInFlight = 0;
|
|
32
|
+
/** Answers each chunk; replace it to fail, refuse or hold chunks. */
|
|
33
|
+
answerChunk: (request: AddImportChunkRequest) => Promise<{ Error: string }> = async () => ({ Error: '' });
|
|
34
|
+
/** The progress the kernel reports on each read, the last one repeating. */
|
|
35
|
+
reads: Partial<ImportProgressResponse>[] = [{ State: ContractsImportState.Completed }];
|
|
36
|
+
readCount = 0;
|
|
37
|
+
/** The progress the kernel streams to an observer. */
|
|
38
|
+
observed: Partial<ImportProgressResponse>[] = [];
|
|
39
|
+
|
|
40
|
+
/** The chunk limits the kernel gives when an import begins. */
|
|
41
|
+
limits = { MaxChunkEvents: 0, MaxChunkBytes: 0 };
|
|
42
|
+
|
|
43
|
+
readonly imports = {
|
|
44
|
+
beginImport: async () => this.commandResult({ ImportId: toContractsGuid(this.importId), State: ContractsImportState.Open,
|
|
45
|
+
...this.limits, Error: '' }),
|
|
46
|
+
addImportChunk: async (request: AddImportChunkRequest) => {
|
|
47
|
+
this.inFlight++;
|
|
48
|
+
this.maxInFlight = Math.max(this.maxInFlight, this.inFlight);
|
|
49
|
+
try {
|
|
50
|
+
const answer = await this.answerChunk(request);
|
|
51
|
+
if (!answer.Error) {
|
|
52
|
+
this.chunks.push({
|
|
53
|
+
chunkIndex: request.ChunkIndex,
|
|
54
|
+
firstEventIndex: request.FirstEventIndex,
|
|
55
|
+
contents: request.Events.map(event => event.Content)
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return this.commandResult({ ChunkIndex: request.ChunkIndex, AlreadyStaged: false, EventsReceived: request.Events.length,
|
|
59
|
+
EventsStaged: request.Events.length, Rejections: [], Error: answer.Error });
|
|
60
|
+
} finally {
|
|
61
|
+
this.inFlight--;
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
completeImport: async (request: { ExpectedChunks: number; ExpectedEvents: bigint }) => {
|
|
65
|
+
this.completions.push({ ExpectedChunks: request.ExpectedChunks, ExpectedEvents: request.ExpectedEvents });
|
|
66
|
+
return this.commandResult({ ImportId: toContractsGuid(this.importId), State: ContractsImportState.Validating, Error: '' });
|
|
67
|
+
},
|
|
68
|
+
abandonImport: async () => {
|
|
69
|
+
this.abandons.push(this.chunks.length);
|
|
70
|
+
this.reads = [{ State: ContractsImportState.Abandoned }];
|
|
71
|
+
return this.commandResult({ ImportId: toContractsGuid(this.importId), State: ContractsImportState.Abandoned, Error: '' });
|
|
72
|
+
},
|
|
73
|
+
getImport: async () => {
|
|
74
|
+
const read = this.reads[Math.min(this.readCount++, this.reads.length - 1)];
|
|
75
|
+
return { IsAuthorized: true, ValidationResults: [], ExceptionMessages: [], ExceptionStackTrace: '', CorrelationId: undefined,
|
|
76
|
+
Data: this.progress(read) };
|
|
77
|
+
},
|
|
78
|
+
observeImport: () => this.observe()
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
get client(): ImportsClient {
|
|
82
|
+
return this.imports as unknown as ImportsClient;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Creates a session against this fake, converting each event to a wire event carrying its JSON. */
|
|
86
|
+
session(settings: Partial<EventImportSettings> = {}): EventImport {
|
|
87
|
+
return new EventImport({
|
|
88
|
+
imports: this.client,
|
|
89
|
+
eventStore: 'store',
|
|
90
|
+
namespace: 'default',
|
|
91
|
+
importId: this.importId,
|
|
92
|
+
options: {},
|
|
93
|
+
maxChunkEvents: 0,
|
|
94
|
+
maxChunkBytes: 0,
|
|
95
|
+
convert: wireEventFor,
|
|
96
|
+
retryDelay: () => 0,
|
|
97
|
+
pollInterval: 0,
|
|
98
|
+
...settings
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** The contents of every received chunk, in chunk order. */
|
|
103
|
+
get receivedContents(): string[] {
|
|
104
|
+
return [...this.chunks].sort((left, right) => left.chunkIndex - right.chunkIndex).flatMap(chunk => [...chunk.contents]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async *observe() {
|
|
108
|
+
for (const progress of this.observed) {
|
|
109
|
+
yield { IsAuthorized: true, ValidationResults: [], ExceptionMessages: [], ExceptionStackTrace: '', CorrelationId: undefined,
|
|
110
|
+
Data: this.progress(progress) };
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
progress(values: Partial<ImportProgressResponse>): ImportProgressResponse {
|
|
115
|
+
return {
|
|
116
|
+
ImportId: toContractsGuid(this.importId), EventSequenceId: 'event-log', State: ContractsImportState.Staging, OnInvalid: 0,
|
|
117
|
+
Created: undefined, LastActivity: undefined, StagedChunks: [], EventsReceived: 0n, EventsStaged: 0n, EventsCommitted: 0n,
|
|
118
|
+
AlreadyAppended: 0n, RejectedCount: 0n, Rejections: [], CommittedRanges: [], FirstSequenceNumber: 0n, LastSequenceNumber: 0n,
|
|
119
|
+
FailureReason: '', MaxChunkEvents: 0, MaxChunkBytes: 0, AcknowledgedChunks: 0, AcknowledgedEvents: 0n,
|
|
120
|
+
...values
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
private commandResult<T>(response: T) {
|
|
125
|
+
return { IsAuthorized: true, ValidationResults: [], ExceptionMessages: [], ExceptionStackTrace: '', AuthorizationFailureReason: '',
|
|
126
|
+
CorrelationId: undefined, Response: response };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** An event for the fake, identified by its number. */
|
|
131
|
+
export function numbered(number: number): EventForEventSourceId {
|
|
132
|
+
return { eventSourceId: 'source', event: { number } };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The numbered events from zero up to a count. */
|
|
136
|
+
export function numberedEvents(count: number): EventForEventSourceId[] {
|
|
137
|
+
return Array.from({ length: count }, (_, index) => numbered(index));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/** The wire contents numbered events produce. */
|
|
141
|
+
export function contentsOf(count: number): string[] {
|
|
142
|
+
return numberedEvents(count).map(event => JSON.stringify(event.event));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A wire event carrying the event's JSON as its content. */
|
|
146
|
+
export function wireEventFor(event: EventForEventSourceId): ImportEvent {
|
|
147
|
+
return {
|
|
148
|
+
EventSourceId: event.eventSourceId, EventSourceType: '', EventStreamType: '', EventStreamId: '',
|
|
149
|
+
EventType: { Id: 'numbered', Generation: 1, Tombstone: false }, Content: JSON.stringify(event.event), Tags: [], NamedTags: [],
|
|
150
|
+
Occurred: undefined, Subject: '', CorrelationId: undefined, Causation: [], CausedBy: undefined, IdempotencyKey: '', EventSource: '',
|
|
151
|
+
SourceSequenceNumber: 0n
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** A promise with its resolver, for holding a fake call until a spec lets it go. */
|
|
156
|
+
export function gate(): { promise: Promise<void>; open: () => void } {
|
|
157
|
+
let open!: () => void;
|
|
158
|
+
const promise = new Promise<void>(resolve => { open = resolve; });
|
|
159
|
+
return { promise, open };
|
|
160
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ClientError, Status } from 'nice-grpc-common';
|
|
5
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
6
|
+
import { ImportChunkRefused } from '../ImportChunkRefused.js';
|
|
7
|
+
import { a_kernel_import_session, contentsOf, numberedEvents } from './given/a_kernel_import_session.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
describe('when a chunk fails because the connection dropped', () => {
|
|
12
|
+
let kernel: a_kernel_import_session;
|
|
13
|
+
let attempts: number[];
|
|
14
|
+
beforeEach(async () => {
|
|
15
|
+
kernel = new a_kernel_import_session();
|
|
16
|
+
attempts = [];
|
|
17
|
+
kernel.answerChunk = async request => {
|
|
18
|
+
attempts.push(request.ChunkIndex);
|
|
19
|
+
if (request.ChunkIndex === 1 && attempts.filter(index => index === 1).length < 3) {
|
|
20
|
+
throw new ClientError('/Imports/AddImportChunk', Status.UNAVAILABLE, 'connection reset');
|
|
21
|
+
}
|
|
22
|
+
return { Error: '' };
|
|
23
|
+
};
|
|
24
|
+
const session = kernel.session({ maxChunkEvents: 2 });
|
|
25
|
+
await session.add(numberedEvents(6));
|
|
26
|
+
await session.complete();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should send the same chunk again', () => attempts.filter(index => index === 1).length.should.equal(3));
|
|
30
|
+
it('should send every event once, in order', () => kernel.receivedContents.should.deep.equal(contentsOf(6)));
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('when a chunk keeps failing because the connection dropped', () => {
|
|
34
|
+
let error: unknown;
|
|
35
|
+
let kernel: a_kernel_import_session;
|
|
36
|
+
beforeEach(async () => {
|
|
37
|
+
kernel = new a_kernel_import_session();
|
|
38
|
+
kernel.answerChunk = async () => { throw new ClientError('/Imports/AddImportChunk', Status.UNAVAILABLE, 'down'); };
|
|
39
|
+
const session = kernel.session({ options: { maxChunkAttempts: 2 } });
|
|
40
|
+
await session.add(numberedEvents(1));
|
|
41
|
+
error = await session.complete().catch(caught => caught);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('should fail completing with the connection failure', () => (error as ClientError).code.should.equal(Status.UNAVAILABLE));
|
|
45
|
+
it('should not complete the import on the kernel', () => kernel.completions.should.be.empty);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('when the kernel refuses a chunk', () => {
|
|
49
|
+
let error: unknown;
|
|
50
|
+
let attempts: number;
|
|
51
|
+
let kernel: a_kernel_import_session;
|
|
52
|
+
beforeEach(async () => {
|
|
53
|
+
kernel = new a_kernel_import_session();
|
|
54
|
+
attempts = 0;
|
|
55
|
+
kernel.answerChunk = async () => { attempts++; return { Error: 'The import is not staging' }; };
|
|
56
|
+
const session = kernel.session();
|
|
57
|
+
await session.add(numberedEvents(1));
|
|
58
|
+
error = await session.complete().catch(caught => caught);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('should fail completing with the refusal', () => error.should.be.instanceOf(ImportChunkRefused));
|
|
62
|
+
it('should carry the reason', () => (error as ImportChunkRefused).reason.should.equal('The import is not staging'));
|
|
63
|
+
it('should not send the chunk again', () => attempts.should.equal(1));
|
|
64
|
+
it('should not complete the import on the kernel', () => kernel.completions.should.be.empty);
|
|
65
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { EventForEventSourceId } from '../../eventSequences/EventForEventSourceId.js';
|
|
6
|
+
import type { ImportResult } from '../ImportResult.js';
|
|
7
|
+
import { ImportState } from '../ImportState.js';
|
|
8
|
+
import { ImportWasAbandoned } from '../ImportWasAbandoned.js';
|
|
9
|
+
import { a_kernel_import_session, numbered, numberedEvents } from './given/a_kernel_import_session.js';
|
|
10
|
+
|
|
11
|
+
chai.should();
|
|
12
|
+
|
|
13
|
+
describe('when abandoning an import while events are being added from a source that stalls', () => {
|
|
14
|
+
let kernel: a_kernel_import_session;
|
|
15
|
+
let addError: unknown;
|
|
16
|
+
let completeError: unknown;
|
|
17
|
+
let result: ImportResult;
|
|
18
|
+
beforeEach(async () => {
|
|
19
|
+
kernel = new a_kernel_import_session();
|
|
20
|
+
async function* stalling(): AsyncGenerator<EventForEventSourceId> {
|
|
21
|
+
yield numbered(0);
|
|
22
|
+
yield numbered(1);
|
|
23
|
+
await new Promise(() => undefined);
|
|
24
|
+
}
|
|
25
|
+
const session = kernel.session({ maxChunkEvents: 1 });
|
|
26
|
+
const adding = session.add(stalling()).catch(caught => caught);
|
|
27
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
28
|
+
result = await session.abandon();
|
|
29
|
+
addError = await adding;
|
|
30
|
+
completeError = await session.complete().catch(caught => caught);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('should stop the add', () => addError.should.be.instanceOf(ImportWasAbandoned));
|
|
34
|
+
it('should abandon the import on the kernel after the chunks already sent', () => kernel.abandons.should.deep.equal([1]));
|
|
35
|
+
it('should report the import as abandoned', () => result.state.should.equal(ImportState.Abandoned));
|
|
36
|
+
it('should refuse to complete', () => completeError.should.be.instanceOf(ImportWasAbandoned));
|
|
37
|
+
it('should not complete the import on the kernel', () => kernel.completions.should.be.empty);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('when adding to an abandoned import', () => {
|
|
41
|
+
let error: unknown;
|
|
42
|
+
let kernel: a_kernel_import_session;
|
|
43
|
+
beforeEach(async () => {
|
|
44
|
+
kernel = new a_kernel_import_session();
|
|
45
|
+
const session = kernel.session();
|
|
46
|
+
await session.abandon();
|
|
47
|
+
error = await session.add(numberedEvents(1)).catch(caught => caught);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('should refuse the events', () => error.should.be.instanceOf(ImportWasAbandoned));
|
|
51
|
+
it('should send nothing', () => kernel.chunks.should.be.empty);
|
|
52
|
+
});
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import type { EventForEventSourceId } from '../../eventSequences/EventForEventSourceId.js';
|
|
6
|
+
import { ImportWasAbandoned } from '../ImportWasAbandoned.js';
|
|
7
|
+
import { a_kernel_import_session, numbered } from './given/a_kernel_import_session.js';
|
|
8
|
+
|
|
9
|
+
chai.should();
|
|
10
|
+
|
|
11
|
+
describe('when cancelling an add with an abort signal while the source stalls', () => {
|
|
12
|
+
let kernel: a_kernel_import_session;
|
|
13
|
+
let error: unknown;
|
|
14
|
+
let reason: Error;
|
|
15
|
+
let stillUsable: boolean;
|
|
16
|
+
beforeEach(async () => {
|
|
17
|
+
kernel = new a_kernel_import_session();
|
|
18
|
+
async function* stalling(): AsyncGenerator<EventForEventSourceId> {
|
|
19
|
+
yield numbered(0);
|
|
20
|
+
yield numbered(1);
|
|
21
|
+
yield numbered(2);
|
|
22
|
+
await new Promise(() => undefined);
|
|
23
|
+
}
|
|
24
|
+
const controller = new AbortController();
|
|
25
|
+
reason = new Error('the operator cancelled');
|
|
26
|
+
const session = kernel.session({ maxChunkEvents: 2 });
|
|
27
|
+
const adding = session.add(stalling(), controller.signal).catch(caught => caught);
|
|
28
|
+
await new Promise(resolve => setTimeout(resolve, 10));
|
|
29
|
+
controller.abort(reason);
|
|
30
|
+
error = await adding;
|
|
31
|
+
await session.add([numbered(3)]);
|
|
32
|
+
await session.complete();
|
|
33
|
+
stillUsable = true;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should stop with the reason it was cancelled for', () => error!.should.equal(reason));
|
|
37
|
+
it('should not report the import as abandoned', () => error!.should.not.be.instanceOf(ImportWasAbandoned));
|
|
38
|
+
it('should keep the chunks already handed to the kernel', () => kernel.chunks[0].contents.length.should.equal(2));
|
|
39
|
+
it('should leave the session usable', () => stillUsable.should.be.true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('when cancelling completion with an abort signal while waiting for the commit', () => {
|
|
43
|
+
let error: unknown;
|
|
44
|
+
let reason: Error;
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
const kernel = new a_kernel_import_session();
|
|
47
|
+
kernel.reads = [{ State: 3 }];
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
reason = new Error('stop waiting');
|
|
50
|
+
const session = kernel.session({ pollInterval: 5 });
|
|
51
|
+
await session.add([numbered(0)]);
|
|
52
|
+
const completing = session.complete(controller.signal).catch(caught => caught);
|
|
53
|
+
await new Promise(resolve => setTimeout(resolve, 20));
|
|
54
|
+
controller.abort(reason);
|
|
55
|
+
error = await completing;
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('should stop waiting with the reason it was cancelled for', () => error!.should.equal(reason));
|
|
59
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { ImportState as ContractsImportState } from '@cratis/chronicle.contracts';
|
|
5
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
6
|
+
import type { ImportResult } from '../ImportResult.js';
|
|
7
|
+
import { ImportState } from '../ImportState.js';
|
|
8
|
+
import { a_kernel_import_session, numberedEvents } from './given/a_kernel_import_session.js';
|
|
9
|
+
|
|
10
|
+
chai.should();
|
|
11
|
+
|
|
12
|
+
describe('when completing an import that commits', () => {
|
|
13
|
+
let kernel: a_kernel_import_session;
|
|
14
|
+
let result: ImportResult;
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
kernel = new a_kernel_import_session();
|
|
17
|
+
kernel.reads = [
|
|
18
|
+
{ State: ContractsImportState.Validating },
|
|
19
|
+
{ State: ContractsImportState.Committing, EventsStaged: 3n, EventsCommitted: 1n },
|
|
20
|
+
{ State: ContractsImportState.Completed, EventsStaged: 3n, EventsCommitted: 3n, FirstSequenceNumber: 10n, LastSequenceNumber: 12n }
|
|
21
|
+
];
|
|
22
|
+
const session = kernel.session();
|
|
23
|
+
await session.add(numberedEvents(3));
|
|
24
|
+
result = await session.complete();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should wait until the commit is done', () => kernel.readCount.should.equal(3));
|
|
28
|
+
it('should be a success', () => result.isSuccess.should.be.true);
|
|
29
|
+
it('should report the events committed', () => result.eventsCommitted.should.equal(3));
|
|
30
|
+
it('should report the first sequence number', () => result.firstSequenceNumber!.value.should.equal(10n));
|
|
31
|
+
it('should report the last sequence number', () => result.lastSequenceNumber!.value.should.equal(12n));
|
|
32
|
+
it('should carry the identifier of the import', () => result.importId.toString().should.equal(kernel.importId.toString()));
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('when completing an import with invalid events', () => {
|
|
36
|
+
let result: ImportResult;
|
|
37
|
+
beforeEach(async () => {
|
|
38
|
+
const kernel = new a_kernel_import_session();
|
|
39
|
+
kernel.reads = [{
|
|
40
|
+
State: ContractsImportState.Failed, EventsStaged: 2n, RejectedCount: 1n, FailureReason: 'One event was rejected',
|
|
41
|
+
Rejections: [{ Index: 1n, EventType: 'numbered', Reasons: ['number must be positive'] }]
|
|
42
|
+
}];
|
|
43
|
+
const session = kernel.session();
|
|
44
|
+
await session.add(numberedEvents(2));
|
|
45
|
+
result = await session.complete();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should not be a success', () => result.isSuccess.should.be.false);
|
|
49
|
+
it('should be failed', () => result.state.should.equal(ImportState.Failed));
|
|
50
|
+
it('should commit nothing', () => result.eventsCommitted.should.equal(0));
|
|
51
|
+
it('should report no sequence numbers', () => (result.firstSequenceNumber === undefined).should.be.true);
|
|
52
|
+
it('should report the rejected event by its index', () =>
|
|
53
|
+
result.rejections.should.deep.equal([{ index: 1, eventType: 'numbered', reasons: ['number must be positive'] }]));
|
|
54
|
+
it('should report how many were rejected', () => result.rejectedCount.should.equal(1));
|
|
55
|
+
it('should report why it failed', () => result.failureReason!.should.equal('One event was rejected'));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('when completing an import without waiting for the commit', () => {
|
|
59
|
+
let kernel: a_kernel_import_session;
|
|
60
|
+
let result: ImportResult;
|
|
61
|
+
beforeEach(async () => {
|
|
62
|
+
kernel = new a_kernel_import_session();
|
|
63
|
+
kernel.reads = [{ State: ContractsImportState.Committing }, { State: ContractsImportState.Completed }];
|
|
64
|
+
const session = kernel.session({ options: { waitForCommit: false } });
|
|
65
|
+
await session.add(numberedEvents(1));
|
|
66
|
+
result = await session.complete();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should read the import once', () => kernel.readCount.should.equal(1));
|
|
70
|
+
it('should report the state it read', () => result.state.should.equal(ImportState.Committing));
|
|
71
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright (c) Cratis. All rights reserved.
|
|
2
|
+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
|
3
|
+
|
|
4
|
+
import { beforeEach, chai, describe, it } from 'vitest';
|
|
5
|
+
import { sizeOfImportEvent } from '../sizeOfImportEvent.js';
|
|
6
|
+
import { a_kernel_import_session, contentsOf, numberedEvents, wireEventFor } from './given/a_kernel_import_session.js';
|
|
7
|
+
|
|
8
|
+
chai.should();
|
|
9
|
+
|
|
10
|
+
describe('when cutting events into chunks within the kernel event limit', () => {
|
|
11
|
+
let kernel: a_kernel_import_session;
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
kernel = new a_kernel_import_session();
|
|
14
|
+
const session = kernel.session({ maxChunkEvents: 3 });
|
|
15
|
+
await session.add(numberedEvents(10));
|
|
16
|
+
await session.complete();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should send chunks of at most the limit', () => kernel.chunks.map(chunk => chunk.contents.length).sort().should.deep.equal([1, 3, 3, 3]));
|
|
20
|
+
it('should number the chunks in order', () => kernel.chunks.map(chunk => chunk.chunkIndex).sort().should.deep.equal([0, 1, 2, 3]));
|
|
21
|
+
it('should give each chunk the index of its first event', () =>
|
|
22
|
+
[...kernel.chunks].sort((left, right) => left.chunkIndex - right.chunkIndex).map(chunk => chunk.firstEventIndex)
|
|
23
|
+
.should.deep.equal([0n, 3n, 6n, 9n]));
|
|
24
|
+
it('should send every event once, in order', () => kernel.receivedContents.should.deep.equal(contentsOf(10)));
|
|
25
|
+
it('should complete with the chunks and events it sent', () =>
|
|
26
|
+
kernel.completions.should.deep.equal([{ ExpectedChunks: 4, ExpectedEvents: 10n }]));
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
describe('when cutting events into chunks within the kernel byte limit', () => {
|
|
30
|
+
let kernel: a_kernel_import_session;
|
|
31
|
+
let limit: number;
|
|
32
|
+
beforeEach(async () => {
|
|
33
|
+
kernel = new a_kernel_import_session();
|
|
34
|
+
limit = sizeOfImportEvent(wireEventFor(numberedEvents(1)[0])) * 2;
|
|
35
|
+
const session = kernel.session({ maxChunkBytes: limit });
|
|
36
|
+
await session.add(numberedEvents(5));
|
|
37
|
+
await session.complete();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('should keep each chunk within the limit', () =>
|
|
41
|
+
kernel.chunks.every(chunk => chunk.contents.reduce((bytes, content) => bytes + Buffer.byteLength(content), 0) <= limit).should.be.true);
|
|
42
|
+
it('should send every event once, in order', () => kernel.receivedContents.should.deep.equal(contentsOf(5)));
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('when cutting events into chunks with a client limit below the kernel limit', () => {
|
|
46
|
+
let kernel: a_kernel_import_session;
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
kernel = new a_kernel_import_session();
|
|
49
|
+
const session = kernel.session({ maxChunkEvents: 100, options: { maxEventsPerChunk: 2 } });
|
|
50
|
+
await session.add(numberedEvents(5));
|
|
51
|
+
await session.complete();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('should use the lower limit', () => kernel.chunks.map(chunk => chunk.contents.length).sort().should.deep.equal([1, 2, 2]));
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe('when adding events in several calls', () => {
|
|
58
|
+
let kernel: a_kernel_import_session;
|
|
59
|
+
beforeEach(async () => {
|
|
60
|
+
kernel = new a_kernel_import_session();
|
|
61
|
+
const session = kernel.session({ maxChunkEvents: 4 });
|
|
62
|
+
const events = numberedEvents(6);
|
|
63
|
+
await session.add(events.slice(0, 3));
|
|
64
|
+
await session.add((async function* () { yield* events.slice(3); })());
|
|
65
|
+
await session.complete();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should fill chunks across the calls', () => kernel.chunks.map(chunk => chunk.contents.length).sort().should.deep.equal([2, 4]));
|
|
69
|
+
it('should send every event once, in order', () => kernel.receivedContents.should.deep.equal(contentsOf(6)));
|
|
70
|
+
});
|