Skip to content

Commit 88a370b

Browse files
committed
added unit test cases
1 parent 207ae96 commit 88a370b

File tree

5 files changed

+73
-41
lines changed

5 files changed

+73
-41
lines changed

__tests__/code-excution/python.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1-
import { CodeContext } from "../../src/types/worker";
1+
import { mainClassName } from "../../src/config";
2+
import { CodeContext, ExecuteContainer } from "../../src/types/worker";
23
import JobWorker from "../../src/worker";
34
import fs from 'fs'
45

6+
describe("Python transform into executable", () => {
7+
const worker = new JobWorker();
8+
it('should instantiate an object and execute the function', () => {
9+
const functionName = "solution"
10+
const code = worker.transformCodeIntoExecutable("python3", {
11+
code: "",
12+
functionName: functionName
13+
})
14+
expect(code).toBe(`\nprint(${mainClassName}().${functionName}())`)
15+
})
16+
})
17+
518

619
describe("Python code excution test", () => {
720
const worker = new JobWorker();
@@ -14,10 +27,11 @@ describe("Python code excution test", () => {
1427
}
1528
worker
1629
.startContainer("python3", codeContext)
17-
.then((output) => {
18-
expect(output).toBe("Counter({'a': 2, 's': 2, 'w': 2, 'e': 2, 'l': 1, 'd': 1, 'k': 1})")
30+
.then((response: ExecuteContainer) => {
31+
expect(response.codeOutput).toBe("Counter({'a': 2, 's': 2, 'w': 2, 'e': 2, 'l': 1, 'd': 1, 'k': 1})")
32+
worker.removeContainer(response.containerID!);
1933
})
20-
.catch((e) => console.log(e));
34+
.catch(_ => {});
2135
});
2236
})
2337
})

__tests__/test-code/python/counter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
class Execute:
44
def calculate(self):
55
str = "asldkasweew"
6-
print(Counter(str))
6+
return Counter(str)
77

__tests__/worker.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,3 @@ describe("Docker initialization test", () => {
2828
});
2929

3030

31-
32-
describe("Transform into executable", () => {
33-
const worker = new JobWorker();
34-
it('should instantiate an object and execute the function', () => {
35-
const functionName = "solution"
36-
const code = worker.transformCodeIntoExecutable("python3", {
37-
code: "",
38-
functionName: functionName
39-
})
40-
expect(code).toBe(`\n${mainClassName}().${functionName}()`)
41-
})
42-
})

src/types/worker.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type language = "python3" | "javascript";
22
export type fileFormat = "py" | "js";
3+
export type Stdout = string
34

45
export interface CodeContext {
56
/**
@@ -13,21 +14,23 @@ export interface CodeContext {
1314
functionName: string
1415
}
1516

16-
export interface ContainerInitialization {
17+
interface BaseError {
1718
/**
18-
* Indicates whether it successfully created a contaienr
19+
* Indicates an error during the process of spinning up the container
1920
*/
20-
error: boolean;
21+
error: boolean
2122

2223
/**
23-
* The ID of the newly created container
24+
* An error message in case the job fails
2425
*/
25-
containerID: string;
26+
errorMessage?: string
27+
}
2628

29+
export interface ContainerInitialization extends BaseError {
2730
/**
28-
* Error message in case of an error
31+
* The ID of the newly created container
2932
*/
30-
errorMessage?: string;
33+
containerID: string;
3134
}
3235

3336
export interface WriteFileStatus {
@@ -42,16 +45,15 @@ export interface WriteFileStatus {
4245
fileFormat: string;
4346
}
4447

45-
export interface JobStatus {
48+
49+
50+
export interface JobStatus extends BaseError{
4651
/**
4752
* A brief message that describes that job status
4853
*/
4954
message: string;
5055

51-
/**
52-
* Whether the job failed
53-
*/
54-
jobFailed: boolean;
56+
5557

5658
/**
5759
* indicates if the job is retryable
@@ -73,3 +75,14 @@ export interface JobStatus {
7375
*/
7476
filePath?: string;
7577
}
78+
79+
export interface ExecuteContainer extends BaseError{
80+
/**
81+
* stdout from the container as a result of running the code
82+
*/
83+
codeOutput?: Stdout
84+
/**
85+
* ID of the container
86+
*/
87+
containerID?: string
88+
}

src/worker.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
WriteFileStatus,
1010
language,
1111
fileFormat,
12+
ExecuteContainer,
13+
Stdout,
1214
} from "./types/worker";
1315
import { mainClassName } from "./config";
1416

@@ -19,6 +21,7 @@ class JobWorker {
1921
python3: "py",
2022
javascript: "js",
2123
};
24+
2225
/**
2326
* Creates an appropriate docker container
2427
* to execute the target code
@@ -55,14 +58,15 @@ class JobWorker {
5558
}
5659

5760
/**
58-
* Returns a piece of code that executes a function in a class
59-
* language specific, so defined using switch cases
61+
* Returns a piece of code that executes the class method
62+
* this is language specific, so defined using switch cases
6063
*/
61-
transformCodeIntoExecutable(language: language, context: CodeContext) {
64+
transformCodeIntoExecutable(language: language, context: CodeContext): string {
6265
switch(language) {
6366
case "python3": {
64-
return `\n${mainClassName}().${context.functionName}()`
67+
return `\nprint(${mainClassName}().${context.functionName}())`
6568
}
69+
default: return ""
6670
}
6771
}
6872

@@ -99,7 +103,7 @@ class JobWorker {
99103
/**
100104
* Removes a container with the provided containerID
101105
*/
102-
removeContainer(containerID: string): Promise<string> {
106+
removeContainer(containerID: string): Promise<Stdout> {
103107
return new Promise((resolve, reject) => {
104108
const removeContainer = `docker rm --force ${containerID}`;
105109
child.exec(removeContainer, (error, stdout, stderr) => {
@@ -157,7 +161,7 @@ class JobWorker {
157161
.then(() => {
158162
resolve({
159163
message: `Job has succedded.`,
160-
jobFailed: false,
164+
error: false,
161165
retryable: true,
162166
context: context,
163167
containerID: containerID,
@@ -183,24 +187,37 @@ class JobWorker {
183187
* 1] Spin up the container
184188
* 2] Record the output from the container
185189
*/
186-
startContainer(language: language, context: CodeContext): Promise<string> {
190+
startContainer(language: language, context: CodeContext): Promise<ExecuteContainer> {
187191
return new Promise((resolve, reject) => {
188192
this.initContainer(language, context)
189193
.then((jobStatus: JobStatus) => {
190-
if (!jobStatus.jobFailed && jobStatus.containerID) {
194+
if (!jobStatus.error && jobStatus.containerID) {
191195
const startContainer = `docker start -a ${jobStatus.containerID}`;
192196
child.exec(startContainer, (error, stdout, stderr) => {
193197
if (error) {
194-
reject(error.message);
198+
reject({
199+
error: true,
200+
errorMessage: error.message
201+
} as ExecuteContainer);
195202
} else if (stderr) {
196-
reject(stderr);
203+
reject({
204+
error: true,
205+
errorMessage: stderr
206+
} as ExecuteContainer);
197207
} else {
198-
resolve(stdout.trim());
208+
resolve({
209+
error: false,
210+
codeOutput: stdout.trim(),
211+
containerID: jobStatus.containerID
212+
} as ExecuteContainer);
199213
}
200214
});
201215
} else {
202216
// job failed
203-
reject();
217+
reject({
218+
error: true,
219+
errorMessage: `Job has failed in the process of initializing the container for the following reason(s): ${jobStatus.message}`
220+
} as ExecuteContainer);
204221
}
205222
})
206223
.catch((e) => console.log(e));

0 commit comments

Comments
 (0)