@cratis/chronicle 6.61.0 → 6.63.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/ChronicleClient.ts +86 -3
- package/connection/ChronicleConnection.ts +7 -1
- package/connection/ChronicleServices.ts +2 -0
- package/connection/ConnectionLifecycle.ts +42 -1
- package/dist/ChronicleClient.d.ts +28 -1
- package/dist/ChronicleClient.d.ts.map +1 -1
- package/dist/ChronicleClient.js +71 -3
- package/dist/ChronicleClient.js.map +1 -1
- 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/connection/ConnectionLifecycle.d.ts +24 -0
- package/dist/connection/ConnectionLifecycle.d.ts.map +1 -1
- package/dist/connection/ConnectionLifecycle.js +40 -1
- package/dist/connection/ConnectionLifecycle.js.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/ObservationReconnectBackoff.d.ts +64 -0
- package/dist/observation/ObservationReconnectBackoff.d.ts.map +1 -0
- package/dist/observation/ObservationReconnectBackoff.js +75 -0
- package/dist/observation/ObservationReconnectBackoff.js.map +1 -0
- package/dist/observation/ObservationStreamOutcome.d.ts +10 -0
- package/dist/observation/ObservationStreamOutcome.d.ts.map +1 -0
- package/dist/observation/ObservationStreamOutcome.js +4 -0
- package/dist/observation/ObservationStreamOutcome.js.map +1 -0
- package/dist/observation/classifyStreamError.d.ts +25 -0
- package/dist/observation/classifyStreamError.d.ts.map +1 -0
- package/dist/observation/classifyStreamError.js +55 -0
- package/dist/observation/classifyStreamError.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts +13 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js +25 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_backoff.js.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts +27 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.d.ts.map +1 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js +36 -0
- package/dist/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.js.map +1 -0
- 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/reactors/Reactors.d.ts +15 -5
- package/dist/reactors/Reactors.d.ts.map +1 -1
- package/dist/reactors/Reactors.js +95 -18
- package/dist/reactors/Reactors.js.map +1 -1
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts +26 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.d.ts.map +1 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js +64 -0
- package/dist/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.js.map +1 -0
- package/dist/reducers/Reducers.d.ts +11 -5
- package/dist/reducers/Reducers.d.ts.map +1 -1
- package/dist/reducers/Reducers.js +93 -19
- package/dist/reducers/Reducers.js.map +1 -1
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts +25 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.d.ts.map +1 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js +70 -0
- package/dist/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.js.map +1 -0
- 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/for_ChronicleClient/when_reconnecting/and_an_observation_stream_lost_its_session.spec.ts +212 -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/ObservationReconnectBackoff.ts +109 -0
- package/observation/ObservationStreamOutcome.ts +12 -0
- package/observation/classifyStreamError.ts +67 -0
- package/observation/for_ObservationReconnectBackoff/given/a_backoff.ts +29 -0
- package/observation/for_ObservationReconnectBackoff/given/a_scripted_stream.ts +52 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_delivered_before_it_ended.spec.ts +23 -0
- package/observation/for_ObservationReconnectBackoff/when_a_stream_stayed_open_before_it_ended.spec.ts +25 -0
- package/observation/for_ObservationReconnectBackoff/when_jittering_the_delay.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_streams_keep_ending_without_working.spec.ts +24 -0
- package/observation/for_ObservationReconnectBackoff/when_the_base_delay_is_not_positive.spec.ts +18 -0
- package/observation/for_ObservationReconnectBackoff/when_the_settings_are_not_finite.spec.ts +32 -0
- package/observation/for_ObservationReconnectBackoff/when_using_the_defaults.spec.ts +29 -0
- package/observation/for_classifyStreamError/when_classifying_a_stream_error.spec.ts +36 -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/reactors/Reactors.ts +101 -18
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_handler_failed_with_a_lost_session.spec.ts +32 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reactors/for_Reactors/when_reconnecting_the_observation_stream/given/a_reactor_whose_stream_ends.ts +69 -0
- package/reducers/Reducers.ts +99 -19
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_reducer_failed_with_a_lost_session.spec.ts +33 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_replaced_stream_fails_late.spec.ts +28 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_delivered_before_it_ended.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_a_stream_stayed_open_before_it_ended.spec.ts +23 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_it_is_disposed_while_waiting_to_reopen.spec.ts +27 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_streams_keep_ending_without_working.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_after_streams_kept_ending.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_client_reconnected_while_waiting_to_reopen.spec.ts +25 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/and_the_kernel_lost_the_session.spec.ts +24 -0
- package/reducers/for_Reducers/when_reconnecting_the_observation_stream/given/a_reducer_whose_stream_ends.ts +69 -0
- 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,14 @@
|
|
|
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
|
+
/** An event an import rejected. */
|
|
5
|
+
export interface ImportRejection {
|
|
6
|
+
/** The position of the event in the import, counted from zero across every chunk. */
|
|
7
|
+
readonly index: number;
|
|
8
|
+
|
|
9
|
+
/** The identifier of the event's type. */
|
|
10
|
+
readonly eventType: string;
|
|
11
|
+
|
|
12
|
+
/** Why the event was rejected. */
|
|
13
|
+
readonly reasons: readonly string[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
import type { EventSequenceNumber } from '../eventSequences/EventSequenceNumber.js';
|
|
6
|
+
import type { ImportRejection } from './ImportRejection.js';
|
|
7
|
+
import type { ImportState } from './ImportState.js';
|
|
8
|
+
|
|
9
|
+
/** The outcome of an import. */
|
|
10
|
+
export interface ImportResult {
|
|
11
|
+
/** The identifier of the import. */
|
|
12
|
+
readonly importId: Guid;
|
|
13
|
+
|
|
14
|
+
/** The state the import reached. */
|
|
15
|
+
readonly state: ImportState;
|
|
16
|
+
|
|
17
|
+
/** How many events were staged. */
|
|
18
|
+
readonly eventsStaged: number;
|
|
19
|
+
|
|
20
|
+
/** How many events were committed. */
|
|
21
|
+
readonly eventsCommitted: number;
|
|
22
|
+
|
|
23
|
+
/** How many events were already appended under their idempotency key, and so not appended again. */
|
|
24
|
+
readonly alreadyAppended: number;
|
|
25
|
+
|
|
26
|
+
/** The first sequence number the import committed, if it committed any. */
|
|
27
|
+
readonly firstSequenceNumber?: EventSequenceNumber;
|
|
28
|
+
|
|
29
|
+
/** The last sequence number the import committed, if it committed any. */
|
|
30
|
+
readonly lastSequenceNumber?: EventSequenceNumber;
|
|
31
|
+
|
|
32
|
+
/** How many events were rejected, including any beyond the retained {@link ImportResult.rejections}. */
|
|
33
|
+
readonly rejectedCount: number;
|
|
34
|
+
|
|
35
|
+
/** The rejected events, each with its index in the import, its event type and the reasons. */
|
|
36
|
+
readonly rejections: readonly ImportRejection[];
|
|
37
|
+
|
|
38
|
+
/** Why the import failed or was abandoned, if it was. */
|
|
39
|
+
readonly failureReason?: string;
|
|
40
|
+
|
|
41
|
+
/** Whether the import completed. */
|
|
42
|
+
readonly isSuccess: boolean;
|
|
43
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
/** The state of an import session. */
|
|
5
|
+
export enum ImportState {
|
|
6
|
+
/** The import is open and has not received any chunk yet. */
|
|
7
|
+
Open = 0,
|
|
8
|
+
|
|
9
|
+
/** The import has received chunks and accepts more. */
|
|
10
|
+
Staging = 1,
|
|
11
|
+
|
|
12
|
+
/** Staging is closed and the whole import is being validated. */
|
|
13
|
+
Validating = 2,
|
|
14
|
+
|
|
15
|
+
/** The staged events are being committed to the event sequence. */
|
|
16
|
+
Committing = 3,
|
|
17
|
+
|
|
18
|
+
/** Every staged event was committed or reported. */
|
|
19
|
+
Completed = 4,
|
|
20
|
+
|
|
21
|
+
/** The import failed; when it failed before committing, nothing was committed. */
|
|
22
|
+
Failed = 5,
|
|
23
|
+
|
|
24
|
+
/** The import was abandoned before its commit started. */
|
|
25
|
+
Abandoned = 6
|
|
26
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
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 type { ImportsClient } from '@cratis/chronicle.contracts';
|
|
5
|
+
import type { IEventSources } from '../eventSources/IEventSources.js';
|
|
6
|
+
|
|
7
|
+
/** The event sequence an import targets, and how to reach the kernel's Imports service. */
|
|
8
|
+
export interface ImportTarget {
|
|
9
|
+
/** The Imports service. */
|
|
10
|
+
readonly imports: ImportsClient;
|
|
11
|
+
|
|
12
|
+
/** The name of the event store. */
|
|
13
|
+
readonly eventStore: string;
|
|
14
|
+
|
|
15
|
+
/** The namespace. */
|
|
16
|
+
readonly namespace: string;
|
|
17
|
+
|
|
18
|
+
/** The identifier of the event sequence the events are imported into. */
|
|
19
|
+
readonly eventSequenceId: string;
|
|
20
|
+
|
|
21
|
+
/** The registered event sources, for events routed through a definition. */
|
|
22
|
+
readonly eventSources?: IEventSources;
|
|
23
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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 type { Guid } from '@cratis/fundamentals';
|
|
5
|
+
|
|
6
|
+
/** The exception that is thrown when events are added to, or completed on, an import that was abandoned. */
|
|
7
|
+
export class ImportWasAbandoned extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new {@link ImportWasAbandoned}.
|
|
10
|
+
* @param importId - The identifier of the import.
|
|
11
|
+
*/
|
|
12
|
+
constructor(readonly importId: Guid) {
|
|
13
|
+
super(`Import '${importId.toString()}' was abandoned.`);
|
|
14
|
+
this.name = 'ImportWasAbandoned';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
/** The exception that is thrown when the Chronicle kernel does not support imports. */
|
|
5
|
+
export class ImportsNotSupported extends Error {
|
|
6
|
+
constructor() {
|
|
7
|
+
super('The Chronicle kernel does not support importing events. Upgrade the kernel to a version that serves the Imports service.');
|
|
8
|
+
this.name = 'ImportsNotSupported';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
/** What an import does when one of its events is invalid. */
|
|
5
|
+
export enum OnInvalidEvent {
|
|
6
|
+
/** Fail the whole import and commit nothing. The default. */
|
|
7
|
+
Reject = 0,
|
|
8
|
+
|
|
9
|
+
/** Leave the invalid events out, report them, and commit the rest. */
|
|
10
|
+
Skip = 1
|
|
11
|
+
}
|
|
@@ -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
|
+
});
|