Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inter-Process Communication #498

Merged
merged 9 commits into from
Aug 18, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ipc prop boolean => number
gustavohenke committed Aug 13, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 5ab0918b51b8c1887ba54cc729c4e3242695bc1f
22 changes: 12 additions & 10 deletions src/command.spec.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,8 @@ let sendMessage: jest.Mock;
let spawn: jest.Mocked<SpawnCommand>;
let killProcess: KillProcess;

const IPC_FD = 3;

autoUnsubscribe();

beforeEach(() => {
@@ -254,7 +256,7 @@ describe('#start()', () => {

describe('on incoming messages', () => {
it('does not share to the incoming messages stream, if IPC is disabled', () => {
const { command } = createCommand({ ipc: false });
const { command } = createCommand();
const spy = subscribeSpyTo(command.messages.incoming);
command.start();

@@ -263,7 +265,7 @@ describe('#start()', () => {
});

it('shares to the incoming messages stream, if IPC is enabled', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
const spy = subscribeSpyTo(command.messages.incoming);
command.start();

@@ -282,7 +284,7 @@ describe('#start()', () => {

describe('on outgoing messages', () => {
it('calls onSent with an error if the process does not have IPC enabled', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
command.start();

Object.assign(process, {
@@ -297,7 +299,7 @@ describe('#start()', () => {
});

it('sends the message to the process', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
command.start();

const message1 = {};
@@ -336,7 +338,7 @@ describe('#start()', () => {
});

it('sends the message to the process, if it starts late', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
command.messages.outgoing.next({ message: {}, onSent() {} });
expect(process.send).not.toHaveBeenCalled();

@@ -345,7 +347,7 @@ describe('#start()', () => {
});

it('calls onSent with the result of sending the message', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
command.start();

const onSent = jest.fn();
@@ -364,13 +366,13 @@ describe('#start()', () => {

describe('#send()', () => {
it('throws if IPC is not set up', () => {
const { command } = createCommand({ ipc: false });
const { command } = createCommand({ ipc: IPC_FD });
const fn = () => command.send({});
expect(fn).toThrow();
});

it('pushes the message on the outgoing messages stream', () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
const spy = subscribeSpyTo(command.messages.outgoing);

const message1 = { foo: true };
@@ -395,15 +397,15 @@ describe('#send()', () => {
});

it('resolves when onSent callback is called with no arguments', async () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
const spy = subscribeSpyTo(command.messages.outgoing);
const promise = command.send({});
spy.getFirstValue().onSent();
await expect(promise).resolves.toBeUndefined();
});

it('rejects when onSent callback is called with an argument', async () => {
const { command } = createCommand({ ipc: true });
const { command } = createCommand({ ipc: IPC_FD });
const spy = subscribeSpyTo(command.messages.outgoing);
const promise = command.send({});
spy.getFirstValue().onSent('foo');
8 changes: 4 additions & 4 deletions src/command.ts
Original file line number Diff line number Diff line change
@@ -40,11 +40,11 @@ export interface CommandInfo {

/**
* Whether sending of messages to/from this command (also known as "inter-process communication")
* should be enabled.
* should be enabled, and using which file descriptor number.
*
* @default false
* If set, must be > 2.
*/
ipc?: boolean;
ipc?: number;

/**
* Output command in raw format.
@@ -140,7 +140,7 @@ export class Command implements CommandInfo {
readonly cwd?: string;

/** @inheritdoc */
readonly ipc?: boolean = false;
readonly ipc?: number;

readonly close = new Rx.Subject<CloseEvent>();
readonly error = new Rx.Subject<unknown>();