提交 fc7f5753 编写于 作者: B Benjamin Pasero

ipc - add tests for channel proxy

上级 fa9c39ae
......@@ -10,6 +10,9 @@ import { CancellationToken, CancellationTokenSource } from 'vs/base/common/cance
import { canceled } from 'vs/base/common/errors';
import { timeout } from 'vs/base/common/async';
import { VSBuffer } from 'vs/base/common/buffer';
import { createChannelSender, createChannelReceiver } from 'vs/base/parts/ipc/node/ipcChannelCreator';
import { URI } from 'vs/base/common/uri';
import { isEqual } from 'vs/base/common/resources';
class QueueProtocol implements IMessagePassingProtocol {
......@@ -101,14 +104,16 @@ interface ITestService {
neverComplete(): Promise<void>;
neverCompleteCT(cancellationToken: CancellationToken): Promise<void>;
buffersLength(buffers: Buffer[]): Promise<number>;
marshall(uri: URI): Promise<URI>;
context(): Promise<unknown>;
pong: Event<string>;
onPong: Event<string>;
}
class TestService implements ITestService {
private readonly _pong = new Emitter<string>();
readonly pong = this._pong.event;
private readonly _onPong = new Emitter<string>();
readonly onPong = this._onPong.event;
marco(): Promise<string> {
return Promise.resolve('polo');
......@@ -135,7 +140,15 @@ class TestService implements ITestService {
}
ping(msg: string): void {
this._pong.fire(msg);
this._onPong.fire(msg);
}
marshall(uri: URI): Promise<URI> {
return Promise.resolve(uri);
}
context(context?: unknown): Promise<unknown> {
return Promise.resolve(context);
}
}
......@@ -156,7 +169,7 @@ class TestChannel implements IServerChannel {
listen(_: unknown, event: string, arg?: any): Event<any> {
switch (event) {
case 'pong': return this.service.pong;
case 'onPong': return this.service.onPong;
default: throw new Error('not implemented');
}
}
......@@ -164,8 +177,8 @@ class TestChannel implements IServerChannel {
class TestChannelClient implements ITestService {
get pong(): Event<string> {
return this.channel.listen('pong');
get onPong(): Event<string> {
return this.channel.listen('onPong');
}
constructor(private channel: IChannel) { }
......@@ -189,6 +202,14 @@ class TestChannelClient implements ITestService {
buffersLength(buffers: Buffer[]): Promise<number> {
return this.channel.call('buffersLength', buffers);
}
marshall(uri: URI): Promise<URI> {
return this.channel.call('marshall', uri);
}
context(): Promise<unknown> {
return this.channel.call('context');
}
}
suite('Base IPC', function () {
......@@ -281,7 +302,7 @@ suite('Base IPC', function () {
test('listen to events', async function () {
const messages: string[] = [];
ipcService.pong(msg => messages.push(msg));
ipcService.onPong(msg => messages.push(msg));
await timeout(0);
assert.deepEqual(messages, []);
......@@ -300,4 +321,98 @@ suite('Base IPC', function () {
return assert.equal(r, 5);
});
});
suite('one to one (proxy)', function () {
let server: IPCServer;
let client: IPCClient;
let service: TestService;
let ipcService: ITestService;
setup(function () {
service = new TestService();
const testServer = new TestIPCServer();
server = testServer;
server.registerChannel(TestChannelId, createChannelReceiver(service));
client = testServer.createConnection('client1');
ipcService = createChannelSender(client.getChannel(TestChannelId));
});
teardown(function () {
client.dispose();
server.dispose();
});
test('call success', async function () {
const r = await ipcService.marco();
return assert.equal(r, 'polo');
});
test('call error', async function () {
try {
await ipcService.error('nice error');
return assert.fail('should not reach here');
} catch (err) {
return assert.equal(err.message, 'nice error');
}
});
test('listen to events', async function () {
const messages: string[] = [];
ipcService.onPong(msg => messages.push(msg));
await timeout(0);
assert.deepEqual(messages, []);
service.ping('hello');
await timeout(0);
assert.deepEqual(messages, ['hello']);
service.ping('world');
await timeout(0);
assert.deepEqual(messages, ['hello', 'world']);
});
test('marshalling uri', async function () {
const uri = URI.file('foobar');
const r = await ipcService.marshall(uri);
assert.ok(r instanceof URI);
return assert.ok(isEqual(r, uri));
});
test('buffers in arrays', async function () {
const r = await ipcService.buffersLength([Buffer.allocUnsafe(2), Buffer.allocUnsafe(3)]);
return assert.equal(r, 5);
});
});
suite('one to one (proxy, extra context)', function () {
let server: IPCServer;
let client: IPCClient;
let service: TestService;
let ipcService: ITestService;
setup(function () {
service = new TestService();
const testServer = new TestIPCServer();
server = testServer;
server.registerChannel(TestChannelId, createChannelReceiver(service));
client = testServer.createConnection('client1');
ipcService = createChannelSender(client.getChannel(TestChannelId), { context: 'Super Context' });
});
teardown(function () {
client.dispose();
server.dispose();
});
test('call extra context', async function () {
const r = await ipcService.context();
return assert.equal(r, 'Super Context');
});
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册