All files / integrationSpecs/nodejs/process processTransportUtils.js

100% Statements 16/16
100% Branches 2/2
100% Functions 3/3
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 441x 1x 1x 1x   1x           1x                       1x   1x 1x 2x 1x 1x 1x       1x       1x   1x      
const path = require("path");
const {fork} = require('child_process');
const {processTransport} = require("../../../dist/transports/nodejs/process");
const {proxycom} = require("../../../dist");
 
const apiConfig = {props: ["foo"]};
 
/**
 * Utility to help the test execution of multi-process scenario.
 * @type {{start: (function(*, *, *): Promise<unknown>)}}
 */
module.exports = {
    /**
     * Forks a child process (see file childProcess) and exposes an api.
     * Once forked, it sends a message to child process to call an api method with a specific argument.
     * Child process will then send a message with the returned value.
     *
     * @param fooSpy The method to be exposed on the service
     * @param callFooWith The value to be passed
     * @param resolvedSpy The function to call with the value received by child
     * @returns {Promise<*>} A promise that resolves once all parent-child interaction is done
     */
    start: (fooSpy, callFooWith, resolvedSpy) => {
        const child = fork(path.resolve(__dirname,'./child-process'));
 
        return new Promise((resolve)=> {
            child.on("message", (message) => {
                if(message.action === "returnValue") {
                    resolvedSpy(message.value);
                    child.disconnect();
                    resolve("done");
                }
            })
 
            const service = {
                foo: fooSpy
            }
 
            proxycom.exposeApi({apiConfig, api: service, transport: processTransport.getForParentProcess(child)});
 
            child.send({action: "callFoo", value: callFooWith});
        })
    }
}