Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core-manager): implement events listener and events database #3754

Merged
merged 13 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions __tests__/unit/core-watcher/database-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import "jest-extended";

import { Container } from "@arkecosystem/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { DatabaseService } from "@packages/core-watcher/src/database-service";
import { existsSync } from "fs-extra";
import { dirSync, setGracefulCleanup } from "tmp";

let sandbox: Sandbox;
let database: DatabaseService;
let storagePath: string;

beforeEach(() => {
sandbox = new Sandbox();

storagePath = dirSync().name + "/events.sqlite";

sandbox.app.bind(Container.Identifiers.WatcherDatabaseService).to(DatabaseService).inSingletonScope();

sandbox.app.bind(Container.Identifiers.PluginConfiguration).toConstantValue({
getRequired: jest.fn().mockReturnValue(storagePath),
});

database = sandbox.app.get(Container.Identifiers.WatcherDatabaseService);
});

afterEach(() => {
setGracefulCleanup();
});

describe("DatabaseService", () => {
describe("Boot", () => {
it("should boot and create file", async () => {
database.boot();

expect(existsSync(storagePath)).toBeTrue();
});
});

describe("Dispose", () => {
it("should dispose", async () => {
database.boot();
database.dispose();

expect(() => {
database.addEvent("dummy_event", { data: "dummy_data" });
}).toThrowError();
});
});

describe("Flush", () => {
it("should dispose", async () => {
database.boot();

database.addEvent("dummy_event", { data: "dummy_data" });

expect(database.getAllEvents().length).toBe(1);

database.flush();

expect(database.getAllEvents().length).toBe(0);
});
});

describe("AddEvent", () => {
it("should add event", async () => {
database.boot();
expect(existsSync(storagePath)).toBeTrue();

database.addEvent("dummy_event", { data: "dummy_data" });

const result = database.getAllEvents();

expect(result).toBeArray();
expect(result[0]).toMatchObject({
id: 1,
event: "dummy_event",
data: { data: "dummy_data" },
});

expect(result[0].timestamp).toBeDefined();
});
});

describe("Query", () => {
beforeEach(() => {
database.boot();

for (let i = 0; i < 100; i++) {
database.addEvent("dummy_event", { data: "dummy_data" });
database.addEvent("another_dummy_event", { data: "another_dummy_data" });
}
});

it("should return limit 10", async () => {
const result = database.queryEvents();

expect(result.total).toBe(200);
expect(result.limit).toBe(10);
expect(result.offset).toBe(0);
expect(result.data).toBeArray();
expect(result.data.length).toBe(10);
});

it("should return limit 10 with offset", async () => {
const result = database.queryEvents({ offset: 10 });

expect(result.total).toBe(200);
expect(result.limit).toBe(10);
expect(result.offset).toBe(10);
expect(result.data).toBeArray();
expect(result.data.length).toBe(10);
});

it("should return limit 20", async () => {
const result = database.queryEvents({ limit: 20 });

expect(result.total).toBe(200);
expect(result.limit).toBe(20);
expect(result.offset).toBe(0);
expect(result.data).toBeArray();
expect(result.data.length).toBe(20);
});

it("should return events with name", async () => {
const result = database.queryEvents({ limit: 1000, event: "dummy_event" });

expect(result.total).toBe(100);
expect(result.limit).toBe(1000);
expect(result.offset).toBe(0);
expect(result.data).toBeArray();
expect(result.data.length).toBe(100);
});
});
});
43 changes: 43 additions & 0 deletions __tests__/unit/core-watcher/listener.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import "jest-extended";

import { Container } from "@arkecosystem/core-kernel";
import { Sandbox } from "@packages/core-test-framework";
import { Listener } from "@packages/core-watcher/src/listener";

let sandbox: Sandbox;
let listener: Listener;
const mockDatabase = {
addEvent: jest.fn(),
};

let handler;

const mockEventDispatcher = {
listen: jest.fn().mockImplementation((event, cb) => {
handler = cb;
}),
};

beforeEach(() => {
sandbox = new Sandbox();

sandbox.app.bind(Container.Identifiers.WatcherDatabaseService).toConstantValue(mockDatabase);
sandbox.app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(mockEventDispatcher);
sandbox.app.bind(Container.Identifiers.WatcherEventListener).to(Listener).inSingletonScope();

listener = sandbox.app.get(Container.Identifiers.WatcherEventListener);
});

afterEach(() => {});

describe("Listener", () => {
describe("Boot", () => {
it("should boot and save event on emit", async () => {
listener.boot();

handler.handle({ name: "dummy_event", data: "dummy_data" });

expect(mockDatabase.addEvent).toHaveBeenCalledTimes(1);
});
});
});
73 changes: 73 additions & 0 deletions __tests__/unit/core-watcher/service-provider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import "jest-extended";

import { Application, Container, Providers } from "@packages/core-kernel";
import { ServiceProvider } from "@packages/core-watcher/src/service-provider";
import { dirSync, setGracefulCleanup } from "tmp";

let app: Application;

const logger = {
info: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
};

const setPluginConfiguration = (app: Application, serviceProvider: ServiceProvider, configuration: any) => {
const pluginConfiguration = app.get<Providers.PluginConfiguration>(Container.Identifiers.PluginConfiguration);
const instance: Providers.PluginConfiguration = pluginConfiguration.from("core-watcher", configuration);

serviceProvider.setConfig(instance);
};

const mockEventDispatcher = {
listen: jest.fn(),
};

beforeEach(() => {
app = new Application(new Container.Container());

app.bind(Container.Identifiers.LogService).toConstantValue(logger);
app.bind(Container.Identifiers.PluginConfiguration).to(Providers.PluginConfiguration).inSingletonScope();
app.bind(Container.Identifiers.EventDispatcherService).toConstantValue(mockEventDispatcher);
app.bind(Container.Identifiers.FilesystemService).toConstantValue({});
});

afterEach(() => {
setGracefulCleanup();
});

describe("ServiceProvider", () => {
let serviceProvider: ServiceProvider;

beforeEach(() => {
serviceProvider = app.resolve<ServiceProvider>(ServiceProvider);
});

it("should register", async () => {
const usedDefaults = { storage: dirSync().name + "/events.sqlite" };
setPluginConfiguration(app, serviceProvider, usedDefaults);

await expect(serviceProvider.register()).toResolve();
});

it("should boot", async () => {
const usedDefaults = { storage: dirSync().name + "/events.sqlite" };
setPluginConfiguration(app, serviceProvider, usedDefaults);

await expect(serviceProvider.register()).toResolve();
await expect(serviceProvider.boot()).toResolve();
});

it("should dispose", async () => {
const usedDefaults = { storage: dirSync().name + "/events.sqlite" };
setPluginConfiguration(app, serviceProvider, usedDefaults);

await expect(serviceProvider.register()).toResolve();
await expect(serviceProvider.boot()).toResolve();
await expect(serviceProvider.dispose()).toResolve();
});

it("should not be required", async () => {
await expect(serviceProvider.required()).resolves.toBeFalse();
});
});
4 changes: 4 additions & 0 deletions packages/core-kernel/src/ioc/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ export const Identifiers = {
// Registries
TransactionHandlerRegistry: Symbol.for("Registry<TransactionHandler>"),
TransactionHandlerProvider: Symbol.for("Provider<TransactionHandler>"),

// Watcher
WatcherEventListener: Symbol.for("Watcher<EventListener>"),
WatcherDatabaseService: Symbol.for("Watcher<DatabaseService>"),
};
7 changes: 7 additions & 0 deletions packages/core-watcher/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Path-based git attributes
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html

# Ignore all test and documentation with "export-ignore".
/.gitattributes export-ignore
/.gitignore export-ignore
/README.md export-ignore
21 changes: 21 additions & 0 deletions packages/core-watcher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ARK Core - Watcher

<p align="center">
<img src="https://raw.githubusercontent.com/ARKEcosystem/core/master/banner.png" />
</p>

## Documentation

You can find installation instructions and detailed instructions on how to use this package at the [dedicated documentation site](https://ark.io/documentation).

## Security

If you discover a security vulnerability within this package, please send an e-mail to security@ark.io. All security vulnerabilities will be promptly addressed.

## Credits

This project exists thanks to all the people who [contribute](../../../../contributors).

## License

[MIT](LICENSE) © [ARK Ecosystem](https://ark.io)
41 changes: 41 additions & 0 deletions packages/core-watcher/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@arkecosystem/core-watcher",
"version": "3.0.0-next.0",
"description": "Manager for ARK Core",
"license": "MIT",
"contributors": [
"Sebastijan Kuzner <sebastijan@ark.io>"
],
"files": [
"dist"
],
"main": "dist/index",
"types": "dist/index",
"scripts": {
"build": "yarn clean && yarn compile",
"build:watch": "yarn clean && yarn compile -w",
"build:docs": "../../node_modules/typedoc/bin/typedoc --out docs src",
"clean": "del dist",
"compile": "../../node_modules/typescript/bin/tsc",
"prepublishOnly": "yarn build",
"pretest": "bash ../../scripts/pre-test.sh"
},
"dependencies": {
"@arkecosystem/core-kernel": "^3.0.0-next.0",
"better-sqlite3": "^7.0.0"
},
"devDependencies": {
"nock": "^12.0.3"
},
"engines": {
"node": ">=10.x"
},
"publishConfig": {
"access": "public"
},
"arkecosystem": {
"core": {
"alias": "watcher"
}
}
}
Loading