This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
multi-runtime-channel.test.ts
176 lines (156 loc) · 7 KB
/
multi-runtime-channel.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* tslint:disable:no-invalid-this no-function-expression insecure-random mocha-no-side-effect-code no-empty */
import * as assert from 'assert';
import { cleanOpenRuntimes, launchAndConnect, TEST_TIMEOUT, DELAY_MS } from './multi-runtime-utils';
import { delayPromise } from './delay-promise';
import * as fs from 'fs';
import * as path from 'path';
import * as sinon from 'sinon';
import { conn } from './connect';
import Fin from '../src/api/fin';
describe ('Multi Runtime Channels', function() {
this.retries(2);
this.slow(TEST_TIMEOUT / 2 );
this.timeout(TEST_TIMEOUT);
let fin: Fin;
const appConfig = JSON.parse(fs.readFileSync(path.resolve('test/app.json')).toString());
before(async () => {
fin = await conn();
});
beforeEach(async () => {
return await cleanOpenRuntimes();
});
describe('Multi Runtime with External Provider', function () {
it('Should work in multi runtime with an external Provider', function(done: any) {
// tslint:disable-next-line no-invalid-this
const url = appConfig.startup_app.url;
const newUrl = url.slice(0, url.lastIndexOf('/')) + '/client.html';
const clientConfig = {
'name': 'channel-client-test',
'url': newUrl,
'uuid': 'channel-client-test',
'autoShow': true,
'saveWindowState': false,
'nonPersistent': true,
'experimental': {
'v2Api': true
}
};
async function test () {
const spy = sinon.spy();
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
const provider = await finB.InterApplicationBus.Channel.create('test');
provider.register('test', () => {
spy();
return 'return-test';
});
provider.onConnection(c => {
spy();
});
const client = await finA.Application.create(clientConfig);
await client.run();
await delayPromise(DELAY_MS);
await finA.InterApplicationBus.subscribe({uuid: 'channel-client-test'}, 'return', (msg: any) => {
assert.ok(spy.calledTwice, 'Did not get IAB from dispatch');
assert.equal(msg, 'return-test');
done();
});
await finB.InterApplicationBus.publish('start', 'hi');
}
test().catch(() => cleanOpenRuntimes());
});
});
describe('Multi Runtime with External Client', function () {
it('Should work in multi runtime with an External Client', function(done: any) {
const url = appConfig.startup_app.url;
const newUrl = url.slice(0, url.lastIndexOf('/')) + '/service.html';
const serviceConfig = {
'name': 'channel-provider-test',
'url': newUrl,
'uuid': 'channel-provider-test',
'autoShow': true,
'saveWindowState': false,
'nonPersistent': true,
'experimental': {
'v2Api': true
}
};
async function test() {
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
const service = await finA.Application.create(serviceConfig);
await service.run();
await delayPromise(DELAY_MS);
const client = await finB.InterApplicationBus.Channel.connect('test');
client.register('multi-runtime-test', (r: string) => {
assert.equal(r, 'return-mrt', 'wrong payload sent from service');
done();
});
client.dispatch('test').then(res => {
assert.equal(res, 'return-test', 'wrong return payload from service');
});
}
test().catch(() => cleanOpenRuntimes());
});
});
describe('Multi Runtime getAllChannels', function () {
it('Should get all channels from across multiple runtimes', function(done: any) {
const url = appConfig.startup_app.url;
const newUrl = url.slice(0, url.lastIndexOf('/')) + '/service.html';
const serviceConfig = {
'name': 'channel-provider-test',
'url': newUrl,
'uuid': 'channel-provider-test',
'autoShow': true,
'saveWindowState': false,
'nonPersistent': true,
'experimental': {
'v2Api': true
}
};
async function test() {
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
const service = await finA.Application.create(serviceConfig);
await service.run();
await finB.InterApplicationBus.Channel.create('test-channel-multi-runtime');
await delayPromise(DELAY_MS * 3);
const allChannels = await fin.InterApplicationBus.Channel.getAllChannels();
assert.equal(allChannels.length, 2, `expected 2 channels in allChannels: ${JSON.stringify(allChannels)}`);
done();
}
test().catch(() => cleanOpenRuntimes());
});
});
describe('Multi Runtime connect before channel creation with wait', function () {
it('Should be able to connect before channel creation', function(done: any) {
const url = appConfig.startup_app.url;
const newUrl = url.slice(0, url.lastIndexOf('/')) + '/service.html';
const serviceConfig = {
'name': 'channel-provider-mrtest',
'url': newUrl,
'uuid': 'channel-provider-mrtest',
'autoShow': true,
'saveWindowState': false,
'nonPersistent': true,
'experimental': {
'v2Api': true
}
};
async function test() {
const [finA, finB] = await Promise.all([launchAndConnect(), launchAndConnect()]);
finB.InterApplicationBus.Channel.connect('mrtest')
.then((c) => {
c.register('multi-runtime-test', (r: string) => {
assert.equal(r, 'return-mrt', 'wrong payload sent from service');
done();
});
c.dispatch('test').then((res: any) => {
assert.equal(res, 'return-test', 'wrong return payload from service');
});
});
await delayPromise(DELAY_MS * 2);
const service = await finA.Application.create(serviceConfig);
await service.run();
}
test().catch(() => cleanOpenRuntimes());
});
});
});