Fix improper closing of resources in integration tests.

This commit is contained in:
gnuxie 2022-11-02 16:03:07 +00:00
parent 51e601603d
commit 0bd666f168
5 changed files with 35 additions and 16 deletions

View File

@ -14,7 +14,7 @@
"start:dev": "yarn build && node --async-stack-traces lib/index.js", "start:dev": "yarn build && node --async-stack-traces lib/index.js",
"test": "ts-mocha --project ./tsconfig.json test/commands/**/*.ts", "test": "ts-mocha --project ./tsconfig.json test/commands/**/*.ts",
"test:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/**/*Test.ts\"", "test:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --require test/integration/fixtures.ts --timeout 300000 --project ./tsconfig.json \"test/integration/**/*Test.ts\"",
"test:appservice:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --exit --timeout 300000 --project ./tsconfig.json \"test/appservice/integration/**/*Test.ts\"", "test:appservice:integration": "NODE_ENV=harness ts-mocha --async-stack-traces --timeout 300000 --project ./tsconfig.json \"test/appservice/integration/**/*Test.ts\"",
"test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts", "test:manual": "NODE_ENV=harness ts-node test/integration/manualLaunchScript.ts",
"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" "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"
}, },

View File

@ -2,9 +2,11 @@ import request from "request";
import express from "express"; import express from "express";
import * as bodyParser from "body-parser"; import * as bodyParser from "body-parser";
import { MjolnirManager } from "./MjolnirManager"; import { MjolnirManager } from "./MjolnirManager";
import * as http from "http";
export class Api { export class Api {
private httpdConfig: express.Express = express(); private httpdConfig: express.Express = express();
private httpServer?: http.Server;
constructor( constructor(
private homeserver: string, private homeserver: string,
@ -34,7 +36,19 @@ export class Api {
}); });
} }
public async close() {
return await new Promise((resolve, reject) => {
if (!this.httpServer) {
throw new TypeError("Server was never started");
}
this.httpServer.close(error => error ? reject(error) : resolve(undefined))
});
}
public start(port: number) { public start(port: number) {
if (this.httpServer) {
throw new TypeError("server already started");
}
this.httpdConfig.use(bodyParser.json()); this.httpdConfig.use(bodyParser.json());
this.httpdConfig.get("/get", this.pathGet.bind(this)); this.httpdConfig.get("/get", this.pathGet.bind(this));
@ -42,7 +56,7 @@ export class Api {
this.httpdConfig.post("/create", this.pathCreate.bind(this)); this.httpdConfig.post("/create", this.pathCreate.bind(this));
this.httpdConfig.post("/join", this.pathJoin.bind(this)); this.httpdConfig.post("/join", this.pathJoin.bind(this));
this.httpdConfig.listen(port); this.httpServer = this.httpdConfig.listen(port);
} }
private async pathGet(req: express.Request, response: express.Response) { private async pathGet(req: express.Request, response: express.Response) {

View File

@ -100,6 +100,7 @@ export class MjolnirAppService {
public async close(): Promise<void> { public async close(): Promise<void> {
await this.bridge.close(); await this.bridge.close();
await this.dataStore.close(); await this.dataStore.close();
await this.api.close();
} }
public static generateRegistration(reg: AppServiceRegistration, callback: (finalRegisration: AppServiceRegistration) => void) { public static generateRegistration(reg: AppServiceRegistration, callback: (finalRegisration: AppServiceRegistration) => void) {

View File

@ -1,28 +1,24 @@
import { readTestConfig, setupHarness } from "../utils/harness"; import { isPolicyRoom, readTestConfig, setupHarness } from "../utils/harness";
import { newTestUser } from "../../integration/clientHelper"; import { newTestUser } from "../../integration/clientHelper";
import { getFirstReply } from "../../integration/commands/commandUtils"; import { getFirstReply } from "../../integration/commands/commandUtils";
import { MatrixClient } from "matrix-bot-sdk"; import { MatrixClient } from "matrix-bot-sdk";
import { MjolnirAppService } from "../../../src/appservice/AppService"; import { MjolnirAppService } from "../../../src/appservice/AppService";
import PolicyList from "../../../src/models/PolicyList"; import { doesNotMatch } from "assert";
import { CreateEvent } from "matrix-bot-sdk";
interface Context extends Mocha.Context { interface Context extends Mocha.Context {
user?: MatrixClient, user?: MatrixClient,
appservice?: MjolnirAppService appservice?: MjolnirAppService
} }
describe("Test that the app service can provision a mjolnir on invite of the appservice bot", function () {
afterEach(function(this: Context) { afterEach(function(this: Context) {
this.user?.stop(); this.user?.stop();
// something still runs, and i'm not sure what? -- ignoring with --exit. if (this.appservice) {
this.appservice?.close(); return this.appservice.close();
}); } else {
console.warn("Missing Appservice in this context, so cannot stop it.")
async function isPolicyRoom(user: MatrixClient, roomId: string): Promise<boolean> {
const createEvent = new CreateEvent(await user.getRoomStateEvent(roomId, "m.room.create", ""));
return PolicyList.ROOM_TYPE_VARIANTS.includes(createEvent.type);
} }
});
describe("Test that the app service can provision a mjolnir on invite of the appservice bot", function () {
it("", async function (this: Context) { it("", async function (this: Context) {
const config = readTestConfig(); const config = readTestConfig();
this.appservice = await setupHarness(); this.appservice = await setupHarness();

View File

@ -4,6 +4,8 @@ import { ensureAliasedRoomExists } from "../../integration/mjolnirSetupUtils";
import { read as configRead, IConfig } from "../../../src/appservice/config/config"; import { read as configRead, IConfig } from "../../../src/appservice/config/config";
import { PgDataStore } from "../../../src/appservice/datastore"; import { PgDataStore } from "../../../src/appservice/datastore";
import { newTestUser } from "../../integration/clientHelper"; import { newTestUser } from "../../integration/clientHelper";
import PolicyList from "../../../src/models/PolicyList";
import { CreateEvent, MatrixClient } from "matrix-bot-sdk";
export function readTestConfig(): IConfig { export function readTestConfig(): IConfig {
return configRead(path.join(__dirname, "../../../src/appservice/config/config.harness.yaml")); return configRead(path.join(__dirname, "../../../src/appservice/config/config.harness.yaml"));
@ -20,3 +22,9 @@ export async function setupHarness(): Promise<MjolnirAppService> {
await appservice.start(9000); await appservice.start(9000);
return appservice; return appservice;
} }
export async function isPolicyRoom(user: MatrixClient, roomId: string): Promise<boolean> {
const createEvent = new CreateEvent(await user.getRoomStateEvent(roomId, "m.room.create", ""));
return PolicyList.ROOM_TYPE_VARIANTS.includes(createEvent.type);
}