Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/parts/ipc/test/browser/ipc.mp.test.ts
4780 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { CancellationToken } from '../../../../common/cancellation.js';
8
import { Event } from '../../../../common/event.js';
9
import { Client as MessagePortClient } from '../../browser/ipc.mp.js';
10
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../test/common/utils.js';
11
12
suite('IPC, MessagePorts', () => {
13
14
test('message passing', async () => {
15
const { port1, port2 } = new MessageChannel();
16
17
const client1 = new MessagePortClient(port1, 'client1');
18
const client2 = new MessagePortClient(port2, 'client2');
19
20
client1.registerChannel('client1', {
21
call(_: unknown, command: string, arg: any, cancellationToken: CancellationToken): Promise<any> {
22
switch (command) {
23
case 'testMethodClient1': return Promise.resolve('success1');
24
default: return Promise.reject(new Error('not implemented'));
25
}
26
},
27
28
listen(_: unknown, event: string, arg?: any): Event<any> {
29
switch (event) {
30
default: throw new Error('not implemented');
31
}
32
}
33
});
34
35
client2.registerChannel('client2', {
36
call(_: unknown, command: string, arg: any, cancellationToken: CancellationToken): Promise<any> {
37
switch (command) {
38
case 'testMethodClient2': return Promise.resolve('success2');
39
default: return Promise.reject(new Error('not implemented'));
40
}
41
},
42
43
listen(_: unknown, event: string, arg?: any): Event<any> {
44
switch (event) {
45
default: throw new Error('not implemented');
46
}
47
}
48
});
49
50
const channelClient1 = client2.getChannel('client1');
51
assert.strictEqual(await channelClient1.call('testMethodClient1'), 'success1');
52
53
const channelClient2 = client1.getChannel('client2');
54
assert.strictEqual(await channelClient2.call('testMethodClient2'), 'success2');
55
56
client1.dispose();
57
client2.dispose();
58
});
59
60
ensureNoDisposablesAreLeakedInTestSuite();
61
});
62
63