Skip to content

Commit 39a1c1b

Browse files
committed
unit test cases
1 parent 7f82e3b commit 39a1c1b

File tree

3 files changed

+60
-71
lines changed

3 files changed

+60
-71
lines changed

__tests__/code-excution/python.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ describe("Python transform into executable", () => {
1313
})
1414

1515

16-
describe("Python code excution test", () => {
17-
18-
it('should calculate a frequency in string', () => {
19-
fs.readFile(`${__dirname}/../test-code/python/counter.py`, 'utf8', (_, code) => {
20-
const codeContext: CodeContext = {
21-
code: code,
22-
functionName: "calculate"
23-
}
24-
const worker = new JobWorker("python3", codeContext);
25-
worker
26-
.startContainer()
27-
.then((response: ExecuteContainer) => {
28-
expect(response.codeOutput).toBe("Counter({'a': 2, 's': 2, 'w': 2, 'e': 2, 'l': 1, 'd': 1, 'k': 1})")
29-
worker.cleanupJob();
30-
})
31-
.catch(_ => {});
32-
});
33-
})
34-
})
16+
// TODO needs some work, cannot seem to ready file here, and is being recognized as success
17+
18+
// describe("Python code excution test", () => {
19+
// it('should calculate a frequency in string', () => {
20+
// expect.assertions(1);
21+
22+
// fs.readFile(`${__dirname}/../test-code/python/counter.py`, 'utf8', async (error, code) => {
23+
// const codeContext: CodeContext = {
24+
// code: code,
25+
// functionName: "calculate"
26+
// }
27+
// const worker = new JobWorker("python3", codeContext);
28+
// const response = await worker.startContainer()
29+
// console.log(response)
30+
// expect(response.codeOutput).toBe("Counter({'a': 2, 's': 2, 'w': 2, 'e': 2, 'l': 1, 'd': 1, 'k': 1})")
31+
// await worker.cleanupJob();
32+
// });
33+
// })
34+
// })

__tests__/worker.ts

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,45 @@
1-
import { ContainerInitialization } from "../src/types/worker";
21
import JobWorker from "../src/worker";
32

43
describe("Docker initialization test", () => {
54
it("should create a python container", async () => {
65
const worker = new JobWorker("python3", { code: "", functionName: "" });
7-
worker
8-
.createContainer()
9-
.then(({ error, containerID }) => {
10-
expect(error).toBe(false);
11-
expect(containerID).toBeTruthy;
12-
worker.removeContainer();
13-
})
14-
.catch((_) => {});
6+
const { error, containerID } = await worker.createContainer()
7+
expect(error).toBe(false);
8+
expect(containerID).toBeTruthy;
9+
await worker.removeContainer();
1510
});
1611

1712
// TODO write a tast case for writeFile() & copyContext
1813

1914
it("should not create a container", async () => {
20-
const worker = new JobWorker("invalidName" as any, {
21-
code: "",
22-
functionName: "",
23-
});
24-
worker
25-
.createContainer()
26-
.catch(
27-
({ error, containerID, errorMessage }: ContainerInitialization) => {
28-
expect(error).toBe(true);
29-
expect(containerID).toBeFalsy();
30-
expect(errorMessage).toBeTruthy();
31-
}
32-
);
15+
const worker = new JobWorker("invalidName" as any, { code: "", functionName: ""});
16+
expect.assertions(3);
17+
try {
18+
await worker.createContainer()
19+
} catch ({ error, containerID, errorMessage }: any) {
20+
expect(error).toBe(true);
21+
expect(containerID).toBeFalsy();
22+
expect(errorMessage).toBe("Invalid language name.");
23+
}
3324
});
3425

3526

3627
it("should not initialize a container", async () => {
3728
const worker = new JobWorker("invalidName" as any, { code: "print('Hello World!')", functionName: "" });
38-
worker
39-
.initContainer()
40-
.then(({ error, message }) => {
41-
expect(error).toBe(false);
42-
expect(message).toBe("Job has succedded.");
43-
worker.cleanupJob();
44-
})
45-
.catch((_) => {});
29+
expect.assertions(2);
30+
try {
31+
await worker.initContainer()
32+
} catch ({ error, message }: any) {
33+
expect(error).toEqual(true)
34+
expect(message).toEqual("Invalid language name.")
35+
}
4636
});
4737

4838
it("should initialize a python container with code in it", async () => {
4939
const worker = new JobWorker("python3", { code: "print('Hello World!')", functionName: "" });
50-
worker
51-
.initContainer()
52-
.then(({ error, message }) => {
53-
expect(error).toBe(false)
54-
expect(message).toBeTruthy;
55-
expect(message).toBe("Job has succedded.");
56-
worker.cleanupJob();
57-
})
58-
.catch((_) => {});
40+
const { error, message } = await worker.initContainer()
41+
expect(error).toBe(false)
42+
expect(message).toBe("Job has succeeded.");
43+
worker.cleanupJob();
5944
});
6045
});

src/worker.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ class JobWorker {
3838
*/
3939
createContainer(): Promise<ContainerInitialization> {
4040
return new Promise((resolve, reject) => {
41-
const initCommand = `docker create ${containerMemLimit} ${containerCPULimit} ${this.language}`;
4241
if (!(this.language in fileFormats)) {
43-
return reject({
42+
reject({
4443
error: true,
45-
errorMessage: "Invalid container name.",
44+
errorMessage: "Invalid language name.",
4645
});
4746
}
48-
47+
48+
const initCommand = `docker create ${containerMemLimit} ${containerCPULimit} ${this.language}`;
4949
child.exec(initCommand, (error, containerID, stderr) => {
5050
if (error) {
5151
reject({
@@ -140,24 +140,27 @@ class JobWorker {
140140
initContainer(): Promise<JobStatus> {
141141
return new Promise(async (resolve, reject) => {
142142
this.createContainer()
143-
.then(async ({ error, errorMessage }) => {
144-
if (error) return new Error(errorMessage);
145-
return this.copyContext()
143+
.then(() => {
144+
this.copyContext()
146145
.then(() => {
147146
resolve({
148-
message: `Job has succedded.`,
147+
message: `Job has succeeded.`,
149148
error: false,
150149
retryable: true,
151150
});
152151
})
153152
.catch((e) => {
154-
throw new Error(e);
153+
reject({
154+
message: e,
155+
error: true,
156+
retryable: true,
157+
});
155158
});
156159
})
157-
.catch((e) => {
160+
.catch(({errorMessage}) => {
158161
reject({
159-
message: `Job has failed for the following reason(s): ${e}.`,
160-
jobFailed: true,
162+
message: errorMessage,
163+
error: true,
161164
retryable: true,
162165
});
163166
});
@@ -169,7 +172,7 @@ class JobWorker {
169172
* 1] Spin up the container
170173
* 2] Record the output from the container
171174
*/
172-
startContainer(): Promise<ExecuteContainer> {
175+
async startContainer(): Promise<ExecuteContainer> {
173176
return new Promise((resolve, reject) => {
174177
this.initContainer()
175178
.then((jobStatus: JobStatus) => {
@@ -213,6 +216,7 @@ class JobWorker {
213216
if (!this.containerID) {
214217
reject("ContainerID not found.");
215218
}
219+
216220
const removeContainer = `docker rm --force ${this.containerID}`;
217221
child.exec(removeContainer, (error, stdout, stderr) => {
218222
if (error) {

0 commit comments

Comments
 (0)