1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | const chai = require("chai");
|
19 | const proxy_factory_1 = require("./proxy-factory");
|
20 | const channel_spec_1 = require("../message-rpc/channel.spec");
|
21 | const expect = chai.expect;
|
22 | class 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 | }
|
37 | class TestClient {
|
38 | constructor() {
|
39 | this.notifications = [];
|
40 | }
|
41 | notifyThat(arg) {
|
42 | this.notifications.push(arg);
|
43 | }
|
44 | }
|
45 | describe('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 | });
|
83 | function 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 |
|
\ | No newline at end of file |