Skip to content

Commit bf90546

Browse files
committed
Fix improper closing of resources in integration tests.
1 parent e697812 commit bf90546

File tree

5 files changed

+35
-16
lines changed

5 files changed

+35
-16
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"start:dev": "yarn build && node --async-stack-traces lib/index.js",
1515
"test": "ts-mocha --project ./tsconfig.json test/commands/**/*.ts",
1616
"test:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/**/*Test.ts\"",
17-
"test:appservice:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --exit --timeout 300000 --project ./tsconfig.json \"test/appservice/integration/**/*Test.ts\"",
17+
"test:appservice:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --timeout 300000 --project ./tsconfig.json \"test/appservice/integration/**/*Test.ts\"",
1818
"test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts",
1919
"version": "sed -i '/# version automated/s/[0-9][0-9]*\\.[0-9][0-9]*\\.[0-9][0-9]*/'$npm_package_version'/' synapse_antispam/setup.py && git add synapse_antispam/setup.py && cat synapse_antispam/setup.py"
2020
},

src/appservice/Api.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import request from "request";
22
import express from "express";
33
import * as bodyParser from "body-parser";
44
import { MjolnirManager } from "./MjolnirManager";
5+
import * as http from "http";
56

67
export class Api {
78
private httpdConfig: express.Express = express();
9+
private httpServer?: http.Server;
810

911
constructor(
1012
private homeserver: string,
@@ -34,15 +36,27 @@ export class Api {
3436
});
3537
}
3638

39+
public async close() {
40+
return await new Promise((resolve, reject) => {
41+
if (!this.httpServer) {
42+
throw new TypeError("Server was never started");
43+
}
44+
this.httpServer.close(error => error ? reject(error) : resolve(undefined))
45+
});
46+
}
47+
3748
public start(port: number) {
49+
if (this.httpServer) {
50+
throw new TypeError("server already started");
51+
}
3852
this.httpdConfig.use(bodyParser.json());
3953

4054
this.httpdConfig.get("/get", this.pathGet.bind(this));
4155
this.httpdConfig.get("/list", this.pathList.bind(this));
4256
this.httpdConfig.post("/create", this.pathCreate.bind(this));
4357
this.httpdConfig.post("/join", this.pathJoin.bind(this));
4458

45-
this.httpdConfig.listen(port);
59+
this.httpServer = this.httpdConfig.listen(port);
4660
}
4761

4862
private async pathGet(req: express.Request, response: express.Response) {

src/appservice/AppService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export class MjolnirAppService {
100100
public async close(): Promise<void> {
101101
await this.bridge.close();
102102
await this.dataStore.close();
103+
await this.api.close();
103104
}
104105

105106
public static generateRegistration(reg: AppServiceRegistration, callback: (finalRegisration: AppServiceRegistration) => void) {

test/appservice/integration/provisionTest.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
import { readTestConfig, setupHarness } from "../utils/harness";
1+
import { isPolicyRoom, readTestConfig, setupHarness } from "../utils/harness";
22
import { newTestUser } from "../../integration/clientHelper";
33
import { getFirstReply } from "../../integration/commands/commandUtils";
44
import { MatrixClient } from "matrix-bot-sdk";
55
import { MjolnirAppService } from "../../../src/appservice/AppService";
6-
import PolicyList from "../../../src/models/PolicyList";
7-
import { CreateEvent } from "matrix-bot-sdk";
6+
import { doesNotMatch } from "assert";
87

98
interface Context extends Mocha.Context {
109
user?: MatrixClient,
1110
appservice?: MjolnirAppService
1211
}
1312

14-
afterEach(function(this: Context) {
15-
this.user?.stop();
16-
// something still runs, and i'm not sure what? -- ignoring with --exit.
17-
this.appservice?.close();
18-
});
19-
20-
async function isPolicyRoom(user: MatrixClient, roomId: string): Promise<boolean> {
21-
const createEvent = new CreateEvent(await user.getRoomStateEvent(roomId, "m.room.create", ""));
22-
return PolicyList.ROOM_TYPE_VARIANTS.includes(createEvent.type);
23-
}
24-
2513
describe("Test that the app service can provision a mjolnir on invite of the appservice bot", function () {
14+
afterEach(function(this: Context) {
15+
this.user?.stop();
16+
if (this.appservice) {
17+
return this.appservice.close();
18+
} else {
19+
console.warn("Missing Appservice in this context, so cannot stop it.")
20+
}
21+
});
2622
it("", async function (this: Context) {
2723
const config = readTestConfig();
2824
this.appservice = await setupHarness();

test/appservice/utils/harness.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { ensureAliasedRoomExists } from "../../integration/mjolnirSetupUtils";
44
import { read as configRead, IConfig } from "../../../src/appservice/config/config";
55
import { PgDataStore } from "../../../src/appservice/datastore";
66
import { newTestUser } from "../../integration/clientHelper";
7+
import PolicyList from "../../../src/models/PolicyList";
8+
import { CreateEvent, MatrixClient } from "matrix-bot-sdk";
79

810
export function readTestConfig(): IConfig {
911
return configRead(path.join(__dirname, "../../../src/appservice/config/config.harness.yaml"));
@@ -20,3 +22,9 @@ export async function setupHarness(): Promise<MjolnirAppService> {
2022
await appservice.start(9000);
2123
return appservice;
2224
}
25+
26+
export async function isPolicyRoom(user: MatrixClient, roomId: string): Promise<boolean> {
27+
const createEvent = new CreateEvent(await user.getRoomStateEvent(roomId, "m.room.create", ""));
28+
return PolicyList.ROOM_TYPE_VARIANTS.includes(createEvent.type);
29+
}
30+

0 commit comments

Comments
 (0)