Tuesday 11 April 2017

Node.js: This socket has been ended by the other party

I am using child_process module to spawn java subprocess. It works fine. But when I try to test that code using chai, it fails during writing to child stdin with:

Error: This socket has been ended by the other party
  at Socket.writeAfterFIN [as write] (net.js:294:12)

here is my slightly modified code:

node.js code (index.js):

class MyJavaCLIAdapter {
    constructor({stream}) {
        ...
        this.stream = stream;
        ...
    }

    async makeSomething({param1, param2}) {
        const formattedRequest = this._formatRequestString(param1, param2);

        this.stream.stdin.write(formattedRequest + '\n'); // FAILS HERE

        ...
    }
...
}

const myCLIProcess = childProcess.spawn(
    'java',
    ['-cp', 'some-java-file-3.7.0.jar:commons-io-2.5.jar:.', 'MyJavaCLI']
);

const cliAdapter = new MyJavaCLIAdapter({stream: myCLIProcess});

module.exports = function({param1, param2}) {
    return cliAdapter.makeSomething({param1, param2});
}

file with chai test:

const chai = require('chai');
const { assert } = chai;

const myfunc = require('../index.js');

suite('some chai tests');

test('some test', async () => {
    const reqData = {
        param1: "value1",
        param2: "value2"
    };

    const result = await myfunc(reqData);
    const correctResult = ...;

    assert.deepEqual(
        result,
        correctResult
    );
});



via Dmitry Nalyvaiko

No comments:

Post a Comment