Path: blob/main/src/vs/base/parts/ipc/test/browser/ipc.mp.test.ts
4780 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { CancellationToken } from '../../../../common/cancellation.js';7import { Event } from '../../../../common/event.js';8import { Client as MessagePortClient } from '../../browser/ipc.mp.js';9import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../test/common/utils.js';1011suite('IPC, MessagePorts', () => {1213test('message passing', async () => {14const { port1, port2 } = new MessageChannel();1516const client1 = new MessagePortClient(port1, 'client1');17const client2 = new MessagePortClient(port2, 'client2');1819client1.registerChannel('client1', {20call(_: unknown, command: string, arg: any, cancellationToken: CancellationToken): Promise<any> {21switch (command) {22case 'testMethodClient1': return Promise.resolve('success1');23default: return Promise.reject(new Error('not implemented'));24}25},2627listen(_: unknown, event: string, arg?: any): Event<any> {28switch (event) {29default: throw new Error('not implemented');30}31}32});3334client2.registerChannel('client2', {35call(_: unknown, command: string, arg: any, cancellationToken: CancellationToken): Promise<any> {36switch (command) {37case 'testMethodClient2': return Promise.resolve('success2');38default: return Promise.reject(new Error('not implemented'));39}40},4142listen(_: unknown, event: string, arg?: any): Event<any> {43switch (event) {44default: throw new Error('not implemented');45}46}47});4849const channelClient1 = client2.getChannel('client1');50assert.strictEqual(await channelClient1.call('testMethodClient1'), 'success1');5152const channelClient2 = client1.getChannel('client2');53assert.strictEqual(await channelClient2.call('testMethodClient2'), 'success2');5455client1.dispose();56client2.dispose();57});5859ensureNoDisposablesAreLeakedInTestSuite();60});616263