Skip to content

ci: Fix flaky tests invalid server state: initialized #9178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 18 additions & 22 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,26 +151,16 @@ if (process.env.PARSE_SERVER_TEST_CACHE === 'redis') {
}

const openConnections = {};
const destroyAliveConnections = function () {
for (const socketId in openConnections) {
try {
openConnections[socketId].destroy();
delete openConnections[socketId];
} catch (e) {
/* */
}
}
};
// Set up a default API server for testing with default configuration.
let server;
let parseServer;

let didChangeConfiguration = false;

// Allows testing specific configurations of Parse Server
const reconfigureServer = async (changedConfiguration = {}) => {
if (server) {
await new Promise(resolve => server.close(resolve));
server = undefined;
if (parseServer) {
await parseServer.handleShutdown();
await new Promise(resolve => parseServer.server.close(resolve));
parseServer = undefined;
return reconfigureServer(changedConfiguration);
}
didChangeConfiguration = Object.keys(changedConfiguration).length !== 0;
Expand All @@ -179,14 +169,17 @@ const reconfigureServer = async (changedConfiguration = {}) => {
port,
});
cache.clear();
const parseServer = await ParseServer.startApp(newConfiguration);
server = parseServer.server;
parseServer = await ParseServer.startApp(newConfiguration);
if (parseServer.config.state === 'initialized') {
console.error('Failed to initialize Parse Server');
return reconfigureServer(newConfiguration);
}
Parse.CoreManager.setRESTController(RESTController);
parseServer.expressApp.use('/1', err => {
console.error(err);
fail('should not call next');
});
server.on('connection', connection => {
parseServer.server.on('connection', connection => {
const key = `${connection.remoteAddress}:${connection.remotePort}`;
openConnections[key] = connection;
connection.on('close', () => {
Expand Down Expand Up @@ -220,10 +213,6 @@ beforeEach(() => {

afterEach(function (done) {
const afterLogOut = async () => {
if (Object.keys(openConnections).length > 0) {
console.warn('There were open connections to the server left after the test finished');
}
destroyAliveConnections();
await TestUtils.destroyAllDataPermanently(true);
SchemaCache.clear();
if (didChangeConfiguration) {
Expand Down Expand Up @@ -278,6 +267,13 @@ afterEach(function (done) {
.then(afterLogOut);
});

afterAll(() => {
// Jasmine process counts as one open connection
if (Object.keys(openConnections).length > 1) {
console.warn('There were open connections to the server left after the test finished');
}
});

const TestObject = Parse.Object.extend({
className: 'TestObject',
});
Expand Down
Loading