DEV: Await for all async MessageBus callbacks (#17966)
Should take care of yet another category of flaky tests
This commit is contained in:
parent
129885c260
commit
83a975a28b
|
@ -2,7 +2,8 @@
|
||||||
"extends": "eslint-config-discourse",
|
"extends": "eslint-config-discourse",
|
||||||
"rules": {
|
"rules": {
|
||||||
"discourse-ember/global-ember": 2,
|
"discourse-ember/global-ember": 2,
|
||||||
"eol-last": 2
|
"eol-last": 2,
|
||||||
|
"no-restricted-globals": 0
|
||||||
},
|
},
|
||||||
"globals": {
|
"globals": {
|
||||||
"_": "off",
|
"_": "off",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import Service from "@ember/service";
|
import Service from "@ember/service";
|
||||||
import EmberObject, { computed } from "@ember/object";
|
import EmberObject, { computed } from "@ember/object";
|
||||||
import { ajax } from "discourse/lib/ajax";
|
import { ajax } from "discourse/lib/ajax";
|
||||||
import { cancel, debounce, next, once, run, throttle } from "@ember/runloop";
|
import { cancel, debounce, next, once, throttle } from "@ember/runloop";
|
||||||
import discourseLater from "discourse-common/lib/later";
|
import discourseLater from "discourse-common/lib/later";
|
||||||
import Session from "discourse/models/session";
|
import Session from "discourse/models/session";
|
||||||
import { Promise } from "rsvp";
|
import { Promise } from "rsvp";
|
||||||
|
@ -170,15 +170,13 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
|
||||||
|
|
||||||
this.lastSeenId = initialData.last_message_id;
|
this.lastSeenId = initialData.last_message_id;
|
||||||
|
|
||||||
let callback = (data, global_id, message_id) =>
|
|
||||||
run(() => this._processMessage(data, global_id, message_id));
|
|
||||||
this.presenceService.messageBus.subscribe(
|
this.presenceService.messageBus.subscribe(
|
||||||
`/presence${this.name}`,
|
`/presence${this.name}`,
|
||||||
callback,
|
this._processMessage,
|
||||||
this.lastSeenId
|
this.lastSeenId
|
||||||
);
|
);
|
||||||
|
|
||||||
this.set("_subscribedCallback", callback);
|
this.set("_subscribedCallback", this._processMessage);
|
||||||
this.trigger("change");
|
this.trigger("change");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,12 +196,10 @@ class PresenceChannelState extends EmberObject.extend(Evented) {
|
||||||
|
|
||||||
async _resubscribe() {
|
async _resubscribe() {
|
||||||
this.unsubscribe();
|
this.unsubscribe();
|
||||||
// Stored at object level for tests to hook in
|
await this.subscribe();
|
||||||
this._resubscribePromise = this.subscribe();
|
|
||||||
await this._resubscribePromise;
|
|
||||||
delete this._resubscribePromise;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@bind
|
||||||
async _processMessage(data, global_id, message_id) {
|
async _processMessage(data, global_id, message_id) {
|
||||||
if (message_id !== this.lastSeenId + 1) {
|
if (message_id !== this.lastSeenId + 1) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
|
|
|
@ -15,7 +15,6 @@ import { getApplication, getContext, settled } from "@ember/test-helpers";
|
||||||
import { getOwner } from "discourse-common/lib/get-owner";
|
import { getOwner } from "discourse-common/lib/get-owner";
|
||||||
import { run } from "@ember/runloop";
|
import { run } from "@ember/runloop";
|
||||||
import { setupApplicationTest } from "ember-qunit";
|
import { setupApplicationTest } from "ember-qunit";
|
||||||
import { Promise } from "rsvp";
|
|
||||||
import Site from "discourse/models/site";
|
import Site from "discourse/models/site";
|
||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import { _clearSnapshots } from "select-kit/components/composer-actions";
|
import { _clearSnapshots } from "select-kit/components/composer-actions";
|
||||||
|
@ -448,15 +447,11 @@ QUnit.assert.containsInstance = function (collection, klass, message) {
|
||||||
};
|
};
|
||||||
|
|
||||||
export async function selectDate(selector, date) {
|
export async function selectDate(selector, date) {
|
||||||
return new Promise((resolve) => {
|
const elem = document.querySelector(selector);
|
||||||
const elem = document.querySelector(selector);
|
elem.value = date;
|
||||||
elem.value = date;
|
const evt = new Event("input", { bubbles: true, cancelable: false });
|
||||||
const evt = new Event("input", { bubbles: true, cancelable: false });
|
elem.dispatchEvent(evt);
|
||||||
elem.dispatchEvent(evt);
|
elem.blur();
|
||||||
elem.blur();
|
|
||||||
|
|
||||||
resolve();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function queryAll(selector, context) {
|
export function queryAll(selector, context) {
|
||||||
|
@ -491,10 +486,12 @@ export function exists(selector) {
|
||||||
|
|
||||||
export async function publishToMessageBus(channelPath, ...args) {
|
export async function publishToMessageBus(channelPath, ...args) {
|
||||||
args = cloneJSON(args);
|
args = cloneJSON(args);
|
||||||
MessageBus.callbacks
|
|
||||||
.filterBy("channel", channelPath)
|
|
||||||
.forEach((c) => c.func(...args));
|
|
||||||
|
|
||||||
|
const promises = MessageBus.callbacks
|
||||||
|
.filterBy("channel", channelPath)
|
||||||
|
.map((callback) => callback.func(...args));
|
||||||
|
|
||||||
|
await Promise.allSettled(promises);
|
||||||
await settled();
|
await settled();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,8 +138,6 @@ acceptance("Presence - Subscribing", function (needs) {
|
||||||
99
|
99
|
||||||
);
|
);
|
||||||
|
|
||||||
await channel._presenceState._resubscribePromise;
|
|
||||||
|
|
||||||
sinon.assert.calledOnce(stub);
|
sinon.assert.calledOnce(stub);
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
channel.users.length,
|
channel.users.length,
|
||||||
|
@ -189,8 +187,6 @@ acceptance("Presence - Subscribing", function (needs) {
|
||||||
3
|
3
|
||||||
);
|
);
|
||||||
|
|
||||||
await channel._presenceState._resubscribePromise;
|
|
||||||
|
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
channel.count,
|
channel.count,
|
||||||
3,
|
3,
|
||||||
|
|
Loading…
Reference in New Issue