Skip to content

Commit fa9b67d

Browse files
committed
add tests for errors
1 parent e9c8c66 commit fa9b67d

File tree

6 files changed

+95
-14
lines changed

6 files changed

+95
-14
lines changed

src/__test__/createConnectedInstances.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import subProcessIPCLoopback from './subProcessIPCLoopback'
1+
import SubProcessIPCLoopback from './subProcessIPCLoopback'
22
import IPOS from '../main'
33
import {withoutProcessSend} from './withoutProcessSend'
44

5-
export default async function createConnectedInstances(): Promise<{ main_ipos: IPOS, sub_ipos: IPOS, sub_process: subProcessIPCLoopback }> {
6-
const sub_process = new subProcessIPCLoopback()
5+
export default async function createConnectedInstances(): Promise<{ main_ipos: IPOS, sub_ipos: IPOS, sub_process: SubProcessIPCLoopback }> {
6+
const sub_process = new SubProcessIPCLoopback()
77
let main_ipos: IPOS, sub_ipos: IPOS
8-
await withoutProcessSend(() => {
8+
withoutProcessSend(() => {
99
main_ipos = IPOS.new() as IPOS
1010
})
1111

src/__test__/error.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import child_process from 'child_process'
2+
3+
import IPOS from '../main'
4+
import {withoutProcessSend} from './withoutProcessSend'
5+
import IPOSMessaging from '../messaging'
6+
import createConnectedInstances from "./createConnectedInstances";
7+
import {examples} from "./testData";
8+
9+
describe('Probing errors', () => {
10+
it('Changing inherent property', () => {
11+
let ipos: IPOS
12+
withoutProcessSend(() => ipos = IPOS.new() as IPOS)
13+
expect(() => ipos.get = (key) => 'myValue').toThrow()
14+
})
15+
16+
it('Set uncreated field', () => {
17+
let ipos: IPOS
18+
withoutProcessSend(() => ipos = IPOS.new() as IPOS)
19+
expect(() => ipos.myKey = 'myValue').toThrow()
20+
})
21+
22+
it('Add process without ipc channel', () => {
23+
withoutProcessSend(() => {
24+
const ipos = IPOS.new() as IPOS
25+
const sub_process = child_process.spawn('node', ['../../example/sub-process.js'])
26+
expect(() => ipos.addProcess(sub_process)).toThrow()
27+
})
28+
})
29+
30+
it('Create IPOSMessaging instance with process without ipc channel', () => {
31+
withoutProcessSend(() => {
32+
expect(() => new IPOSMessaging(process)).toThrow()
33+
})
34+
})
35+
36+
it('Serialise unknown native object', async () => {
37+
const {main_ipos} = await createConnectedInstances()
38+
const buffer = new ArrayBuffer(2)
39+
expect(() => main_ipos.create('myKey', new DataView(buffer))).toThrow()
40+
})
41+
42+
it('Serialise unregistered class', async () => {
43+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
44+
45+
class Unregistered {
46+
}
47+
48+
const unregisteredInstance = new Unregistered()
49+
expect(() => main_ipos.create('myKey', unregisteredInstance))
50+
.toThrowError(`Class: \`${unregisteredInstance.constructor.name}\` must have methods to serialize and deserialize objects. (\`.stringify()\`, \`.serialize()\`)`)
51+
})
52+
53+
it('Deserialise unregistered class', async () => {
54+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
55+
56+
class Unregistered {
57+
serialize() {
58+
return 'unregisteredClass'
59+
}
60+
}
61+
62+
const unregisteredInstance = new Unregistered()
63+
expect(() => main_ipos.create('myKey', unregisteredInstance))
64+
.toThrowError(`Did not recognize type \`${unregisteredInstance.constructor.name}\`. Did you register it in the child process?`)
65+
})
66+
67+
it('Deserialise registered class without deserialisation method', async () => {
68+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
69+
70+
class Registered {
71+
serialize() {
72+
return 'unregisteredClass'
73+
}
74+
}
75+
76+
IPOS.registerClass(Registered)
77+
78+
const registeredInstance = new Registered()
79+
expect(() => main_ipos.create('myKey', registeredInstance))
80+
.toThrowError(`Did not recognize type \`${registeredInstance.constructor.name}\`. Did you register it in the child process?`)
81+
})
82+
})

src/__test__/initialize.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import IPOS from '../main'
2-
import subProcessIPCLoopback from './subProcessIPCLoopback'
2+
import SubProcessIPCLoopback from './subProcessIPCLoopback'
33
import {withoutProcessSend} from './withoutProcessSend'
44

55
describe('Initialising IPOS', () => {
@@ -29,7 +29,7 @@ describe('Initialising IPOS', () => {
2929
*/
3030

3131
it('Connect subprocess after it has initialised', async () => {
32-
const sub_process = new subProcessIPCLoopback()
32+
const sub_process = new SubProcessIPCLoopback()
3333

3434
const sub_ipos: Promise<IPOS> = IPOS.new() as Promise<IPOS>
3535
let main_ipos: IPOS
@@ -55,7 +55,7 @@ describe('Initialising IPOS', () => {
5555
})
5656

5757
it('Connect subprocess before it has initialised', async () => {
58-
const sub_process = new subProcessIPCLoopback()
58+
const sub_process = new SubProcessIPCLoopback()
5959

6060
let main_ipos: IPOS
6161
withoutProcessSend(() => {

src/__test__/subProcessIPCLoopback.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default class subProcessIPCLoopback {
1+
export default class SubProcessIPCLoopback {
22
private readonly originalProcessSend;
33
private readonly originalProcessOn;
44
private subProcessListener?: NodeJS.MessageListener;

src/__test__/synchronize.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import IPOS from '../main'
22

3-
import subProcessIPCLoopback from './subProcessIPCLoopback'
3+
import SubProcessIPCLoopback from './subProcessIPCLoopback'
44
import {withoutProcessSend} from './withoutProcessSend'
55
import createFieldsTest from './runCreateFieldsTest'
66

77
describe('Synchronising fields', () =>
88
createFieldsTest(
99
async (setValue: (ipos_for_setting: IPOS) => void, probeValue: (ipos_for_probing: IPOS) => void) => {
10-
const sub_process = new subProcessIPCLoopback()
10+
const sub_process = new SubProcessIPCLoopback()
1111
let main_ipos: IPOS, sub_ipos: IPOS
12-
await withoutProcessSend(() => {
12+
withoutProcessSend(() => {
1313
main_ipos = IPOS.new() as IPOS
1414
setValue(main_ipos)
1515
})

src/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,8 @@ export default class IPOS {
186186
private syncProcess(process: ChildProcess): Promise<void> {
187187
let resolve: Function
188188
const promise: Promise<void> = new Promise(res => resolve = res)
189-
this.processMessagingMap.get(process)?.listenOnceForType('sync_ok', () => {
190-
resolve()
191-
})
189+
this.processMessagingMap.get(process)
190+
?.listenOnceForType('sync_ok', () => resolve())
192191
this.processMessagingMap.get(process)?.send('sync', {fields: this.fields})
193192
return promise
194193
}

0 commit comments

Comments
 (0)