Skip to content

Commit

Permalink
feat: begin to frickle change of config using restart
Browse files Browse the repository at this point in the history
  • Loading branch information
Milena-Czierlinski committed Sep 16, 2024
1 parent 84e22f3 commit 71e8514
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
23 changes: 18 additions & 5 deletions packages/runtime/src/Runtime.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IDatabaseConnection } from "@js-soft/docdb-access-abstractions";
import { LokiJsConnection } from "@js-soft/docdb-access-loki";
import { MongoDbConnection } from "@js-soft/docdb-access-mongo";
import { ILogger, ILoggerFactory } from "@js-soft/logging-abstractions";
import { EventBus, EventEmitter2EventBus } from "@js-soft/ts-utils";
import {
Expand Down Expand Up @@ -126,7 +128,7 @@ export abstract class Runtime<TConfig extends RuntimeConfig = RuntimeConfig> {
return this._isInitialized;
}

public async init(): Promise<void> {
public async init(existingDatabaseConnection?: MongoDbConnection | LokiJsConnection): Promise<void> {
if (this._isInitialized) {
throw RuntimeErrors.general.alreadyInitialized();
}
Expand All @@ -135,7 +137,7 @@ export abstract class Runtime<TConfig extends RuntimeConfig = RuntimeConfig> {

await this.initDIContainer();

await this.initTransportLibrary();
await this.initTransportLibrary(existingDatabaseConnection);
await this.initAccount();

this._modules = new RuntimeModuleRegistry();
Expand Down Expand Up @@ -169,10 +171,21 @@ export abstract class Runtime<TConfig extends RuntimeConfig = RuntimeConfig> {
};
}

private async initTransportLibrary() {
private async initTransportLibrary(existingDatabaseConnection?: MongoDbConnection | LokiJsConnection) {
this.logger.debug("Initializing Database connection... ");

const databaseConnection = await this.createDatabaseConnection();
let databaseConnection;
if (existingDatabaseConnection) {
// TODO: maybe this needs to be connected newly
databaseConnection = existingDatabaseConnection;
if (databaseConnection instanceof LokiJsConnection) {
// await databaseConnection.getDatabase();
} else {
await databaseConnection.connect();
}
} else {
databaseConnection = await this.createDatabaseConnection();
}

const transportConfig = this.createTransportConfigWithAdditionalHeaders({
...this.runtimeConfig.transportLibrary,
Expand All @@ -183,7 +196,7 @@ export abstract class Runtime<TConfig extends RuntimeConfig = RuntimeConfig> {
this.logger.error(`An error was thrown in an event handler of the transport event bus (namespace: '${namespace}'). Root error: ${error}`);
});

this.transport = new Transport(databaseConnection, transportConfig, eventBus, this.loggerFactory);
this.transport = new Transport(databaseConnection, transportConfig, eventBus, this.loggerFactory); // TODO: is it right that a new Transport it created here?

this.logger.debug("Initializing Transport Library...");
await this.transport.init();
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime/test/lib/RuntimeServiceProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { LokiJsConnection } from "@js-soft/docdb-access-loki";
import { MongoDbConnection } from "@js-soft/docdb-access-mongo";
import { AnonymousServices, ConsumptionServices, DataViewExpander, DeciderModuleConfigurationOverwrite, RuntimeConfig, TransportServices } from "../../src";
import { MockEventBus } from "./MockEventBus";
import { TestRuntime } from "./TestRuntime";
Expand All @@ -19,6 +21,7 @@ export interface LaunchConfiguration {
enableAttributeListenerModule?: boolean;
enableNotificationModule?: boolean;
enableDefaultRepositoryAttributes?: boolean;
databaseConnection?: MongoDbConnection | LokiJsConnection; // TODO: this feels very hacky
}

export class RuntimeServiceProvider {
Expand Down Expand Up @@ -70,7 +73,7 @@ export class RuntimeServiceProvider {
return copy;
}

// TODO: where is DB generated? Can I set it to specific Identity's DB?
// TODO: where is DB generated? Can I set it to specific Identity's DB? runtime.transport.databaseConnection, TestRuntimeService.dbConnection
public async launch(count: number, launchConfiguration: LaunchConfiguration = {}): Promise<TestRuntimeServices[]> {
const runtimeServices: TestRuntimeServices[] = [];

Expand All @@ -93,7 +96,7 @@ export class RuntimeServiceProvider {
});
this.runtimes.push(runtime);

await runtime.init();
await runtime.init(launchConfiguration.databaseConnection); // TODO: pass DB connection
await runtime.start();

const services = await runtime.getServices("");
Expand Down
46 changes: 39 additions & 7 deletions packages/runtime/test/modules/DeciderModule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,15 @@ import {
RelationshipTemplateProcessedEvent,
RelationshipTemplateProcessedResult
} from "../../src";
import { RuntimeServiceProvider, TestRequestItem, TestRuntimeServices, establishRelationship, exchangeMessage, expectThrowsAsync } from "../lib";
import {
RuntimeServiceProvider,
TestRequestItem,
TestRuntimeServices,
establishRelationship,
exchangeMessage,
executeFullCreateAndShareRepositoryAttributeFlow,
expectThrowsAsync
} from "../lib";

const runtimeServiceProvider = new RuntimeServiceProvider();

Expand Down Expand Up @@ -547,7 +555,7 @@ describe("DeciderModule", () => {
let sender: TestRuntimeServices;

beforeAll(async () => {
const runtimeServices = await runtimeServiceProvider.launch(1, { enableDeciderModule: true });
const runtimeServices = await runtimeServiceProvider.launch(1, { enableDeciderModule: true, enableRequestModule: true });
sender = runtimeServices[0];
}, 30000);

Expand Down Expand Up @@ -1600,22 +1608,46 @@ describe("DeciderModule", () => {

// TODO: this requires that we can adjust the automationConfig at a later point in time -> stop and start runtime with new config for same identity
test("accepts a DeleteAttributeRequestItem given a DeleteAttributeRequestItemConfig with all fields set", async () => {
let recipient = (await runtimeServiceProvider.launch(1, { enableDeciderModule: true, enableRequestModule: true }))[0];

const testRuntimes = runtimeServiceProvider["runtimes"];
const recipientTestRuntime = testRuntimes[testRuntimes.length - 1];
const recipientDatabaseConnection = recipientTestRuntime["dbConnection"];

await establishRelationship(sender.transport, recipient.transport);
const sharedAttribute = await executeFullCreateAndShareRepositoryAttributeFlow(sender, recipient, {
content: {
value: {
"@type": "GivenName",
value: "Given name of sender"
}
}
});

await recipientTestRuntime.stop();

const deletionDate = CoreDate.utc().add({ days: 1 }).toString();
const deciderConfig: DeciderModuleConfigurationOverwrite = {
automationConfig: [
{
requestConfig: {
"content.item.@type": "DeleteAttributeRequestItem",
"content.item.attributeId": ""
"content.item.attributeId": sharedAttribute.id
},
responseConfig: {
accept: true,
deletionDate: ""
deletionDate: deletionDate
}
}
]
};
const recipient = (await runtimeServiceProvider.launch(1, { enableDeciderModule: true, configureDeciderModule: deciderConfig }))[0];
await establishRelationship(sender.transport, recipient.transport);
recipient = (
await runtimeServiceProvider.launch(1, {
enableDeciderModule: true,
configureDeciderModule: deciderConfig,
databaseConnection: recipientDatabaseConnection
})
)[0];

const message = await exchangeMessage(sender.transport, recipient.transport);
const receivedRequestResult = await recipient.consumption.incomingRequests.received({
Expand All @@ -1625,7 +1657,7 @@ describe("DeciderModule", () => {
{
"@type": "DeleteAttributeRequestItem",
mustBeAccepted: true,
attributeId: ""
attributeId: sharedAttribute.id
}
]
},
Expand Down

0 comments on commit 71e8514

Please sign in to comment.