Skip to content

Commit

Permalink
Exposed resumable task store in config
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Jul 10, 2024
1 parent bcd669b commit c25f790
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const config = {
messageStore: process.env.DWN_STORAGE_MESSAGES || process.env.DWN_STORAGE || 'level://data',
dataStore: process.env.DWN_STORAGE_DATA || process.env.DWN_STORAGE || 'level://data',
eventLog: process.env.DWN_STORAGE_EVENTS || process.env.DWN_STORAGE || 'level://data',
resumableTaskStore: process.env.DWN_STORAGE_RESUMABLE_TASKS || process.env.DWN_STORAGE || 'level://data',

// tenant registration feature configuration
registrationStoreUrl: process.env.DWN_REGISTRATION_STORE_URL || process.env.DWN_STORAGE,
Expand Down
2 changes: 1 addition & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function getDwnConfig(
const dataStore: DataStore = await getStore(config.dataStore, StoreType.DataStore);
const eventLog: EventLog = await getStore(config.eventLog, StoreType.EventLog);
const messageStore: MessageStore = await getStore(config.messageStore, StoreType.MessageStore);
const resumableTaskStore: ResumableTaskStore = await getStore(config.messageStore, StoreType.ResumableTaskStore);
const resumableTaskStore: ResumableTaskStore = await getStore(config.resumableTaskStore, StoreType.ResumableTaskStore);

return { didResolver, eventStream, eventLog, dataStore, messageStore, resumableTaskStore, tenantGate };
}
Expand Down
26 changes: 26 additions & 0 deletions tests/plugins/resumable-task-store-sqlite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ResumableTaskStore } from "@tbd54566975/dwn-sdk-js";
import { ResumableTaskStoreSql } from "@tbd54566975/dwn-sql-store";
import { getDialectFromUrl } from "../../src/storage.js";

/**
* An example of a plugin that is used for testing.
* The points to note are:
* - The class must be a default export.
* - The constructor must not take any arguments.
*/
export default class ResumableTaskStoreSqlite extends ResumableTaskStoreSql implements ResumableTaskStore {
constructor() {
const sqliteDialect = getDialectFromUrl(new URL('sqlite://'));
super(sqliteDialect);

// NOTE: the following line is added purely to test the constructor invocation.
ResumableTaskStoreSqlite.spyingTheConstructor();
}

/**
* NOTE: This method is introduced purely to indirectly test/spy invocation of the constructor.
* As I was unable to find an easy way to directly spy the constructor.
*/
public static spyingTheConstructor(): void {
}
}
11 changes: 8 additions & 3 deletions tests/scenarios/dynamic-plugin-loading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { DwnServer } from '../../src/dwn-server.js';

import { DidDht, DidKey, UniversalResolver } from '@web5/dids';
import CommonScenarioValidator from '../common-scenario-validator.js';
import MessageStoreSqlite from '../plugins/message-store-sqlite.js';
import ResumableTaskStoreSqlite from '../plugins/resumable-task-store-sqlite.js';

// node.js 18 and earlier needs globalThis.crypto polyfill
if (!globalThis.crypto) {
Expand All @@ -23,7 +25,6 @@ describe('Dynamic DWN plugin loading', function () {
let dwnServer: DwnServer;

afterEach(async () => {
// clock.restore();
if (dwnServer !== undefined) {
await dwnServer.stop();
}
Expand All @@ -44,7 +45,9 @@ describe('Dynamic DWN plugin loading', function () {
// 3. Validate that the DWN instance is using the custom data store plugin.

// NOTE: was not able to spy on constructor directly, so spying on a method that is called in the constructor
const customMessageStoreConstructorSpy = sinon.spy(MessageStoreSqlite, 'spyingTheConstructor');
const customDataStoreConstructorSpy = sinon.spy(DataStoreSqlite, 'spyingTheConstructor');
const customResumableTaskStoreConstructorSpy = sinon.spy(ResumableTaskStoreSqlite, 'spyingTheConstructor');
const customEventLogConstructorSpy = sinon.spy(EventLogSqlite, 'spyingTheConstructor');
const customEventStreamConstructorSpy = sinon.spy(EventStreamInMemory, 'spyingTheConstructor');

Expand All @@ -55,9 +58,9 @@ describe('Dynamic DWN plugin loading', function () {
// The default config is not reliable because other tests modify it.
dwnServerConfigCopy.registrationStoreUrl = undefined; // allow all traffic

// dwnServerConfigCopy.messageStore = '../tests/plugins/message-store-sqlite.js'; // not working
dwnServerConfigCopy.messageStore = 'sqlite://';
dwnServerConfigCopy.messageStore = '../tests/plugins/message-store-sqlite.js';
dwnServerConfigCopy.dataStore = '../tests/plugins/data-store-sqlite.js';
dwnServerConfigCopy.resumableTaskStore = '../tests/plugins/resumable-task-store-sqlite.js';
dwnServerConfigCopy.eventLog = '../tests/plugins/event-log-sqlite.js';
dwnServerConfigCopy.eventStreamPluginPath = '../tests/plugins/event-stream-in-memory.js';

Expand All @@ -72,7 +75,9 @@ describe('Dynamic DWN plugin loading', function () {
});
dwnServer = new DwnServer({ config: dwnServerConfigCopy, didResolver });
await dwnServer.start();
expect(customMessageStoreConstructorSpy.calledOnce).to.be.true;
expect(customDataStoreConstructorSpy.calledOnce).to.be.true;
expect(customResumableTaskStoreConstructorSpy.calledOnce).to.be.true;
expect(customEventLogConstructorSpy.calledOnce).to.be.true;
expect(customEventStreamConstructorSpy.calledOnce).to.be.true;

Expand Down
1 change: 1 addition & 0 deletions tests/scenarios/registration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('Registration scenarios', function () {
// and dwn-server.spec.ts already uses LevelDB.
dwnServerConfig.messageStore = 'sqlite://',
dwnServerConfig.dataStore = 'sqlite://',
dwnServerConfig.resumableTaskStore = 'sqlite://',
dwnServerConfig.eventLog = 'sqlite://',

// registration config
Expand Down

0 comments on commit c25f790

Please sign in to comment.