From 70430c2796b6c0455a863edc62760a3d45951fc5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 6 Sep 2021 08:13:11 +0200 Subject: Wait mock server termination --- shared/extra-utils/mock-servers/mock-object-storage.ts | 3 ++- .../extra-utils/mock-servers/mock-plugin-blocklist.ts | 3 ++- shared/extra-utils/mock-servers/mock-proxy.ts | 3 ++- shared/extra-utils/mock-servers/utils.ts | 17 +++++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 shared/extra-utils/mock-servers/utils.ts (limited to 'shared/extra-utils') diff --git a/shared/extra-utils/mock-servers/mock-object-storage.ts b/shared/extra-utils/mock-servers/mock-object-storage.ts index f1c5a6123..b6071b2f2 100644 --- a/shared/extra-utils/mock-servers/mock-object-storage.ts +++ b/shared/extra-utils/mock-servers/mock-object-storage.ts @@ -4,6 +4,7 @@ import { Server } from 'http' import { pipeline } from 'stream' import { randomInt } from '@shared/core-utils' import { ObjectStorageCommand } from '../server' +import { terminateServer } from './utils' export class MockObjectStorage { private server: Server @@ -37,6 +38,6 @@ export class MockObjectStorage { } terminate () { - if (this.server) this.server.close() + return terminateServer(this.server) } } diff --git a/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts b/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts index 6f66bf4f0..6a71532b5 100644 --- a/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts +++ b/shared/extra-utils/mock-servers/mock-plugin-blocklist.ts @@ -1,6 +1,7 @@ import express, { Request, Response } from 'express' import { Server } from 'http' import { randomInt } from '@shared/core-utils' +import { terminateServer } from './utils' type BlocklistResponse = { data: { @@ -32,6 +33,6 @@ export class MockBlocklist { } terminate () { - if (this.server) this.server.close() + return terminateServer(this.server) } } diff --git a/shared/extra-utils/mock-servers/mock-proxy.ts b/shared/extra-utils/mock-servers/mock-proxy.ts index eb5f177c7..f955d3f9e 100644 --- a/shared/extra-utils/mock-servers/mock-proxy.ts +++ b/shared/extra-utils/mock-servers/mock-proxy.ts @@ -2,6 +2,7 @@ import { createServer, Server } from 'http' import proxy from 'proxy' import { randomInt } from '@shared/core-utils' +import { terminateServer } from './utils' class MockProxy { private server: Server @@ -16,7 +17,7 @@ class MockProxy { } terminate () { - if (this.server) this.server.close() + return terminateServer(this.server) } } diff --git a/shared/extra-utils/mock-servers/utils.ts b/shared/extra-utils/mock-servers/utils.ts new file mode 100644 index 000000000..64d94c868 --- /dev/null +++ b/shared/extra-utils/mock-servers/utils.ts @@ -0,0 +1,17 @@ +import { Server } from 'http' + +function terminateServer (server: Server) { + if (!server) return Promise.resolve() + + return new Promise((res, rej) => { + server.close(err => { + if (err) return rej(err) + + return res() + }) + }) +} + +export { + terminateServer +} -- cgit v1.2.3