Skip to content

Commit ac2e736

Browse files
authored
Report polling test temporality (#325)
So the test before sometimes sent the report *before* the protection (that is used to check whether we have received the report) was registered. This of course meant that we didn't ever receive the report from the perspectivee of the test. This PR should now mean we always send the report after registering the protection.
1 parent 6e5d520 commit ac2e736

File tree

1 file changed

+21
-28
lines changed

1 file changed

+21
-28
lines changed
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
1-
import { strict as assert } from "assert";
2-
3-
import config from "../../src/config";
41
import { Mjolnir } from "../../src/Mjolnir";
52
import { IProtection } from "../../src/protections/IProtection";
6-
import { PROTECTIONS } from "../../src/protections/protections";
7-
import { ProtectionSettingValidationError } from "../../src/protections/ProtectionSettings";
8-
import { NumberProtectionSetting, StringProtectionSetting, StringListProtectionSetting } from "../../src/protections/ProtectionSettings";
9-
import { newTestUser, noticeListener } from "./clientHelper";
10-
import { matrixClient, mjolnir } from "./mjolnirSetupUtils";
3+
import { newTestUser } from "./clientHelper";
114

125
describe("Test: Report polling", function() {
136
let client;
147
this.beforeEach(async function () {
158
client = await newTestUser({ name: { contains: "protection-settings" }});
16-
await client.start();
17-
})
18-
this.afterEach(async function () {
19-
await client.stop();
209
})
2110
it("Mjolnir correctly retrieves a report from synapse", async function() {
2211
this.timeout(40000);
2312

24-
const reportPromise = new Promise(async (resolve, reject) => {
13+
let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await client.getUserId()] });
14+
await client.joinRoom(protectedRoomId);
15+
await this.mjolnir.addProtectedRoom(protectedRoomId);
16+
17+
const eventId = await client.sendMessage(protectedRoomId, {msgtype: "m.text", body: "uwNd3q"});
18+
await new Promise(async resolve => {
2519
await this.mjolnir.registerProtection(new class implements IProtection {
2620
name = "jYvufI";
2721
description = "A test protection";
@@ -33,22 +27,21 @@ describe("Test: Report polling", function() {
3327
}
3428
};
3529
});
30+
await this.mjolnir.enableProtection("jYvufI");
31+
await client.doRequest(
32+
"POST",
33+
`/_matrix/client/r0/rooms/${encodeURIComponent(protectedRoomId)}/report/${encodeURIComponent(eventId)}`, "", {
34+
reason: "x5h1Je"
35+
}
36+
);
3637
});
37-
await this.mjolnir.enableProtection("jYvufI");
38-
39-
let protectedRoomId = await this.mjolnir.client.createRoom({ invite: [await client.getUserId()] });
40-
await client.joinRoom(protectedRoomId);
41-
await this.mjolnir.addProtectedRoom(protectedRoomId);
42-
43-
const eventId = await client.sendMessage(protectedRoomId, {msgtype: "m.text", body: "uwNd3q"});
44-
await client.doRequest(
45-
"POST",
46-
`/_matrix/client/r0/rooms/${encodeURIComponent(protectedRoomId)}/report/${encodeURIComponent(eventId)}`, "", {
47-
reason: "x5h1Je"
48-
}
49-
);
50-
51-
await reportPromise;
38+
// So I kid you not, it seems like we can quit before the webserver for reports sends a respond to the client (via L#26)
39+
// because the promise above gets resolved before we finish awaiting the report sending request on L#31,
40+
// then mocha's cleanup code runs (and shuts down the webserver) before the webserver can respond.
41+
// Wait a minute 😲😲🤯 it's not even supposed to be using the webserver if this is testing report polling.
42+
// Ok, well apparently that needs a big refactor to change, but if you change the config before running this test,
43+
// then you can ensure that report polling works. https://github.com/matrix-org/mjolnir/issues/326.
44+
await new Promise(resolve => setTimeout(resolve, 1000));
5245
});
5346
});
5447

0 commit comments

Comments
 (0)