Skip to content

Commit 7af290b

Browse files
committed
add reset field tests
1 parent 2cfc983 commit 7af290b

File tree

7 files changed

+89
-51
lines changed

7 files changed

+89
-51
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import subProcessIPCLoopback from './subProcessIPCLoopback'
2+
import IPOS from '../main'
3+
import {withoutProcessSendSync} from './withoutProcessSendSync'
4+
5+
export default async function createConnectedInstances(): Promise<{ main_ipos: IPOS, sub_ipos: IPOS, sub_process: subProcessIPCLoopback }> {
6+
const sub_process = new subProcessIPCLoopback()
7+
let main_ipos: IPOS, sub_ipos: IPOS
8+
await withoutProcessSendSync(() => {
9+
main_ipos = IPOS.new() as IPOS
10+
})
11+
12+
await Promise.all([
13+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
14+
main_ipos.addProcess(sub_process),
15+
(async () => sub_ipos = await IPOS.new())()
16+
])
17+
18+
// @ts-ignore Variable 'main_ipos', 'sub_ipos' is used before being assigned.
19+
return {main_ipos, sub_ipos, sub_process}
20+
}

src/__test__/createFields.test.ts

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

3-
import subProcessIPCLoopback from './subProcessIPCLoopback'
4-
import {withoutProcessSendSync} from './withoutProcessSendSync'
53
import createFieldsTest from './runCreateFieldsTest'
4+
import createConnectedInstances from './createConnectedInstances'
65

7-
async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) {
8-
const sub_process = new subProcessIPCLoopback()
9-
let main_ipos: IPOS, sub_ipos: IPOS
10-
await withoutProcessSendSync(() => {
11-
main_ipos = IPOS.new() as IPOS
12-
})
13-
14-
await Promise.all([
15-
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
16-
main_ipos.addProcess(sub_process),
17-
(async () => sub_ipos = await IPOS.new())()
18-
])
6+
describe('Updating fields between processes (transferring newly created fields)', () =>
7+
createFieldsTest(
8+
async (setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) => {
9+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
1910

20-
// @ts-ignore Variable 'main_ipos' is used before being assigned.
21-
setValue(main_ipos)
11+
// @ts-ignore Variable 'main_ipos' is used before being assigned.
12+
setValue(main_ipos)
2213

23-
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
24-
probeValue(sub_ipos)
14+
// make sure value is transmitted
15+
await new Promise(res => setTimeout(res, 1))
2516

26-
sub_process.destroy()
27-
}
17+
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
18+
probeValue(sub_ipos)
2819

29-
describe('Updating fields between processes (transferring newly created fields)', () =>
30-
createFieldsTest(initWith, (key) =>
31-
['a', 'e', 'i', 'o', 'u'].some(vowel => key.startsWith(vowel))
32-
? `Create an ${key}`
33-
: `Create a ${key}`
20+
sub_process.destroy()
21+
},
22+
(key) =>
23+
['a', 'e', 'i', 'o', 'u'].some(vowel => key.startsWith(vowel))
24+
? `Create an ${key}`
25+
: `Create a ${key}`
3426
)
3527
)

src/__test__/resetFields.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import IPOS from '../main'
2+
3+
import createFieldsTest from './runCreateFieldsTest'
4+
import createConnectedInstances from './createConnectedInstances'
5+
6+
describe('Updating fields between processes (transferring newly created fields)', () =>
7+
createFieldsTest(
8+
async (setValue: (main_ipos: IPOS, reset: boolean) => void, probeValue: (sub_ipos: IPOS) => void) => {
9+
const {main_ipos, sub_ipos, sub_process} = await createConnectedInstances()
10+
11+
main_ipos.create('myField', 'myValue')
12+
setValue(main_ipos, true)
13+
14+
probeValue(sub_ipos)
15+
16+
sub_process.destroy()
17+
},
18+
(key) => `Reset ${key}`
19+
)
20+
)

src/__test__/runCreateFieldsTest.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import IPOS from '../main'
33
import {examples} from './testData'
44

55
export default function createFieldsTest(
6-
initWith: (setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) => void,
6+
initWith: (setValue: (main_ipos: IPOS, reset?: boolean) => void, probeValue: (sub_ipos: IPOS) => void) => void,
77
createLabel: (key: string) => string
88
) {
99
function customizer(a: any, b: any) {
@@ -18,8 +18,12 @@ export default function createFieldsTest(
1818
it(createLabel(exampleKey), async () => {
1919
const value: unknown = examples[exampleKey]
2020
await initWith(
21-
(main_ipos) => {
22-
main_ipos.create('myField', value)
21+
(main_ipos, reset = false) => {
22+
if (!reset) {
23+
main_ipos.create('myField', value)
24+
} else {
25+
main_ipos.myField = value
26+
}
2327
},
2428
(sub_ipos) => {
2529
// lodash equal to compare maps and sets

src/__test__/synchronize.test.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ import subProcessIPCLoopback from './subProcessIPCLoopback'
44
import {withoutProcessSendSync} from './withoutProcessSendSync'
55
import createFieldsTest from './runCreateFieldsTest'
66

7-
async function initWith(setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) {
8-
const sub_process = new subProcessIPCLoopback()
9-
let main_ipos: IPOS, sub_ipos: IPOS
10-
await withoutProcessSendSync(() => {
11-
main_ipos = IPOS.new() as IPOS
12-
setValue(main_ipos)
13-
})
14-
15-
await Promise.all([
16-
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
17-
main_ipos.addProcess(sub_process),
18-
(async () => sub_ipos = await IPOS.new())()
19-
])
7+
describe('Synchronisation of fields between processes (transferring existing fields)', () =>
8+
createFieldsTest(
9+
async (setValue: (main_ipos: IPOS) => void, probeValue: (sub_ipos: IPOS) => void) => {
10+
const sub_process = new subProcessIPCLoopback()
11+
let main_ipos: IPOS, sub_ipos: IPOS
12+
await withoutProcessSendSync(() => {
13+
main_ipos = IPOS.new() as IPOS
14+
setValue(main_ipos)
15+
})
2016

21-
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
22-
probeValue(sub_ipos)
17+
await Promise.all([
18+
// @ts-ignore Argument of type 'subProcessIPCLoopback' is not assignable to parameter of type 'ChildProcess'
19+
main_ipos.addProcess(sub_process),
20+
(async () => sub_ipos = await IPOS.new())()
21+
])
2322

24-
sub_process.destroy()
25-
}
23+
// @ts-ignore Variable 'sub_ipos' is used before being assigned.
24+
probeValue(sub_ipos)
2625

27-
describe('Synchronisation of fields between processes (transferring existing fields)', () =>
28-
createFieldsTest(initWith, (key) => `Synchronise ${key}`)
26+
sub_process.destroy()
27+
},
28+
(key) => `Synchronise ${key}`)
2929
)

src/intercept.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function intercept(value: object, key: string, interceptCallback: (key: string, method: string, ...args: any) => void): object {
1+
export default function intercept<V>(value: V, key: string, interceptCallback: (key: string, method: string, ...args: any) => void): V {
22
const arrayMutatingMethods = ['copyWithin', 'fill', 'pop', 'push', 'reverse', 'shift', 'sort', 'splice', 'unshift']
33
const objectMutatingMethods: string[] = []
44
const mapMutatingMethods = ['clear', 'delete', 'set']
@@ -11,8 +11,9 @@ export default function intercept(value: object, key: string, interceptCallback:
1111
mutatingMethods.set(Set, setMutatingMethods)
1212
mutatingMethods.set(Function, functionMutatingMethods)
1313

14-
if (!mutatingMethods.has(value.constructor))
14+
if (!value || typeof value !== 'object' || !mutatingMethods.has(value.constructor))
1515
return value
16+
1617
return new Proxy(value, {
1718
get(target, name: string) {
1819
if (Reflect.has(target, name) && mutatingMethods.get(value.constructor).includes(name)) {

src/main.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import IPOSMessaging, {iposMessagingMessage, iposMessagingType} from './messagin
44
import intercept from './intercept.js'
55

66
export default class IPOS {
7-
private readonly fields: Map<string, ProxyHandler<any>>
7+
private readonly fields: Map<string, any>
88
private readonly fieldsRaw: Map<string, any>
9-
private fieldsReverseMap: Map<ProxyHandler<any>, string>
9+
private fieldsReverseMap: Map<any, string>
1010
private processMessagingMap: Map<ChildProcess, IPOSMessaging>
1111
private readonly proxy
1212
protected messaging?: IPOSMessaging
13+
1314
[field: string]: unknown;
1415

1516
static new(): IPOS | Promise<IPOS> {

0 commit comments

Comments
 (0)