Skip to content

Commit 149057f

Browse files
committed
add synchronisation tests
1 parent 7f077e2 commit 149057f

File tree

4 files changed

+86
-32
lines changed

4 files changed

+86
-32
lines changed

package-lock.json

Lines changed: 28 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
},
2525
"devDependencies": {
2626
"@types/jest": "^29.0.3",
27+
"@types/lodash": "^4.14.185",
2728
"@types/node": "^18.7.18",
2829
"jest": "^29.0.3",
30+
"lodash": "^4.17.21",
2931
"typescript": "^4.8.3"
3032
},
3133
"jest": {

src/__test__/initialize.test.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ describe('Initialising IPOS', () => {
3030

3131
it('Connect subprocess after it has initialised', async () => {
3232
const sub_process = new subProcessIPCLoopback()
33-
let sub_ipos: Promise<IPOS> = IPOS.new() as Promise<IPOS>,
34-
main_ipos: IPOS
3533

34+
const sub_ipos: Promise<IPOS> = IPOS.new() as Promise<IPOS>
35+
let main_ipos: IPOS
3636
withoutProcessSendSync(() => {
3737
main_ipos = IPOS.new() as IPOS
3838
})
@@ -48,22 +48,36 @@ describe('Initialising IPOS', () => {
4848

4949
await Promise.all([
5050
expect(addProcessPromise).resolves,
51-
await expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
51+
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
5252
])
53+
5354
sub_process.destroy()
5455
})
5556

56-
/*it('Connect subprocess before it has initialised', async () => {
57+
it('Connect subprocess before it has initialised', async () => {
5758
const sub_process = new subProcessIPCLoopback()
59+
60+
let main_ipos: IPOS
5861
withoutProcessSendSync(() => {
59-
const main_ipos = IPOS.new() as IPOS
60-
let addProcessPromise
61-
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
62-
expect(() => addProcessPromise = main_ipos.addProcess(sub_process)).not.toThrow()
63-
expect(addProcessPromise).toBeInstanceOf(Promise)
62+
main_ipos = IPOS.new() as IPOS
6463
})
64+
65+
let addProcessPromise
66+
expect(() =>
67+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
68+
addProcessPromise = main_ipos.addProcess(sub_process)
69+
).not.toThrow()
70+
6571
const sub_ipos = IPOS.new()
66-
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
72+
73+
expect(addProcessPromise).toBeInstanceOf(Promise)
74+
expect(sub_ipos).toBeInstanceOf(Promise)
75+
76+
await Promise.all([
77+
expect(addProcessPromise).resolves,
78+
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
79+
])
80+
6781
sub_process.destroy()
68-
})*/
82+
})
6983
})

src/__test__/synchronize.test.ts

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1+
import _ from 'lodash'
2+
13
import IPOS from '../main'
4+
25
import subProcessIPCLoopback from './subProcessIPCLoopback'
3-
import {withoutProcessSend} from './withoutProcessSendSync'
6+
import {withoutProcessSend, withoutProcessSendSync} from './withoutProcessSendSync'
47

58
async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) {
69
const sub_process = new subProcessIPCLoopback()
7-
let main_ipos: IPOS
8-
await withoutProcessSend(async () => {
10+
let main_ipos: IPOS, sub_ipos: IPOS
11+
await withoutProcessSendSync(() => {
912
main_ipos = IPOS.new() as IPOS
1013
setValue(main_ipos)
1114
})
12-
console.log('sub_ipos')
13-
let sub_ipos = IPOS.new()
14-
console.log('withoutProcessSend')
15-
await withoutProcessSend(async () => {
16-
console.log('main_ipos.addProcess')
15+
16+
await Promise.all([
1717
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
18-
await main_ipos.addProcess(sub_process)
19-
console.log('done main_ipos.addProcess')
20-
})
21-
console.log('await sub_ipos')
22-
sub_ipos = await sub_ipos
18+
main_ipos.addProcess(sub_process),
19+
(async () => sub_ipos = await IPOS.new())()
20+
])
21+
22+
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
2323
probeValue(sub_ipos)
24+
2425
sub_process.destroy()
2526
}
2627

27-
/*describe('Synchronising fields between processes', () => {
28+
describe('Synchronisation of fields between processes (transferring existing fields)', () => {
2829
const examples: { [key: string]: unknown } = {
2930
'string': 'myString',
3031
'number': 42,
@@ -33,7 +34,19 @@ async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ip
3334
mySecondValue: 42
3435
},
3536
'array': ['myItem', 42],
37+
'map': new Map(Object.entries({
38+
myKey: 'myValue',
39+
mySecondValue: 42
40+
})),
3641
'set': new Set(['myItem', 42]),
42+
'function': (a: number, b: number) => a + b,
43+
}
44+
45+
function customizer(a: any, b: any) {
46+
// compare function contents
47+
if ([typeof a, typeof b].every(a => a === 'function')) {
48+
return a.toString() === b.toString();
49+
}
3750
}
3851

3952
for (const exampleKey in examples) {
@@ -45,12 +58,11 @@ async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ip
4558
main_ipos.create('myField', value)
4659
},
4760
(sub_ipos) => {
48-
console.log(value)
49-
console.log(sub_ipos.myField)
50-
// expect(sub_ipos.get('myField')).toEqual(value)
51-
// expect(sub_ipos.myField).toEqual(value)
61+
// lodash equal to compare maps and sets
62+
expect(_.isEqualWith(sub_ipos.myField, value, customizer)).toEqual(true)
63+
expect(_.isEqualWith(sub_ipos.get('myField'), value, customizer)).toEqual(true)
5264
}
5365
)
5466
})
5567
}
56-
})*/
68+
})

0 commit comments

Comments
 (0)