Skip to content

Commit 185410b

Browse files
committed
add initialising tests #1
1 parent 49ee73c commit 185410b

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.idea/
22
example
33
src
4+
__test__
45
tsconfig.json
56
ipos-*.tgz

src/__test__/initialize.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import IPOS from '../main'
2+
import subProcessIPCLoopback from './subProcessIPCLoopback'
3+
4+
function withoutProcessSend(callback: Function) {
5+
const processSend = process.send
6+
process.send = undefined
7+
callback()
8+
process.send = processSend
9+
}
10+
11+
describe('Initialising IPOS', () => {
12+
it('Create new instance in a main process', () => {
13+
// this is a subprocess anyway
14+
// just simulate main process by setting process.send to undefined (jamming the subprocess detection)
15+
withoutProcessSend(() => {
16+
let ipos
17+
expect(() => ipos = IPOS.new()).not.toThrow()
18+
expect(ipos).toBeInstanceOf(IPOS)
19+
})
20+
})
21+
22+
it('Create new instance in a sub process', () => {
23+
let ipos
24+
expect(() => ipos = IPOS.new()).not.toThrow()
25+
expect(ipos).toBeInstanceOf(Promise)
26+
expect(ipos).resolves.toBeInstanceOf(IPOS)
27+
})
28+
29+
/* The child IPOS doesn't actually have to be in the child process.
30+
* Instead, the child process' ".on("message", handler)" and ".send()" are simulated.
31+
* The main IPOS instance will send over the simulated "sub_process.send()",
32+
* and listen over the simulated "sub_process.on("message", handler)".
33+
* The child IPOS instance will send over the native (intercepted) "process.send()",
34+
* and listen over the native (intercepted) "process.on("message", handler)".
35+
*/
36+
37+
it('Connect subprocess after it has initialised', async () => {
38+
const sub_process = new subProcessIPCLoopback()
39+
const sub_ipos = IPOS.new()
40+
withoutProcessSend(() => {
41+
const main_ipos = IPOS.new() as IPOS
42+
let addProcessPromise
43+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
44+
expect(() => addProcessPromise = main_ipos.addProcess(sub_process)).not.toThrow()
45+
expect(addProcessPromise).toBeInstanceOf(Promise)
46+
})
47+
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
48+
sub_process.destroy()
49+
})
50+
51+
it('Connect subprocess before it has initialised', async () => {
52+
const sub_process = new subProcessIPCLoopback()
53+
withoutProcessSend(() => {
54+
const main_ipos = IPOS.new() as IPOS
55+
let addProcessPromise
56+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
57+
expect(() => addProcessPromise = main_ipos.addProcess(sub_process)).not.toThrow()
58+
expect(addProcessPromise).toBeInstanceOf(Promise)
59+
})
60+
const sub_ipos = IPOS.new()
61+
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
62+
sub_process.destroy()
63+
})
64+
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export default class subProcessIPCLoopback {
2+
private originalProcessSend;
3+
private readonly originalProcessOn;
4+
private subProcessListener?: NodeJS.MessageListener;
5+
private mainProcessListener?: NodeJS.MessageListener;
6+
7+
constructor() {
8+
this.originalProcessSend = process.send
9+
this.originalProcessOn = process.on
10+
process.send = (message: any) => {
11+
if (this.subProcessListener)
12+
this.subProcessListener(message, this.send)
13+
return true
14+
}
15+
process.on = (event: string, listener: (...args: any[]) => void) => {
16+
if (event === 'message') {
17+
this.mainProcessListener = listener
18+
return process
19+
} else {
20+
return this.originalProcessOn(event, listener)
21+
}
22+
}
23+
}
24+
25+
public send(message: any) {
26+
if (this.mainProcessListener)
27+
this.mainProcessListener(message, process.send)
28+
}
29+
30+
public on(event: 'message', listener: NodeJS.MessageListener): this {
31+
this.subProcessListener = listener
32+
return this
33+
}
34+
35+
public destroy() {
36+
process.send = this.originalProcessSend
37+
process.on = this.originalProcessOn
38+
this.mainProcessListener = undefined
39+
this.subProcessListener = undefined
40+
}
41+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"lib": ["ESNext"],
44
"module": "esnext",
55
"target": "esnext",
6+
"moduleResolution": "node",
67

78
"declaration": true,
89

0 commit comments

Comments
 (0)