Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move some services from @webda/core to @webda/runtime (#342)
- Loading branch information
Showing
8 changed files
with
182 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
export * from "./models/binarymodel"; | ||
export * from "./services/cluster"; | ||
export * from "./services/echo"; | ||
export * from "./services/version"; | ||
export * from "./stores/migration"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
]); | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
packages/core/src/services/echo.spec.ts → packages/runtime/src/services/echo.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/core/src/services/echo.ts → packages/runtime/src/services/echo.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/core/src/services/version.ts → packages/runtime/src/services/version.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters