Skip to content

Commit fc7f575

Browse files
author
Benjamin Pasero
committed
ipc - add tests for channel proxy
1 parent fa9c39a commit fc7f575

1 file changed

Lines changed: 123 additions & 8 deletions

File tree

src/vs/base/parts/ipc/test/node/ipc.test.ts

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cance
1010
import { canceled } from 'vs/base/common/errors';
1111
import { timeout } from 'vs/base/common/async';
1212
import { VSBuffer } from 'vs/base/common/buffer';
13+
import { createChannelSender, createChannelReceiver } from 'vs/base/parts/ipc/node/ipcChannelCreator';
14+
import { URI } from 'vs/base/common/uri';
15+
import { isEqual } from 'vs/base/common/resources';
1316

1417
class QueueProtocol implements IMessagePassingProtocol {
1518

@@ -101,14 +104,16 @@ interface ITestService {
101104
neverComplete(): Promise<void>;
102105
neverCompleteCT(cancellationToken: CancellationToken): Promise<void>;
103106
buffersLength(buffers: Buffer[]): Promise<number>;
107+
marshall(uri: URI): Promise<URI>;
108+
context(): Promise<unknown>;
104109

105-
pong: Event<string>;
110+
onPong: Event<string>;
106111
}
107112

108113
class TestService implements ITestService {
109114

110-
private readonly _pong = new Emitter<string>();
111-
readonly pong = this._pong.event;
115+
private readonly _onPong = new Emitter<string>();
116+
readonly onPong = this._onPong.event;
112117

113118
marco(): Promise<string> {
114119
return Promise.resolve('polo');
@@ -135,7 +140,15 @@ class TestService implements ITestService {
135140
}
136141

137142
ping(msg: string): void {
138-
this._pong.fire(msg);
143+
this._onPong.fire(msg);
144+
}
145+
146+
marshall(uri: URI): Promise<URI> {
147+
return Promise.resolve(uri);
148+
}
149+
150+
context(context?: unknown): Promise<unknown> {
151+
return Promise.resolve(context);
139152
}
140153
}
141154

@@ -156,16 +169,16 @@ class TestChannel implements IServerChannel {
156169

157170
listen(_: unknown, event: string, arg?: any): Event<any> {
158171
switch (event) {
159-
case 'pong': return this.service.pong;
172+
case 'onPong': return this.service.onPong;
160173
default: throw new Error('not implemented');
161174
}
162175
}
163176
}
164177

165178
class TestChannelClient implements ITestService {
166179

167-
get pong(): Event<string> {
168-
return this.channel.listen('pong');
180+
get onPong(): Event<string> {
181+
return this.channel.listen('onPong');
169182
}
170183

171184
constructor(private channel: IChannel) { }
@@ -189,6 +202,14 @@ class TestChannelClient implements ITestService {
189202
buffersLength(buffers: Buffer[]): Promise<number> {
190203
return this.channel.call('buffersLength', buffers);
191204
}
205+
206+
marshall(uri: URI): Promise<URI> {
207+
return this.channel.call('marshall', uri);
208+
}
209+
210+
context(): Promise<unknown> {
211+
return this.channel.call('context');
212+
}
192213
}
193214

194215
suite('Base IPC', function () {
@@ -281,7 +302,7 @@ suite('Base IPC', function () {
281302
test('listen to events', async function () {
282303
const messages: string[] = [];
283304

284-
ipcService.pong(msg => messages.push(msg));
305+
ipcService.onPong(msg => messages.push(msg));
285306
await timeout(0);
286307

287308
assert.deepEqual(messages, []);
@@ -300,4 +321,98 @@ suite('Base IPC', function () {
300321
return assert.equal(r, 5);
301322
});
302323
});
324+
325+
suite('one to one (proxy)', function () {
326+
let server: IPCServer;
327+
let client: IPCClient;
328+
let service: TestService;
329+
let ipcService: ITestService;
330+
331+
setup(function () {
332+
service = new TestService();
333+
const testServer = new TestIPCServer();
334+
server = testServer;
335+
336+
server.registerChannel(TestChannelId, createChannelReceiver(service));
337+
338+
client = testServer.createConnection('client1');
339+
ipcService = createChannelSender(client.getChannel(TestChannelId));
340+
});
341+
342+
teardown(function () {
343+
client.dispose();
344+
server.dispose();
345+
});
346+
347+
test('call success', async function () {
348+
const r = await ipcService.marco();
349+
return assert.equal(r, 'polo');
350+
});
351+
352+
test('call error', async function () {
353+
try {
354+
await ipcService.error('nice error');
355+
return assert.fail('should not reach here');
356+
} catch (err) {
357+
return assert.equal(err.message, 'nice error');
358+
}
359+
});
360+
361+
test('listen to events', async function () {
362+
const messages: string[] = [];
363+
364+
ipcService.onPong(msg => messages.push(msg));
365+
await timeout(0);
366+
367+
assert.deepEqual(messages, []);
368+
service.ping('hello');
369+
await timeout(0);
370+
371+
assert.deepEqual(messages, ['hello']);
372+
service.ping('world');
373+
await timeout(0);
374+
375+
assert.deepEqual(messages, ['hello', 'world']);
376+
});
377+
378+
test('marshalling uri', async function () {
379+
const uri = URI.file('foobar');
380+
const r = await ipcService.marshall(uri);
381+
assert.ok(r instanceof URI);
382+
return assert.ok(isEqual(r, uri));
383+
});
384+
385+
test('buffers in arrays', async function () {
386+
const r = await ipcService.buffersLength([Buffer.allocUnsafe(2), Buffer.allocUnsafe(3)]);
387+
return assert.equal(r, 5);
388+
});
389+
});
390+
391+
suite('one to one (proxy, extra context)', function () {
392+
let server: IPCServer;
393+
let client: IPCClient;
394+
let service: TestService;
395+
let ipcService: ITestService;
396+
397+
setup(function () {
398+
service = new TestService();
399+
const testServer = new TestIPCServer();
400+
server = testServer;
401+
402+
server.registerChannel(TestChannelId, createChannelReceiver(service));
403+
404+
client = testServer.createConnection('client1');
405+
ipcService = createChannelSender(client.getChannel(TestChannelId), { context: 'Super Context' });
406+
});
407+
408+
teardown(function () {
409+
client.dispose();
410+
server.dispose();
411+
});
412+
413+
test('call extra context', async function () {
414+
const r = await ipcService.context();
415+
return assert.equal(r, 'Super Context');
416+
});
417+
});
303418
});

0 commit comments

Comments
 (0)