UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2// *****************************************************************************
3// Copyright (C) 2017 TypeFox and others.
4//
5// This program and the accompanying materials are made available under the
6// terms of the Eclipse Public License v. 2.0 which is available at
7// http://www.eclipse.org/legal/epl-2.0.
8//
9// This Source Code may also be made available under the following Secondary
10// Licenses when the conditions for such availability set forth in the Eclipse
11// Public License v. 2.0 are satisfied: GNU General Public License, version 2
12// with the GNU Classpath Exception which is available at
13// https://www.gnu.org/software/classpath/license.html.
14//
15// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
16// *****************************************************************************
17Object.defineProperty(exports, "__esModule", { value: true });
18const chai = require("chai");
19const proxy_factory_1 = require("./proxy-factory");
20const channel_spec_1 = require("../message-rpc/channel.spec");
21const expect = chai.expect;
22class TestServer {
23 constructor() {
24 this.requests = [];
25 }
26 doStuff(arg) {
27 this.requests.push(arg);
28 return Promise.resolve(`done: ${arg}`);
29 }
30 fails(arg, otherArg) {
31 throw new Error('fails failed');
32 }
33 fails2(arg, otherArg) {
34 return Promise.reject(new Error('fails2 failed'));
35 }
36}
37class TestClient {
38 constructor() {
39 this.notifications = [];
40 }
41 notifyThat(arg) {
42 this.notifications.push(arg);
43 }
44}
45describe('Proxy-Factory', () => {
46 it('Should correctly send notifications and requests.', done => {
47 const it = getSetup();
48 it.clientProxy.notifyThat('hello');
49 function check() {
50 if (it.client.notifications.length === 0) {
51 console.log('waiting another 50 ms');
52 setTimeout(check, 50);
53 }
54 else {
55 expect(it.client.notifications[0]).eq('hello');
56 it.serverProxy.doStuff('foo').then(result => {
57 expect(result).to.be.eq('done: foo');
58 done();
59 });
60 }
61 }
62 check();
63 });
64 it('Rejected Promise should result in rejected Promise.', done => {
65 const it = getSetup();
66 const handle = setTimeout(() => done('timeout'), 500);
67 it.serverProxy.fails('a', 'b').catch(err => {
68 expect(err.message).to.contain('fails failed');
69 clearTimeout(handle);
70 done();
71 });
72 });
73 it('Remote Exceptions should result in rejected Promise.', done => {
74 const { serverProxy } = getSetup();
75 const handle = setTimeout(() => done('timeout'), 500);
76 serverProxy.fails2('a', 'b').catch(err => {
77 expect(err.message).to.contain('fails2 failed');
78 clearTimeout(handle);
79 done();
80 });
81 });
82});
83function getSetup() {
84 const client = new TestClient();
85 const server = new TestServer();
86 const serverProxyFactory = new proxy_factory_1.RpcProxyFactory(client);
87 const pipe = new channel_spec_1.ChannelPipe();
88 serverProxyFactory.listen(pipe.right);
89 const serverProxy = serverProxyFactory.createProxy();
90 const clientProxyFactory = new proxy_factory_1.RpcProxyFactory(server);
91 clientProxyFactory.listen(pipe.left);
92 const clientProxy = clientProxyFactory.createProxy();
93 return {
94 client,
95 clientProxy,
96 server,
97 serverProxy
98 };
99}
100//# sourceMappingURL=proxy-factory.spec.js.map
\No newline at end of file