Skip to content

Commit

Permalink
fix: consumed floating promises (#836)
Browse files Browse the repository at this point in the history
* refactor: make get listen path async

* fix: consumed floating promises

* fix: remove unused code

* test: fix testcase

* feat: remove unused await

* test: increase timeout

* fix: add missing typing

* test: update testcase

* refactor: use promise all

* test: fix testcase

* refactor: update logic

* fix: ext server listen
  • Loading branch information
bytemain authored Apr 14, 2022
1 parent 2c6e4a6 commit 83b7588
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 123 deletions.
15 changes: 13 additions & 2 deletions packages/core-common/src/utils/ipc.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { tmpdir } from 'os';
import { join, dirname } from 'path';

import { ensureDirSync } from 'fs-extra';
import { ensureDirSync, ensureDir } from 'fs-extra';

import { isWindows } from '../platform';
import { uuid } from '../uuid';

export function normalizedIpcHandlerPath(name: string, uuidSuffix = false, ipcPath = tmpdir()) {
let handler;
let handler: string;
if (!isWindows) {
handler = join(ipcPath, 'sumi-ipc', `sumi-ipc-${name}${uuidSuffix ? uuid() : ''}.sock`);
ensureDirSync(dirname(handler));
Expand All @@ -16,3 +16,14 @@ export function normalizedIpcHandlerPath(name: string, uuidSuffix = false, ipcPa
}
return handler;
}

export async function normalizedIpcHandlerPathAsync(name: string, uuidSuffix = false, ipcPath = tmpdir()) {
let handler: string;
if (!isWindows) {
handler = join(ipcPath, 'sumi-ipc', `sumi-ipc-${name}${uuidSuffix ? uuid() : ''}.sock`);
await ensureDir(dirname(handler));
} else {
handler = `\\\\.\\pipe\\sumi-ipc-${name}${uuidSuffix ? uuid() : ''}`;
}
return handler;
}
4 changes: 2 additions & 2 deletions packages/core-electron-main/src/bootstrap/window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import treeKill from 'tree-kill';
import { Injectable, Autowired } from '@opensumi/di';
import { ExtensionCandidate } from '@opensumi/ide-core-common';
import { Disposable, getDebugLogger, isOSX, URI, FileUri, Deferred } from '@opensumi/ide-core-common';
import { normalizedIpcHandlerPath } from '@opensumi/ide-core-common/lib/utils/ipc';
import { normalizedIpcHandlerPathAsync } from '@opensumi/ide-core-common/lib/utils/ipc';

import { ElectronAppConfig, ICodeWindow, ICodeWindowOptions } from './types';

Expand Down Expand Up @@ -200,7 +200,7 @@ export class CodeWindow extends Disposable implements ICodeWindow {
this.windowClientId,
this.appConfig.extensionDir,
);
this.rpcListenPath = normalizedIpcHandlerPath('electron-window', true);
this.rpcListenPath = await normalizedIpcHandlerPathAsync('electron-window', true);
await this.node.start(this.rpcListenPath!, (this.workspace || '').toString());
this._nodeReady.resolve();
}
Expand Down
39 changes: 15 additions & 24 deletions packages/extension/__tests__/node/extension.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import { ExtensionHostManager } from '../../src/node/extension.host.manager';
import { ExtensionNodeServiceImpl } from '../../src/node/extension.service';
import { ExtensionServiceClientImpl } from '../../src/node/extension.service.client';


describe('Extension Serivce', () => {
describe('Extension Service', () => {
let injector: Injector;
let extensionService: IExtensionNodeService;
const extensionDir = path.join(__dirname, '../../__mocks__/extensions');
Expand Down Expand Up @@ -46,9 +45,7 @@ describe('Extension Serivce', () => {
},
time() {
return {
timeEnd: () => {
//
},
timeEnd: () => 1,
};
},
},
Expand Down Expand Up @@ -116,62 +113,56 @@ describe('Extension Serivce', () => {
});

describe('extension host process', () => {
let mockExtClientId = 'mock_id123';
beforeEach(() => {
mockExtClientId = 'mock_id' + Math.random();
});

afterEach(async () => {
await extensionService.disposeClientExtProcess(mockExtClientId, true);
});

// jest.setTimeout(20 * 1000);

it('should create extension host process', async () => {
const mockExtClientId = 'mock_id' + Math.random();
await extensionService.createProcess(mockExtClientId);
const port = await extensionService.getProcessInspectPort(mockExtClientId);
expect(port).toBeUndefined();
await extensionService.disposeClientExtProcess(mockExtClientId, true);
});

it.skip('enable extProcess inspect port', async () => {
(global as any).isDev = undefined;
const mockExtClientId = 'mock_id' + Math.random();
await extensionService.createProcess(mockExtClientId);

const res = await extensionService.tryEnableInspectPort(mockExtClientId, 2000);
expect(res).toBeTruthy();

const port = await extensionService.getProcessInspectPort(mockExtClientId);
expect(typeof port).toBe('number');
await extensionService.disposeClientExtProcess(mockExtClientId, true);
});

it('create extension host process with develop mode', async () => {
(global as any).isDev = 1;
const mockExtClientId = 'mock_id' + Math.random();
await extensionService.createProcess(mockExtClientId);
const port = await extensionService.getProcessInspectPort(mockExtClientId);
expect(typeof port).toBe('number');
await extensionService.disposeClientExtProcess(mockExtClientId, false);
(global as any).isDev = undefined;
});

it('create extension host process with enable extension host options', async () => {
(global as any).isDev = undefined;
const mockExtClientId = 'mock_id' + Math.random();
await extensionService.createProcess(mockExtClientId, {
enableDebugExtensionHost: true,
});
const port = await extensionService.getProcessInspectPort(mockExtClientId);
expect(typeof port).toBe('number');
await extensionService.disposeClientExtProcess(mockExtClientId, false);
});
});

describe('getElectronMainThreadListenPath', () => {
it('should create connect listenPath', () => {
const mockExtClientId = 'mock_id' + Math.random();

const listenPath = extensionService.getElectronMainThreadListenPath2(mockExtClientId);
it('should create connect listenPath', async () => {
const listenPath = await extensionService.getElectronMainThreadListenPath2(mockExtClientId);
expect(path.dirname(listenPath)).toBe(path.join(os.tmpdir(), 'sumi-ipc'));
});
});

describe('getExtServerListenOption', () => {
it('should create ext server listen option', async () => {
const mockExtClientId = 'mock_id' + Math.random();

const { port, path: listenPath } = await extensionService.getExtServerListenOption(mockExtClientId);

if (port) {
Expand Down
4 changes: 2 additions & 2 deletions packages/extension/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export interface IExtensionNodeService {
): Promise<IExtensionMetaData[]>;
createProcess(clientId: string, options?: ICreateProcessOptions): Promise<void>;
ensureProcessReady(clientId: string): Promise<boolean>;
getElectronMainThreadListenPath(clientId: string);
getElectronMainThreadListenPath2(clientId: string);
getElectronMainThreadListenPath(clientId: string): Promise<string>;
getElectronMainThreadListenPath2(clientId: string): Promise<string>;
getExtServerListenOption(clientId: string);
getExtension(
extensionPath: string,
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/node/extension.service.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ExtensionServiceClientImpl
}

public async getElectronMainThreadListenPath(clientId: string) {
return this.extensionService.getElectronMainThreadListenPath(clientId);
return await this.extensionService.getElectronMainThreadListenPath(clientId);
}
/**
* 创建插件进程
Expand Down
Loading

0 comments on commit 83b7588

Please sign in to comment.