Skip to content

Commit

Permalink
feat: move some services from @webda/core to @webda/runtime (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Dec 20, 2023
1 parent 3b4471f commit bf78ca9
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 36 deletions.
32 changes: 15 additions & 17 deletions packages/core/src/index.ts
Expand Up @@ -11,48 +11,46 @@ export * from "./errors";
export * from "./unpackedapplication";

// Models
export * from "./models/aclmodel";
export * from "./models/aclmodel"; // move to runtime
export * from "./models/coremodel";
export * from "./models/deployment";
export * from "./models/ident";
export * from "./models/ownermodel";
export * from "./models/ownermodel"; // move to runtime
export * from "./models/relations";
export * from "./models/rolemodel";
export * from "./models/rolemodel"; // move to runtime
export * from "./models/user";

// Queues
export * from "./queues/filequeue";
export * from "./queues/memoryqueue";
export * from "./queues/filequeue"; // move to runtime
export * from "./queues/memoryqueue"; // move to runtime ?
export * from "./queues/pubsubservice";
export * from "./queues/queueservice";

// Services
export * from "./services/asyncevents";
export * from "./services/authentication";
export * from "./services/binary";
export * from "./services/cloudbinary";
export * from "./services/cloudbinary"; // move to runtime
export * from "./services/configuration";
export * from "./services/cron";
export * from "./services/cryptoservice";
export * from "./services/debugmailer";
export * from "./services/domainservice";
export * from "./services/echo";
export * from "./services/filebinary";
export * from "./services/domainservice"; // move rest to runtime
export * from "./services/filebinary"; // move to runtime
export * from "./services/fileconfiguration";
export * from "./services/invitationservice";
export * from "./services/kubernetesconfiguration";
export * from "./services/invitationservice"; // move to runtime
export * from "./services/kubernetesconfiguration"; // move to runtime
export * from "./services/mailer";
export * from "./services/notificationservice";
export * from "./services/oauth";
export * from "./services/proxy";
export * from "./services/resource";
export * from "./services/proxy"; // move to runtime
export * from "./services/resource"; // move to runtime
export * from "./services/service";
export * from "./services/version";

// Stores
export * from "./stores/aggregator";
export * from "./stores/aliasstore";
export * from "./stores/file";
export * from "./stores/aggregator"; // move to runtime
export * from "./stores/aliasstore"; // move to runtime
export * from "./stores/file"; // move to runtime
export * from "./stores/mapper";
export * from "./stores/memory";
export * from "./stores/store";
Expand Down
14 changes: 0 additions & 14 deletions packages/core/src/services/version.spec.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/runtime/src/index.ts
@@ -1,2 +1,5 @@
export * from "./models/binarymodel";
export * from "./services/cluster";
export * from "./services/echo";
export * from "./services/version";
export * from "./stores/migration";
141 changes: 141 additions & 0 deletions packages/runtime/src/services/cluster.spec.ts
@@ -0,0 +1,141 @@
import { suite, test } from "@testdeck/mocha";
import { Service } from "@webda/core";
import { WebdaSimpleTest } from "@webda/core/lib/test";
import * as assert from "assert";
import Sinon from "sinon";
import { ClusterService } from "./cluster";

class FakePubSub extends Service {
consume() {}
sendMessage() {}

getClientEvents(): string[] {
return ["plop", "Store.PartialUpdated"];
}
}

@suite
class ClusterServiceTest extends WebdaSimpleTest {
service: ClusterService;
pubsub: FakePubSub;

@test
async test() {
console.log(Object.keys(this.webda.getModels()));
let CoreModel = this.webda.getModels()["Webda/CoreModel"];
CoreModel.getClientEvents = () => ["test"];
this.pubsub = this.registerService(await new FakePubSub(this.webda, "PubSub", {}).resolve().init());
this.service = this.registerService(
await new ClusterService(this.webda, "Cluster", { keepAlive: 10 }).resolve().init()
);
assert.strictEqual(this.service.ready(), false);
let ctx = await this.newContext();
await this.service.readyEndpoint(ctx);
assert.strictEqual(ctx.statusCode, 503);

// Test update cluster
this.service.members["test"] = { lastSeen: 10 };
this.service.members["test2"] = { lastSeen: Date.now() };
await this.service.updateCluster();
assert.strictEqual(Object.keys(this.service.getMembers()).length, 2);

this.webda.emit("Webda.Init.Services");

await this.webda.getRegistry().save({
test: "ok",
uuid: "plop"
});

await this.sleep(20);
await this.nextTick();
ctx = await this.newContext();
await this.service.readyEndpoint(ctx);
assert.strictEqual(ctx.statusCode, 200);

// Emit
CoreModel.emit(<any>"test", undefined);
this.pubsub.emit("plop", {});
// Hit the force one
this.service.members = {};
this.service.sendMessage({});

// Handle message

let stub = Sinon.stub(this.service, "updateCluster").callsFake(async () => {});
await this.service["handleMessage"]({
emitterId: "testor",
type: "cluster",
event: "ClusterService.MemberRemoved",
emitter: "",
time: Date.now(),
data: {
emitterId: this.service.emitterId
}
});

await this.service["handleMessage"]({
emitterId: this.service.emitterId,
type: "cluster",
event: "ClusterService.MemberRemoved",
emitter: "",
time: Date.now(),
data: {
emitterId: this.service.emitterId
}
});
assert.strictEqual(stub.callCount, 1);
stub.restore();
let logStub = Sinon.stub(this.service, "log").callsFake(() => {});
this.service.models["fake"] = <any>{
emit: Sinon.stub().callsFake(() => {})
};
this.service.services["fake"] = <any>{
emit: Sinon.stub().callsFake(() => {})
};
this.service.stores["fake"] = <any>{
emitStoreEvent: Sinon.stub().callsFake(() => {})
};

// Test the out of sync
for (let type of ["Model", "Store", "Service"]) {
await this.service["handleMessage"]({
emitterId: "testor",
type: <any>type.toLowerCase(),
event: "",
emitter: "unknown",
time: Date.now(),
data: {}
});
assert.deepStrictEqual(logStub.args[0], ["WARN", `${type} not found unknown - code is probably out of sync`]);
if (type !== "Store") {
await this.service["handleMessage"]({
emitterId: "testor",
type: <any>type.toLowerCase(),
event: "Test",
emitter: "fake",
time: Date.now(),
data: { plop: true }
});
assert.deepStrictEqual(this.service[type.toLowerCase() + "s"]["fake"].emit.args[0], [
"Test",
{ emitterId: "testor", plop: true }
]);
}
logStub.resetHistory();
}
await this.service["handleMessage"]({
emitterId: "testor",
type: "store",
event: "Test",
emitter: "fake",
time: Date.now(),
data: { plop: true }
});
console.log(this.service.stores["fake"].emitStoreEvent);
// @ts-ignore
assert.deepStrictEqual(this.service.stores["fake"].emitStoreEvent.args[0], [
"Test",
{ emitterId: "testor", plop: true }
]);
}
}
@@ -1,11 +1,11 @@
import { suite, test } from "@testdeck/mocha";
import { WebdaSimpleTest } from "@webda/core/lib/test";
import * as assert from "assert";
import * as sinon from "sinon";
import { WebdaTest } from "../test";
import { EchoService } from "./echo";

@suite
class EchoTest extends WebdaTest {
class EchoTest extends WebdaSimpleTest {
@test
async cov() {
let service = new EchoService(this.webda, "test", {
Expand Down
@@ -1,5 +1,4 @@
import { WebContext } from "../utils/context";
import { Service, ServiceParameters } from "./service";
import { Service, ServiceParameters, WebContext } from "@webda/core";

export class EchoServiceParameters extends ServiceParameters {
/**
Expand Down
19 changes: 19 additions & 0 deletions packages/runtime/src/services/version.spec.ts
@@ -0,0 +1,19 @@
import { suite, test } from "@testdeck/mocha";
import { WebdaSimpleTest } from "@webda/core/lib/test";
import * as assert from "assert";
import { VersionService } from "./version";

@suite
class VersionTest extends WebdaSimpleTest {
@test
async normal() {
let ctx = await this.newContext();
let service = await this.registerService(new VersionService(this.webda, "test", {}))
.resolve()
.init();
await service.version(ctx);
//await this.getExecutor(ctx, "webda.io", "GET", "/version").execute(ctx);
// Version within package.json of test/ folder
assert.ok(ctx.getResponseBody().match(/\d+\.\d+\.\d+/) !== null);
}
}
@@ -1,4 +1,4 @@
import { OperationContext, Service, ServiceParameters } from "../index";
import { OperationContext, Service, ServiceParameters } from "@webda/core";

/**
* Version parameters
Expand Down

0 comments on commit bf78ca9

Please sign in to comment.