Skip to content
This repository was archived by the owner on Jun 19, 2023. It is now read-only.

Commit 83a975a

Browse files
authored
DEV: Await for all async MessageBus callbacks (#17966)
Should take care of yet another category of flaky tests
1 parent 129885c commit 83a975a

File tree

4 files changed

+16
-26
lines changed

4 files changed

+16
-26
lines changed

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "eslint-config-discourse",
33
"rules": {
44
"discourse-ember/global-ember": 2,
5-
"eol-last": 2
5+
"eol-last": 2,
6+
"no-restricted-globals": 0
67
},
78
"globals": {
89
"_": "off",

app/assets/javascripts/discourse/app/services/presence.js

+5-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Service from "@ember/service";
22
import EmberObject, { computed } from "@ember/object";
33
import { ajax } from "discourse/lib/ajax";
4-
import { cancel, debounce, next, once, run, throttle } from "@ember/runloop";
4+
import { cancel, debounce, next, once, throttle } from "@ember/runloop";
55
import discourseLater from "discourse-common/lib/later";
66
import Session from "discourse/models/session";
77
import { Promise } from "rsvp";
@@ -170,15 +170,13 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
170170

171171
this.lastSeenId = initialData.last_message_id;
172172

173-
let callback = (data, global_id, message_id) =>
174-
run(() => this._processMessage(data, global_id, message_id));
175173
this.presenceService.messageBus.subscribe(
176174
`/presence${this.name}`,
177-
callback,
175+
this._processMessage,
178176
this.lastSeenId
179177
);
180178

181-
this.set("_subscribedCallback", callback);
179+
this.set("_subscribedCallback", this._processMessage);
182180
this.trigger("change");
183181
}
184182

@@ -198,12 +196,10 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
198196

199197
async _resubscribe() {
200198
this.unsubscribe();
201-
// Stored at object level for tests to hook in
202-
this._resubscribePromise = this.subscribe();
203-
await this._resubscribePromise;
204-
delete this._resubscribePromise;
199+
await this.subscribe();
205200
}
206201

202+
@bind
207203
async _processMessage(data, global_id, message_id) {
208204
if (message_id !== this.lastSeenId + 1) {
209205
// eslint-disable-next-line no-console

app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { getApplication, getContext, settled } from "@ember/test-helpers";
1515
import { getOwner } from "discourse-common/lib/get-owner";
1616
import { run } from "@ember/runloop";
1717
import { setupApplicationTest } from "ember-qunit";
18-
import { Promise } from "rsvp";
1918
import Site from "discourse/models/site";
2019
import User from "discourse/models/user";
2120
import { _clearSnapshots } from "select-kit/components/composer-actions";
@@ -448,15 +447,11 @@ QUnit.assert.containsInstance = function (collection, klass, message) {
448447
};
449448

450449
export async function selectDate(selector, date) {
451-
return new Promise((resolve) => {
452-
const elem = document.querySelector(selector);
453-
elem.value = date;
454-
const evt = new Event("input", { bubbles: true, cancelable: false });
455-
elem.dispatchEvent(evt);
456-
elem.blur();
457-
458-
resolve();
459-
});
450+
const elem = document.querySelector(selector);
451+
elem.value = date;
452+
const evt = new Event("input", { bubbles: true, cancelable: false });
453+
elem.dispatchEvent(evt);
454+
elem.blur();
460455
}
461456

462457
export function queryAll(selector, context) {
@@ -491,10 +486,12 @@ export function exists(selector) {
491486

492487
export async function publishToMessageBus(channelPath, ...args) {
493488
args = cloneJSON(args);
494-
MessageBus.callbacks
489+
490+
const promises = MessageBus.callbacks
495491
.filterBy("channel", channelPath)
496-
.forEach((c) => c.func(...args));
492+
.map((callback) => callback.func(...args));
497493

494+
await Promise.allSettled(promises);
498495
await settled();
499496
}
500497

app/assets/javascripts/discourse/tests/unit/services/presence-test.js

-4
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,6 @@ acceptance("Presence - Subscribing", function (needs) {
138138
99
139139
);
140140

141-
await channel._presenceState._resubscribePromise;
142-
143141
sinon.assert.calledOnce(stub);
144142
assert.strictEqual(
145143
channel.users.length,
@@ -189,8 +187,6 @@ acceptance("Presence - Subscribing", function (needs) {
189187
3
190188
);
191189

192-
await channel._presenceState._resubscribePromise;
193-
194190
assert.strictEqual(
195191
channel.count,
196192
3,

0 commit comments

Comments
 (0)