2023-08-12 21:32:02 +00:00
|
|
|
import { tap, expect } from '@push.rocks/tapbundle';
|
|
|
|
import * as smartmongo from '@push.rocks/smartmongo';
|
|
|
|
import type * as taskbuffer from '@push.rocks/taskbuffer';
|
|
|
|
|
|
|
|
import * as smartdata from '../ts/index.js';
|
|
|
|
import { SmartdataDistributedCoordinator, DistributedClass } from '../ts/smartdata.classes.distributedcoordinator.js'; // path might need adjusting
|
|
|
|
const totalInstances = 10;
|
|
|
|
|
|
|
|
// =======================================
|
|
|
|
// Connecting to the database server
|
|
|
|
// =======================================
|
|
|
|
|
|
|
|
let smartmongoInstance: smartmongo.SmartMongo;
|
|
|
|
let testDb: smartdata.SmartdataDb;
|
|
|
|
|
|
|
|
tap.test('should create a testinstance as database', async () => {
|
|
|
|
smartmongoInstance = await smartmongo.SmartMongo.createAndStart();
|
|
|
|
testDb = new smartdata.SmartdataDb(await smartmongoInstance.getMongoDescriptor());
|
|
|
|
await testDb.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should instantiate DistributedClass', async (tools) => {
|
|
|
|
const instance = new DistributedClass();
|
|
|
|
expect(instance).toBeInstanceOf(DistributedClass);
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('DistributedClass should update the time', async (tools) => {
|
|
|
|
const distributedCoordinator = new SmartdataDistributedCoordinator(testDb);
|
|
|
|
await distributedCoordinator.start();
|
|
|
|
const initialTime = distributedCoordinator.ownInstance.data.lastUpdated;
|
|
|
|
await distributedCoordinator.sendHeartbeat();
|
|
|
|
const updatedTime = distributedCoordinator.ownInstance.data.lastUpdated;
|
|
|
|
expect(updatedTime).toBeGreaterThan(initialTime);
|
|
|
|
await distributedCoordinator.stop();
|
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should instantiate SmartdataDistributedCoordinator', async (tools) => {
|
2023-08-14 23:01:16 +00:00
|
|
|
const distributedCoordinator = new SmartdataDistributedCoordinator(testDb);
|
|
|
|
await distributedCoordinator.start();
|
|
|
|
expect(distributedCoordinator).toBeInstanceOf(SmartdataDistributedCoordinator);
|
|
|
|
await distributedCoordinator.stop();
|
2023-08-12 21:32:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('SmartdataDistributedCoordinator should update leader status', async (tools) => {
|
2023-08-14 23:01:16 +00:00
|
|
|
const distributedCoordinator = new SmartdataDistributedCoordinator(testDb);
|
|
|
|
await distributedCoordinator.start();
|
|
|
|
await distributedCoordinator.checkAndMaybeLead();
|
|
|
|
expect(distributedCoordinator.ownInstance.data.elected).toBeOneOf([true, false]);
|
|
|
|
await distributedCoordinator.stop();
|
2023-08-12 21:32:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('SmartdataDistributedCoordinator should handle distributed task requests', async (tools) => {
|
2023-08-14 23:01:16 +00:00
|
|
|
const distributedCoordinator = new SmartdataDistributedCoordinator(testDb);
|
|
|
|
await distributedCoordinator.start();
|
2023-08-12 21:32:02 +00:00
|
|
|
|
|
|
|
const mockTaskRequest: taskbuffer.distributedCoordination.IDistributedTaskRequest = {
|
|
|
|
submitterId: "mockSubmitter12345", // Some unique mock submitter ID
|
2023-08-16 10:08:27 +00:00
|
|
|
requestResponseId: 'uni879873462hjhfkjhsdf', // Some unique ID for the request-response
|
2023-08-12 21:32:02 +00:00
|
|
|
taskName: "SampleTask",
|
|
|
|
taskVersion: "1.0.0", // Assuming it's a version string
|
|
|
|
taskExecutionTime: Date.now(),
|
|
|
|
taskExecutionTimeout: 60000, // Let's say the timeout is 1 minute (60000 ms)
|
|
|
|
taskExecutionParallel: 5, // Let's assume max 5 parallel executions
|
|
|
|
status: 'requesting'
|
|
|
|
};
|
|
|
|
|
2023-08-14 23:01:16 +00:00
|
|
|
const response = await distributedCoordinator.fireDistributedTaskRequest(mockTaskRequest);
|
|
|
|
console.log(response) // based on your expected structure for the response
|
|
|
|
await distributedCoordinator.stop();
|
2023-08-12 21:32:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('SmartdataDistributedCoordinator should update distributed task requests', async (tools) => {
|
2023-08-14 23:01:16 +00:00
|
|
|
const distributedCoordinator = new SmartdataDistributedCoordinator(testDb);
|
2023-08-12 21:32:02 +00:00
|
|
|
|
2023-08-14 23:01:16 +00:00
|
|
|
await distributedCoordinator.start();
|
2023-08-12 21:32:02 +00:00
|
|
|
|
|
|
|
const mockTaskRequest: taskbuffer.distributedCoordination.IDistributedTaskRequest = {
|
|
|
|
submitterId: "mockSubmitter12345", // Some unique mock submitter ID
|
2023-08-15 17:55:22 +00:00
|
|
|
requestResponseId: 'uni879873462hjhfkjhsdf', // Some unique ID for the request-response
|
2023-08-12 21:32:02 +00:00
|
|
|
taskName: "SampleTask",
|
|
|
|
taskVersion: "1.0.0", // Assuming it's a version string
|
|
|
|
taskExecutionTime: Date.now(),
|
|
|
|
taskExecutionTimeout: 60000, // Let's say the timeout is 1 minute (60000 ms)
|
|
|
|
taskExecutionParallel: 5, // Let's assume max 5 parallel executions
|
|
|
|
status: 'requesting'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2023-08-14 23:01:16 +00:00
|
|
|
await distributedCoordinator.updateDistributedTaskRequest(mockTaskRequest);
|
2023-08-12 21:32:02 +00:00
|
|
|
// Here, we can potentially check if a DB entry got updated or some other side-effect of the update method.
|
2023-08-14 23:01:16 +00:00
|
|
|
await distributedCoordinator.stop();
|
2023-08-12 21:32:02 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
tap.test('should elect only one leader amongst multiple instances', async (tools) => {
|
|
|
|
const coordinators = Array.from({ length: totalInstances }).map(() => new SmartdataDistributedCoordinator(testDb));
|
2023-08-14 23:01:16 +00:00
|
|
|
await Promise.all(coordinators.map(coordinator => coordinator.start()));
|
2023-08-12 21:32:02 +00:00
|
|
|
const leaders = coordinators.filter(coordinator => coordinator.ownInstance.data.elected);
|
2023-08-14 23:01:16 +00:00
|
|
|
for (const leader of leaders) {
|
|
|
|
console.log(leader.ownInstance);
|
|
|
|
}
|
2023-08-12 21:32:02 +00:00
|
|
|
expect(leaders.length).toEqual(1);
|
2023-08-14 23:01:16 +00:00
|
|
|
|
|
|
|
// stopping clears a coordinator from being elected.
|
|
|
|
await Promise.all(coordinators.map(coordinator => coordinator.stop()));
|
2023-08-12 21:32:02 +00:00
|
|
|
});
|
|
|
|
|
2023-08-14 23:01:16 +00:00
|
|
|
tap.test('should clean up', async () => {
|
2024-04-13 23:24:21 +00:00
|
|
|
await smartmongoInstance.stopAndDumpToDir(`.nogit/dbdump/test.distributedcoordinator.ts`);
|
2023-08-16 10:08:27 +00:00
|
|
|
setTimeout(() => process.exit(), 2000);
|
2023-08-14 23:01:16 +00:00
|
|
|
})
|
|
|
|
|
2023-08-12 21:32:02 +00:00
|
|
|
tap.start({ throwOnError: true });
|