Skip to content

Commit 7f077e2

Browse files
committed
fix initialise test, change some message funccall orderings
1 parent 185410b commit 7f077e2

File tree

7 files changed

+101
-30
lines changed

7 files changed

+101
-30
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"object-sharing"
1818
],
1919
"scripts": {
20-
"build": "tsc",
20+
"build": "rm -rf lib && tsc",
2121
"watch": "tsc --watch",
2222
"prepack": "npm run build",
23-
"test": "node --experimental-vm-modules ./node_modules/.bin/jest"
23+
"test": "node --experimental-vm-modules ./node_modules/.bin/jest --verbose"
2424
},
2525
"devDependencies": {
2626
"@types/jest": "^29.0.3",

src/__test__/initialize.test.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import IPOS from '../main'
22
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-
}
3+
import {withoutProcessSend, withoutProcessSendSync} from './withoutProcessSendSync'
104

115
describe('Initialising IPOS', () => {
126
it('Create new instance in a main process', () => {
137
// this is a subprocess anyway
148
// just simulate main process by setting process.send to undefined (jamming the subprocess detection)
15-
withoutProcessSend(() => {
9+
withoutProcessSendSync(() => {
1610
let ipos
1711
expect(() => ipos = IPOS.new()).not.toThrow()
1812
expect(ipos).toBeInstanceOf(IPOS)
@@ -36,21 +30,32 @@ describe('Initialising IPOS', () => {
3630

3731
it('Connect subprocess after it has initialised', async () => {
3832
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)
33+
let sub_ipos: Promise<IPOS> = IPOS.new() as Promise<IPOS>,
34+
main_ipos: IPOS
35+
36+
withoutProcessSendSync(() => {
37+
main_ipos = IPOS.new() as IPOS
4638
})
47-
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
39+
40+
let addProcessPromise
41+
expect(() =>
42+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
43+
addProcessPromise = main_ipos.addProcess(sub_process)
44+
).not.toThrow()
45+
46+
expect(addProcessPromise).toBeInstanceOf(Promise)
47+
expect(sub_ipos).toBeInstanceOf(Promise)
48+
49+
await Promise.all([
50+
expect(addProcessPromise).resolves,
51+
await expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
52+
])
4853
sub_process.destroy()
4954
})
5055

51-
it('Connect subprocess before it has initialised', async () => {
56+
/*it('Connect subprocess before it has initialised', async () => {
5257
const sub_process = new subProcessIPCLoopback()
53-
withoutProcessSend(() => {
58+
withoutProcessSendSync(() => {
5459
const main_ipos = IPOS.new() as IPOS
5560
let addProcessPromise
5661
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
@@ -60,5 +65,5 @@ describe('Initialising IPOS', () => {
6065
const sub_ipos = IPOS.new()
6166
expect(sub_ipos).resolves.toBeInstanceOf(IPOS)
6267
sub_process.destroy()
63-
})
68+
})*/
6469
})

src/__test__/synchronize.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import IPOS from '../main'
2+
import subProcessIPCLoopback from './subProcessIPCLoopback'
3+
import {withoutProcessSend} from './withoutProcessSendSync'
4+
5+
async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) {
6+
const sub_process = new subProcessIPCLoopback()
7+
let main_ipos: IPOS
8+
await withoutProcessSend(async () => {
9+
main_ipos = IPOS.new() as IPOS
10+
setValue(main_ipos)
11+
})
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')
17+
// @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
23+
probeValue(sub_ipos)
24+
sub_process.destroy()
25+
}
26+
27+
/*describe('Synchronising fields between processes', () => {
28+
const examples: { [key: string]: unknown } = {
29+
'string': 'myString',
30+
'number': 42,
31+
'object': {
32+
myKey: 'myValue',
33+
mySecondValue: 42
34+
},
35+
'array': ['myItem', 42],
36+
'set': new Set(['myItem', 42]),
37+
}
38+
39+
for (const exampleKey in examples) {
40+
if (!examples.hasOwnProperty(exampleKey)) continue
41+
it(`Synchronise ${exampleKey}`, async () => {
42+
const value: unknown = examples[exampleKey]
43+
await initWith(
44+
(main_ipos) => {
45+
main_ipos.create('myField', value)
46+
},
47+
(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)
52+
}
53+
)
54+
})
55+
}
56+
})*/
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function withoutProcessSendSync(callback: () => void) {
2+
const processSend = process.send
3+
process.send = undefined
4+
callback()
5+
process.send = processSend
6+
}
7+
8+
export async function withoutProcessSend(callback: () => Promise<any>) {
9+
const processSend = process.send
10+
process.send = undefined
11+
await callback()
12+
process.send = processSend
13+
}

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class IPOS {
1010
private processMessagingMap: Map<ChildProcess, IPOSMessaging>
1111
private readonly proxy
1212
protected messaging?: IPOSMessaging
13+
[field: string]: unknown;
1314

1415
static new(): IPOS | Promise<IPOS> {
1516
const ipos = new IPOS()
@@ -156,16 +157,18 @@ export default class IPOS {
156157
resolve()
157158
)
158159
})
160+
// send a "ready" message to receive another "register" (if an instance is initiated)
161+
messaging.send('ready')
159162
return promise
160163
}
161164

162165
private syncProcess(process: ChildProcess): Promise<void> {
163166
let resolve: Function
164167
const promise: Promise<void> = new Promise(res => resolve = res)
165-
this.processMessagingMap.get(process)?.send('sync', {fields: this.fields})
166168
this.processMessagingMap.get(process)?.listenOnceForType('sync_ok', () => {
167169
resolve()
168170
})
171+
this.processMessagingMap.get(process)?.send('sync', {fields: this.fields})
169172
return promise
170173
}
171174
}

src/messaging.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,6 @@ export default class IPOSMessaging {
5858
this.nonIPOSListeners.forEach(callback => callback(message))
5959
}
6060
})
61-
62-
// if the current process is a parent process
63-
if (process instanceof ChildProcess) {
64-
// send a "ready" message to receive another "register" (if an instance is initiated)
65-
this.send('ready')
66-
}
6761
}
6862

6963
/*getNonIPOSMessages(handler: (message: any) => any) {

src/serialize.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function serialize(value: any): any | void {
1717
} else if (value instanceof Map) {
1818
return {
1919
$$iposType: 'Map',
20-
data: new Map(
20+
data: Object.fromEntries(
2121
Array.from(value.entries())
2222
.map(([key, value]) => [key, serialize(value)])
2323
)
@@ -50,7 +50,7 @@ export function deserialize(value: string | number | Array<any> | { $$iposType?:
5050
// todo: is this acceptable?
5151
return eval(`(${value.data})`)
5252
} else if (value.$$iposType === 'Map') {
53-
return Object.fromEntries(
53+
return new Map(
5454
Array.from(Object.entries(value.data))
5555
.map(deserialize)
5656
)

0 commit comments

Comments
 (0)