change files structure
This commit is contained in:
parent
3210e72b22
commit
1ed6449ac6
30 changed files with 7 additions and 7 deletions
0
server/test/context/test.txt
Normal file
0
server/test/context/test.txt
Normal file
72
server/test/core/test_core.ts
Normal file
72
server/test/core/test_core.ts
Normal file
|
@ -0,0 +1,72 @@
|
|||
import { delay } from "../../src/core/helper/delay.js";
|
||||
import { Result } from "../../src/core/helper/result";
|
||||
import { TypedEvent } from "../../src/core/helper/typed_event.js";
|
||||
|
||||
export class TestCore {
|
||||
allTests = 0;
|
||||
testErr = 0;
|
||||
testOk = 0;
|
||||
private static _instance: TestCore;
|
||||
|
||||
public static get instance() {
|
||||
return this._instance || (this._instance = new this());
|
||||
}
|
||||
|
||||
assert = (test: boolean, testName: string) => {
|
||||
this.allTests += 1;
|
||||
if (test) {
|
||||
console.log("\x1b[32m", "✅ - " + testName);
|
||||
this.testOk += 1;
|
||||
return;
|
||||
}
|
||||
this.testErr += 1;
|
||||
console.log("\x1b[31m", "❌ - " + testName);
|
||||
};
|
||||
|
||||
testResult = () => {
|
||||
console.log("\x1b[32m", "=============");
|
||||
|
||||
if (this.allTests - this.testOk === 0) {
|
||||
console.log(
|
||||
"\x1b[32m",
|
||||
`✅ All test success! ${this.allTests}/${this.testOk}`
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (this.testErr !== 0) {
|
||||
console.log("\x1b[31m", "❌ test error:" + String(this.testErr));
|
||||
console.log("\x1b[32m", `✅ test success! ${this.testOk}`);
|
||||
}
|
||||
};
|
||||
resultTest = async (
|
||||
eventClass: TypedEvent<Result<any, any>> | any,
|
||||
args: any,
|
||||
testName: string,
|
||||
isOk: boolean,
|
||||
delayTime = 1000
|
||||
) => {
|
||||
let testIsOk = false;
|
||||
eventClass.call(...args);
|
||||
const listener = eventClass.on(
|
||||
(e: {
|
||||
fold: (arg0: (_s: any) => void, arg1: (_e: any) => void) => void;
|
||||
}) => {
|
||||
e.fold(
|
||||
() => {
|
||||
if (isOk) {
|
||||
testIsOk = true;
|
||||
}
|
||||
},
|
||||
() => {
|
||||
if (!isOk) {
|
||||
testIsOk = true;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
await delay(delayTime);
|
||||
this.assert(testIsOk, testName);
|
||||
listener.dispose();
|
||||
};
|
||||
}
|
96
server/test/executor_program_service_test.ts
Normal file
96
server/test/executor_program_service_test.ts
Normal file
|
@ -0,0 +1,96 @@
|
|||
import { delay } from "../src/core/helper/delay.js";
|
||||
import { EXEC_TYPE } from "../src/core/model/exec_error_model.js";
|
||||
import { ExecutorResult } from "../src/core/model/executor_result.js";
|
||||
import { ExecutorProgramService } from "../src/core/services/executor_program_service.js";
|
||||
import { TestCore } from "./core/test_core.js";
|
||||
import { resultTest as resultTest, __dirname } from "./test.js";
|
||||
import { Worker } from "node:cluster";
|
||||
|
||||
|
||||
|
||||
export class ExecutorProgramServiceTest extends ExecutorProgramService {
|
||||
timeCancel = 1000;
|
||||
public test = async () => {
|
||||
await this.resultsTests();
|
||||
await this.longTimeCancelTest()
|
||||
await this.logWriteAndEventEndTestTypeExec()
|
||||
await this.logWriteAndEventEndTypeSpawn()
|
||||
};
|
||||
private async logWriteAndEventEndTypeSpawn(){
|
||||
const executorProgramService = await new ExecutorProgramService(__dirname + '/')
|
||||
executorProgramService.call(EXEC_TYPE.SPAWN, 'node',['./mocks/log_code.js'])
|
||||
const test = TestCore.instance
|
||||
let testIsOk = false
|
||||
let logEvent = false
|
||||
|
||||
executorProgramService.on((e) =>{
|
||||
if(e.isSuccess()) {
|
||||
const executorResult = e.value as ExecutorResult
|
||||
if(logEvent == false){
|
||||
logEvent = executorResult.data != null && executorResult.data != undefined
|
||||
}
|
||||
testIsOk = executorResult.event == 'END' && logEvent
|
||||
}
|
||||
})
|
||||
await delay(8000)
|
||||
test.assert(testIsOk,'ExecutorProgramService EXEC_TYPE.SPAWN end event and log write')
|
||||
}
|
||||
private async logWriteAndEventEndTestTypeExec(){
|
||||
const executorProgramService = await new ExecutorProgramService(__dirname)
|
||||
executorProgramService.call(EXEC_TYPE.EXEC, 'node ./test/mocks/log_code.js' )
|
||||
const test = TestCore.instance
|
||||
executorProgramService.on((e) =>{
|
||||
if(e.isSuccess()) {
|
||||
const executorResult = e.value as ExecutorResult
|
||||
test.assert(executorResult.data != undefined && executorResult.event == 'END','ExecutorProgramService EXEC_TYPE.EXEC end event and log write')
|
||||
}
|
||||
})
|
||||
await delay(7000)
|
||||
}
|
||||
private async longTimeCancelTest(){
|
||||
const executorProgramService = await new ExecutorProgramService('',1000)
|
||||
executorProgramService.call(EXEC_TYPE.EXEC, 'node ./test/mocks/long_code.js' )
|
||||
await delay(1500)
|
||||
const worker = executorProgramService.worker as Worker
|
||||
const test = TestCore.instance
|
||||
test.assert(worker.isDead(),'ExecutorProgramService long time cancel')
|
||||
|
||||
}
|
||||
private resultsTests = async () => {
|
||||
await resultTest(
|
||||
new ExecutorProgramService(__dirname),
|
||||
[EXEC_TYPE.EXEC, "node ./mocks/error.js"],
|
||||
"ExecutorProgramService EXEC_TYPE.EXEC on Result.error",
|
||||
false,
|
||||
2000
|
||||
);
|
||||
await delay(400)
|
||||
await resultTest(
|
||||
new ExecutorProgramService(__dirname),
|
||||
[EXEC_TYPE.EXEC, "ls"],
|
||||
"ExecutorProgramService EXEC_TYPE.EXEC on Result.ok",
|
||||
true
|
||||
);
|
||||
|
||||
await resultTest(
|
||||
new ExecutorProgramService(__dirname),
|
||||
[EXEC_TYPE.SPAWN, "ls"],
|
||||
"ExecutorProgramService EXEC_TYPE.SPAWN on Result.ok",
|
||||
true
|
||||
);
|
||||
await resultTest(
|
||||
new ExecutorProgramService(__dirname),
|
||||
[EXEC_TYPE.SPAWN, "python3 ./mocks/s.js"],
|
||||
"ExecutorProgramService EXEC_TYPE.SPAWN on Result.error",
|
||||
false,
|
||||
2000
|
||||
);
|
||||
await resultTest(
|
||||
new ExecutorProgramService(__dirname),
|
||||
[EXEC_TYPE.SPAWN, "ls"],
|
||||
"ExecutorProgramService EXEC_TYPE.SPAWN on Result.ok",
|
||||
true,
|
||||
2000
|
||||
);
|
||||
};
|
||||
}
|
86
server/test/files_change_notifier_service_test.ts
Normal file
86
server/test/files_change_notifier_service_test.ts
Normal file
|
@ -0,0 +1,86 @@
|
|||
import { delay } from "../src/core/helper/delay.js";
|
||||
import { EventsFileChanger } from "../src/core/model/meta_data_file_manager_model.js";
|
||||
import { FilesChangeNotifierService } from "../src/core/services/files_change_notifier_service.js";
|
||||
import { assert, __dirname } from "./test.js";
|
||||
import * as fs from "fs";
|
||||
|
||||
export class FilesChangerTest extends FilesChangeNotifierService {
|
||||
directory = __dirname + "/context/";
|
||||
data = () => {
|
||||
return "This is a file containing a collection";
|
||||
};
|
||||
filePath = this.directory + "programming.txt";
|
||||
|
||||
public hashUnitEqualTo(hashEqualValue: EventsFileChanger, testName: string) {
|
||||
let testIsOk = false;
|
||||
|
||||
for (const [_key, value] of Object.entries(this.hashes)) {
|
||||
if ((value.event === hashEqualValue, testName)) {
|
||||
testIsOk = true;
|
||||
}
|
||||
}
|
||||
assert(testIsOk, testName);
|
||||
}
|
||||
public async createFile() {
|
||||
this.call();
|
||||
await delay(2000);
|
||||
fs.writeFileSync(this.filePath, this.data());
|
||||
await delay(1000);
|
||||
this.hashUnitEqualTo(
|
||||
EventsFileChanger.create,
|
||||
"FilesChangeNotifierService create file"
|
||||
);
|
||||
|
||||
this.cancel();
|
||||
}
|
||||
public async updateFile() {
|
||||
this.call();
|
||||
fs.writeFileSync(this.filePath, this.data() + "132321");
|
||||
await delay(1000);
|
||||
fs.writeFileSync(this.filePath, this.data() + "132");
|
||||
await delay(500);
|
||||
this.hashUnitEqualTo(
|
||||
EventsFileChanger.update,
|
||||
"FilesChangeNotifierService update file"
|
||||
);
|
||||
this.cancel();
|
||||
}
|
||||
public async initFile() {
|
||||
this.init();
|
||||
await delay(500);
|
||||
this.hashUnitEqualTo(
|
||||
EventsFileChanger.static,
|
||||
"FilesChangeNotifierService init file"
|
||||
);
|
||||
}
|
||||
public async deleteFile() {
|
||||
this.call();
|
||||
fs.unlinkSync(this.filePath);
|
||||
await delay(1000);
|
||||
this.hashUnitEqualTo(
|
||||
EventsFileChanger.delete,
|
||||
"FilesChangeNotifierService delete file"
|
||||
);
|
||||
this.cancel();
|
||||
}
|
||||
public async notExistsDirectory() {
|
||||
await delay(1000);
|
||||
this.directory = "";
|
||||
const result = this.call();
|
||||
assert(result.isFailure(), "Not exists directory");
|
||||
this.directory = __dirname + "/context/";
|
||||
}
|
||||
public async test() {
|
||||
await this.createFile();
|
||||
await this.updateFile()
|
||||
await this.initFile()
|
||||
await this.deleteFile()
|
||||
await this.notExistsDirectory()
|
||||
await this.testClear();
|
||||
}
|
||||
public testClear() {
|
||||
if (fs.existsSync(this.filePath)) {
|
||||
fs.unlinkSync(this.filePath);
|
||||
}
|
||||
}
|
||||
}
|
17
server/test/mocks/log_code.ts
Normal file
17
server/test/mocks/log_code.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
|
||||
|
||||
setTimeout(() =>{
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
console.log('log')
|
||||
},2000)
|
||||
setTimeout(() =>{
|
||||
console.log('log end')
|
||||
}, 5000)
|
4
server/test/mocks/long_code.ts
Normal file
4
server/test/mocks/long_code.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
const seconds = 1000 * 10
|
||||
setTimeout(()=>{
|
||||
console.log(200)
|
||||
}, seconds)
|
47
server/test/test.ts
Normal file
47
server/test/test.ts
Normal file
|
@ -0,0 +1,47 @@
|
|||
import locator from "../src/core/di/register_di.js";
|
||||
import { UnitTestEnv } from "../src/core/di/env.js";
|
||||
import { fileURLToPath } from "url";
|
||||
import { dirname } from "path";
|
||||
import { ExecutorProgramServiceTest } from "./executor_program_service_test.js";
|
||||
import { FilesChangerTest } from "./files_change_notifier_service_test.js";
|
||||
import { TestCore } from "./core/test_core.js";
|
||||
import "reflect-metadata";
|
||||
import { CalculationProcess, IssueType, ProcessMetaData, ProcessType, TriggerType } from "../src/core/model/process_model.js";
|
||||
|
||||
const testCore = TestCore.instance;
|
||||
const __filename: string = fileURLToPath(import.meta.url);
|
||||
|
||||
export const __dirname: string = dirname(__filename);
|
||||
export const assert = testCore.assert;
|
||||
export const resultTest = testCore.resultTest;
|
||||
const env = new UnitTestEnv(__dirname);
|
||||
|
||||
locator(env);
|
||||
|
||||
const main = async () => {
|
||||
await new ExecutorProgramServiceTest(__dirname).test();
|
||||
await new FilesChangerTest(__dirname).test()
|
||||
await testCore.testResult();
|
||||
};
|
||||
|
||||
main();
|
||||
// const logProcess:ProcessMetaData = {
|
||||
// process: {
|
||||
// type: ProcessType.EXEC,
|
||||
// command: `nix-shell -p 'python39.withPackages(ps: with ps; [ ])' --run 'python3 p.py 1'`,
|
||||
// isGenerating: false,
|
||||
// isLocaleCode: false,
|
||||
// issueType: IssueType.WARNING,
|
||||
// timeout: 10000000,
|
||||
// },
|
||||
|
||||
// trigger: {
|
||||
// type: TriggerType.PROCESS,
|
||||
// value: 'code',
|
||||
// },
|
||||
// env: null,
|
||||
|
||||
// }
|
||||
// const calculationProcess = new CalculationProcess([])
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue