|
| 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 | +}) |
0 commit comments