summaryrefslogtreecommitdiff
path: root/addons/bus/static/tests/bus_tests.js
blob: 449e3ba2c93368882625fe389a6ef7a1b37ff2ca (plain)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
odoo.define('web.bus_tests', function (require) {
"use strict";

var BusService = require('bus.BusService');
var CrossTabBus = require('bus.CrossTab');
var AbstractStorageService = require('web.AbstractStorageService');
var RamStorage = require('web.RamStorage');
var testUtils = require('web.test_utils');
var Widget = require('web.Widget');


var LocalStorageServiceMock;

BusService = BusService.extend({
    TAB_HEARTBEAT_PERIOD: 10,
    MASTER_TAB_HEARTBEAT_PERIOD: 1,
});


QUnit.module('Bus', {
    beforeEach: function () {
        LocalStorageServiceMock = AbstractStorageService.extend({storage: new RamStorage()});
    },
}, function () {
    QUnit.test('notifications received from the longpolling channel', async function (assert) {
        assert.expect(6);

        var pollPromise = testUtils.makeTestPromise();

        var parent = new Widget();
        await testUtils.mock.addMockEnvironment(parent, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    assert.step(route + ' - ' + args.channels.join(','));

                    pollPromise = testUtils.makeTestPromise();
                    pollPromise.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromise);
                    return pollPromise;
                }
                return this._super.apply(this, arguments);
            }
        });

        var widget = new Widget(parent);
        await widget.appendTo($('#qunit-fixture'));

        widget.call('bus_service', 'onNotification', this, function (notifications) {
            assert.step('notification - ' + notifications.toString());
        });
        widget.call('bus_service', 'addChannel', 'lambda');

        pollPromise.resolve([{
            id: 1,
            channel: 'lambda',
            message: 'beta',
        }]);
        await testUtils.nextTick();

        pollPromise.resolve([{
            id: 2,
            channel: 'lambda',
            message: 'epsilon',
        }]);
        await testUtils.nextTick();

        assert.verifySteps([
            '/longpolling/poll - lambda',
            'notification - lambda,beta',
            '/longpolling/poll - lambda',
            'notification - lambda,epsilon',
            '/longpolling/poll - lambda',
        ]);

        parent.destroy();
    });

    QUnit.test('provide notification ID of 0 by default', async function (assert) {
        // This test is important in order to ensure that we provide the correct
        // sentinel value 0 when we are not aware of the last notification ID
        // that we have received. We cannot provide an ID of -1, otherwise it
        // may likely be handled incorrectly (before this test was written,
        // it was providing -1 to the server, which in return sent every stored
        // notifications related to this user).
        assert.expect(3);

        // Simulate no ID of last notification in the local storage
        testUtils.mock.patch(LocalStorageServiceMock, {
            getItem: function (key) {
                if (key === 'last_ts') {
                    return 0;
                }
                return this._super.apply(this, arguments);
            },
        });

        var pollPromise = testUtils.makeTestPromise();
        var parent = new Widget();
        await testUtils.mock.addMockEnvironment(parent, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    assert.step(route);
                    assert.strictEqual(args.last, 0,
                        "provided last notification ID should be 0");

                    pollPromise = testUtils.makeTestPromise();
                    pollPromise.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromise);
                    return pollPromise;
                }
                return this._super.apply(this, arguments);
            }
        });

        var widget = new Widget(parent);
        await widget.appendTo($('#qunit-fixture'));

        // trigger longpolling poll RPC
        widget.call('bus_service', 'addChannel', 'lambda');
        assert.verifySteps(['/longpolling/poll']);

        testUtils.mock.unpatch(LocalStorageServiceMock);
        parent.destroy();
    });

    QUnit.test('cross tab bus share message from a channel', async function (assert) {
        assert.expect(5);

        // master

        var pollPromiseMaster = testUtils.makeTestPromise();

        var parentMaster = new Widget();
        await testUtils.mock.addMockEnvironment(parentMaster, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    assert.step('master' + ' - ' + route + ' - ' + args.channels.join(','));

                    pollPromiseMaster = testUtils.makeTestPromise();
                    pollPromiseMaster.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromiseMaster);
                    return pollPromiseMaster;
                }
                return this._super.apply(this, arguments);
            }
        });

        var master = new Widget(parentMaster);
        await master.appendTo($('#qunit-fixture'));

        master.call('bus_service', 'onNotification', master, function (notifications) {
            assert.step('master - notification - ' + notifications.toString());
        });
        master.call('bus_service', 'addChannel', 'lambda');

        // slave
        await testUtils.nextTick();
        var parentSlave = new Widget();
        await testUtils.mock.addMockEnvironment(parentSlave, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    throw new Error("Can not use the longpolling of the slave client");
                }
                return this._super.apply(this, arguments);
            }
        });

        var slave = new Widget(parentSlave);
        await slave.appendTo($('#qunit-fixture'));

        slave.call('bus_service', 'onNotification', slave, function (notifications) {
            assert.step('slave - notification - ' + notifications.toString());
        });
        slave.call('bus_service', 'addChannel', 'lambda');

        pollPromiseMaster.resolve([{
            id: 1,
            channel: 'lambda',
            message: 'beta',
        }]);
        await testUtils.nextTick();

        assert.verifySteps([
            'master - /longpolling/poll - lambda',
            'master - notification - lambda,beta',
            'slave - notification - lambda,beta',
            'master - /longpolling/poll - lambda',
        ]);

        parentMaster.destroy();
        parentSlave.destroy();
    });

    QUnit.test('cross tab bus elect new master on master unload', async function (assert) {
        assert.expect(8);

        // master
        var pollPromiseMaster = testUtils.makeTestPromise();

        var parentMaster = new Widget();
        await testUtils.mock.addMockEnvironment(parentMaster, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    assert.step('master - ' + route + ' - ' + args.channels.join(','));

                    pollPromiseMaster = testUtils.makeTestPromise();
                    pollPromiseMaster.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromiseMaster);
                    return pollPromiseMaster;
                }
                return this._super.apply(this, arguments);
            }
        });

        var master = new Widget(parentMaster);
        await master.appendTo($('#qunit-fixture'));

        master.call('bus_service', 'onNotification', master, function (notifications) {
            assert.step('master - notification - ' + notifications.toString());
        });
        master.call('bus_service', 'addChannel', 'lambda');

        // slave
        await testUtils.nextTick();
        var parentSlave = new Widget();
        var pollPromiseSlave = testUtils.makeTestPromise();
        await testUtils.mock.addMockEnvironment(parentSlave, {
            data: {},
            services: {
                bus_service: BusService,
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route, args) {
                if (route === '/longpolling/poll') {
                    assert.step('slave - ' + route + ' - ' + args.channels.join(','));

                    pollPromiseSlave = testUtils.makeTestPromise();
                    pollPromiseSlave.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromiseSlave);
                    return pollPromiseSlave;
                }
                return this._super.apply(this, arguments);
            }
        });

        var slave = new Widget(parentSlave);
        await slave.appendTo($('#qunit-fixture'));

        slave.call('bus_service', 'onNotification', slave, function (notifications) {
            assert.step('slave - notification - ' + notifications.toString());
        });
        slave.call('bus_service', 'addChannel', 'lambda');

        pollPromiseMaster.resolve([{
            id: 1,
            channel: 'lambda',
            message: 'beta',
        }]);
        await testUtils.nextTick();

        // simulate unloading master
        master.call('bus_service', '_onUnload');

        pollPromiseSlave.resolve([{
            id: 2,
            channel: 'lambda',
            message: 'gamma',
        }]);
        await testUtils.nextTick();

        assert.verifySteps([
            'master - /longpolling/poll - lambda',
            'master - notification - lambda,beta',
            'slave - notification - lambda,beta',
            'master - /longpolling/poll - lambda',
            'slave - /longpolling/poll - lambda',
            'slave - notification - lambda,gamma',
            'slave - /longpolling/poll - lambda',
        ]);

        parentMaster.destroy();
        parentSlave.destroy();
    });

    QUnit.test('two tabs calling addChannel simultaneously', async function (assert) {
        assert.expect(5);

        let id = 1;
        testUtils.patch(CrossTabBus, {
            init: function () {
                this._super.apply(this, arguments);
                this.__tabId__ = id++;
            },
            addChannel: function (channel) {
                assert.step('Tab ' + this.__tabId__ + ': addChannel ' + channel);
                this._super.apply(this, arguments);
            },
            deleteChannel: function (channel) {
                assert.step('Tab ' + this.__tabId__ + ': deleteChannel ' + channel);
                this._super.apply(this, arguments);
            },
        });

        let pollPromise;
        const parentTab1 = new Widget();
        await testUtils.addMockEnvironment(parentTab1, {
            data: {},
            services: {
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route) {
                if (route === '/longpolling/poll') {
                    pollPromise = testUtils.makeTestPromise();
                    pollPromise.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromise);
                    return pollPromise;
                }
                return this._super.apply(this, arguments);
            }
        });
        const parentTab2 = new Widget();
        await testUtils.addMockEnvironment(parentTab2, {
            data: {},
            services: {
                local_storage: LocalStorageServiceMock,
            },
            mockRPC: function (route) {
                if (route === '/longpolling/poll') {
                    pollPromise = testUtils.makeTestPromise();
                    pollPromise.abort = (function () {
                        this.reject({message: "XmlHttpRequestError abort"}, $.Event());
                    }).bind(pollPromise);
                    return pollPromise;
                }
                return this._super.apply(this, arguments);
            }
        });

        const tab1 = new CrossTabBus(parentTab1);
        const tab2 = new CrossTabBus(parentTab2);

        tab1.addChannel("alpha");
        tab2.addChannel("alpha");
        tab1.addChannel("beta");
        tab2.addChannel("beta");

        assert.verifySteps([
            "Tab 1: addChannel alpha",
            "Tab 2: addChannel alpha",
            "Tab 1: addChannel beta",
            "Tab 2: addChannel beta",
        ]);

        testUtils.unpatch(CrossTabBus);
        parentTab1.destroy();
        parentTab2.destroy();
    });
});

});