Skip to content
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

testing server #4

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
122 changes: 59 additions & 63 deletions spec/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,15 @@ const destroyAliveConnections = function () {
}
};
// 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) {
destroyAliveConnections();
await new Promise(resolve => parseServer.server.close(resolve));
parseServer = undefined;
return reconfigureServer(changedConfiguration);
}
didChangeConfiguration = Object.keys(changedConfiguration).length !== 0;
Expand All @@ -179,14 +179,28 @@ const reconfigureServer = async (changedConfiguration = {}) => {
port,
});
cache.clear();
const parseServer = await ParseServer.startApp(newConfiguration);
server = parseServer.server;
parseServer = await ParseServer.startApp(newConfiguration);
console.log(parseServer.config.state);
if (parseServer.config.state === 'initialized') {
console.log(newConfiguration);
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('close', () => {
// console.log('why we closed');
// });
// parseServer.server.on('error', () => {
// console.log('why we error');
// });
// parseServer.server.on('shutdown', () => {
// console.log('why we error');
// });
parseServer.server.on('connection', connection => {
const key = `${connection.remoteAddress}:${connection.remotePort}`;
openConnections[key] = connection;
connection.on('close', () => {
Expand Down Expand Up @@ -218,64 +232,46 @@ beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = process.env.PARSE_SERVER_TEST_TIMEOUT || 10000;
});

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) {
await reconfigureServer();
} else {
await databaseAdapter.performInitialization({ VolatileClassesSchemas });
}
done();
};
afterEach(async () => {
Parse.Cloud._removeAllHooks();
Parse.CoreManager.getLiveQueryController().setDefaultLiveQueryClient();
defaults.protectedFields = { _User: { '*': ['email'] } };
databaseAdapter
.getAllClasses()
.then(allSchemas => {
allSchemas.forEach(schema => {
const className = schema.className;
expect(className).toEqual({
asymmetricMatch: className => {
if (!className.startsWith('_')) {
return true;
} else {
// Other system classes will break Parse.com, so make sure that we don't save anything to _SCHEMA that will
// break it.
return (
[
'_User',
'_Installation',
'_Role',
'_Session',
'_Product',
'_Audience',
'_Idempotency',
].indexOf(className) >= 0
);
}
},
});
});
})
.then(() => Parse.User.logOut())
.then(
() => {},
() => {}
) // swallow errors
.then(() => {
// Connection close events are not immediate on node 10+... wait a bit
return new Promise(resolve => {
setTimeout(resolve, 0);
});
})
.then(afterLogOut);
const allSchemas = await databaseAdapter.getAllClasses();
allSchemas.forEach(schema => {
const className = schema.className;
expect(className).toEqual({
asymmetricMatch: className => {
if (!className.startsWith('_')) {
return true;
} else {
// Other system classes will break Parse.com, so make sure that we don't save anything to _SCHEMA that will
// break it.
return (
[
'_User',
'_Installation',
'_Role',
'_Session',
'_Product',
'_Audience',
'_Idempotency',
].indexOf(className) >= 0
);
}
},
});
});
await Parse.User.logOut();
if (Object.keys(openConnections).length > 0) {
console.warn(`There were ${Object.keys(openConnections).length} open connections to the server left after the test finished`);
}
await TestUtils.destroyAllDataPermanently(true);
SchemaCache.clear();
if (didChangeConfiguration) {
await reconfigureServer();
} else {
await databaseAdapter.performInitialization({ VolatileClassesSchemas });
}
});

const TestObject = Parse.Object.extend({
Expand Down
3 changes: 1 addition & 2 deletions spec/support/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"spec_dir": "spec",
"spec_files": ["*spec.js"],
"helpers": ["helper.js"],
"random": true
"helpers": ["helper.js"]
}
6 changes: 6 additions & 0 deletions src/ParseServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,17 @@ class ParseServer {
promises.push(cacheAdapter.handleShutdown());
}
if (this.liveQueryServer?.server?.close) {
console.log(this.liveQueryServer?.server);
this.liveQueryServer?.server.getConnections((err, count) => {
console.log('Connections: ', count, err);
});
promises.push(new Promise(resolve => this.liveQueryServer.server.close(resolve)));
}
if (this.liveQueryServer) {
promises.push(this.liveQueryServer.shutdown());
}
console.log('Shutting down Parse Server');
console.log(promises.length);
return (promises.length > 0 ? Promise.all(promises) : Promise.resolve()).then(() => {
if (this.config.serverCloseComplete) {
this.config.serverCloseComplete();
Expand Down
Loading