1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00

Fix integration tests port race condition

This commit is contained in:
Patrik J. Braun 2024-03-25 22:21:18 +01:00
parent 7f65dfd1b2
commit 78b72e2528
7 changed files with 65 additions and 31 deletions

View File

@ -44,21 +44,21 @@ export class Server {
return this.instance;
}
constructor() {
constructor(listen = true) {
if (!(process.env.NODE_ENV === 'production')) {
Logger.info(
LOG_TAG,
'Running in DEBUG mode, set env variable NODE_ENV=production to disable '
);
}
this.init().catch(console.error);
this.init(listen).catch(console.error);
}
get Server(): HttpServer {
return this.server;
}
async init(): Promise<void> {
async init(listen = true): Promise<void> {
this.app = express();
LoggerRouter.route(this.app);
this.app.set('view engine', 'ejs');
@ -68,11 +68,11 @@ export class Server {
Logger.verbose(
LOG_TAG,
() => 'using config from ' +
(
ConfigClassBuilder.attachPrivateInterface(Config)
.__options as ConfigClassOptions<ServerConfig>
).configPath +
':'
(
ConfigClassBuilder.attachPrivateInterface(Config)
.__options as ConfigClassOptions<ServerConfig>
).configPath +
':'
);
Logger.verbose(LOG_TAG, () => JSON.stringify(Config.toJSON({attachDescription: false}), (k, v) => {
const MAX_LENGTH = 80;
@ -140,7 +140,9 @@ export class Server {
this.server = _http.createServer(this.app);
// Listen on provided PORT, on all network interfaces.
this.server.listen(Config.Server.port, Config.Server.host);
if (listen) {
this.server.listen(Config.Server.port, Config.Server.host);
}
this.server.on('error', this.onError);
this.server.on('listening', this.onListening);
this.server.on('close', this.onClose);
@ -153,7 +155,9 @@ export class Server {
});
});
this.onStarted.trigger();
if (!listen) {
this.onStarted.trigger();
}
}
/**
@ -190,6 +194,7 @@ export class Server {
const bind =
typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
Logger.info(LOG_TAG, 'Listening on ' + bind);
this.onStarted.trigger();
};
/**
@ -198,6 +203,20 @@ export class Server {
private onClose = () => {
Logger.info(LOG_TAG, 'Closed http server');
};
public Stop(): Promise<void> {
return new Promise((resolve, reject) => {
if(!this.server.listening){
return resolve();
}
this.server.close((err) => {
if (!err) {
return resolve();
}
reject(err);
});
});
}
}

View File

@ -17,24 +17,24 @@ import {
import {VersionEntity} from '../../../../../src/backend/model/database/enitites/VersionEntity';
import {DatabaseType} from '../../../../../src/common/config/private/PrivateConfig';
import {ProjectPath} from '../../../../../src/backend/ProjectPath';
import {TestHelper} from '../../../../TestHelper';
describe('Typeorm integration', () => {
const tempDir = path.join(__dirname, '../../tmp');
const setUpSqlDB = async () => {
await fs.promises.rm(tempDir, {recursive: true, force: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true});
Config.Database.type = DatabaseType.sqlite;
Config.Database.dbFolder = tempDir;
Config.Database.dbFolder = TestHelper.TMP_DIR;
ProjectPath.reset();
};
const teardownUpSqlDB = async () => {
await SQLConnection.close();
await fs.promises.rm(tempDir, {recursive: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true});
};
beforeEach(async () => {

View File

@ -34,7 +34,7 @@ describe('GalleryRouter', (sqlHelper: DBTestHelper) => {
Config.Media.folder = path.join(__dirname, '../../assets');
Config.Media.tempFolder = TestHelper.TMP_DIR;
ProjectPath.reset();
server = new Server();
server = new Server(false);
await server.onStarted.wait();
};
const tearDown = async () => {

View File

@ -10,6 +10,9 @@ import {SuperAgentStatic} from 'superagent';
import {RouteTestingHelper} from './RouteTestingHelper';
import {QueryParams} from '../../../../src/common/QueryParams';
import {DatabaseType} from '../../../../src/common/config/private/PrivateConfig';
import {TestHelper} from '../../../TestHelper';
import {createLoggerWrapper} from '../../../../src/backend/Logger';
import {ProjectPath} from '../../../../src/backend/ProjectPath';
process.env.NODE_ENV = 'test';
@ -29,16 +32,17 @@ describe('PublicRouter', () => {
permissions: null
};
const {password: pass, ...expectedUser} = testUser;
const tempDir = path.join(__dirname, '../../tmp');
let server: Server;
const setUp = async () => {
await fs.promises.rm(tempDir, {recursive: true, force: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true});
Config.Users.authenticationRequired = true;
Config.Sharing.enabled = true;
Config.Database.type = DatabaseType.sqlite;
Config.Database.dbFolder = tempDir;
Config.Database.dbFolder = TestHelper.TMP_DIR;
ProjectPath.reset();
server = new Server();
server = new Server(false);
await server.onStarted.wait();
await ObjectManagers.getInstance().init();
@ -46,8 +50,9 @@ describe('PublicRouter', () => {
await SQLConnection.close();
};
const tearDown = async () => {
await server.Stop();
await ObjectManagers.reset();
await fs.promises.rm(tempDir, {recursive: true, force: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true});
};
const shouldHaveInjectedUser = (result: any, user: any) => {

View File

@ -39,7 +39,7 @@ describe('SharingRouter', () => {
Config.Database.type = DatabaseType.sqlite;
Config.Database.dbFolder = tempDir;
server = new Server();
server = new Server(false);
await server.onStarted.wait();
await ObjectManagers.getInstance().init();
@ -47,6 +47,7 @@ describe('SharingRouter', () => {
await SQLConnection.close();
};
const tearDown = async () => {
await server.Stop();
await ObjectManagers.reset();
await fs.promises.rm(tempDir, {recursive: true, force: true});
};

View File

@ -40,13 +40,14 @@ describe('UserRouter', () => {
ProjectPath.reset();
server = new Server();
server = new Server(false);
await server.onStarted.wait();
await ObjectManagers.getInstance().init();
await ObjectManagers.getInstance().UserManager.createUser(Utils.clone(testUser));
await SQLConnection.close();
};
const tearDown = async () => {
await server.Stop();
await ObjectManagers.reset();
await fs.promises.rm(tempDir, {recursive: true, force: true});
};

View File

@ -8,6 +8,9 @@ import {TAGS} from '../../../../../src/common/config/public/ClientConfig';
import {ObjectManagers} from '../../../../../src/backend/model/ObjectManagers';
import {UserRoles} from '../../../../../src/common/entities/UserDTO';
import {ExtensionConfigWrapper} from '../../../../../src/backend/model/extension/ExtensionConfigWrapper';
import {TestHelper} from '../../../../TestHelper';
import {Utils} from '../../../../../src/common/Utils';
import {SQLConnection} from '../../../../../src/backend/model/database/SQLConnection';
process.env.NODE_ENV = 'test';
const chai: any = require('chai');
@ -17,29 +20,34 @@ chai.use(chaiHttp);
describe('SettingsRouter', () => {
const tempDir = path.join(__dirname, '../../tmp');
let server: Server;
beforeEach(async () => {
await ObjectManagers.reset();
await fs.promises.rm(tempDir, {recursive: true, force: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true});
Config.Database.type = DatabaseType.sqlite;
Config.Database.dbFolder = tempDir;
Config.Database.dbFolder = TestHelper.TMP_DIR;
ProjectPath.reset();
server = new Server(false);
await server.onStarted.wait();
console.log('done');
await ObjectManagers.getInstance().init();
});
afterEach(async () => {
await server.Stop();
await ObjectManagers.reset();
await fs.promises.rm(tempDir, {recursive: true, force: true});
await fs.promises.rm(TestHelper.TMP_DIR, {recursive: true, force: true});
});
describe('/GET settings', () => {
it('it should GET the settings', async () => {
Config.Users.authenticationRequired = false;
Config.Users.unAuthenticatedUserRole = UserRoles.Admin;
const originalSettings = await ExtensionConfigWrapper.original();
const srv = new Server();
await srv.onStarted.wait();
const result = await chai.request(srv.Server)
const originalSettings = await ExtensionConfigWrapper.original();
console.log('testing');
const result = await chai.request(server.Server)
.get(Config.Server.apiPath + '/settings');
result.res.should.have.status(200);