From 8bf6abfee38784c3f104ab123ba26076992ff026 Mon Sep 17 00:00:00 2001 From: "CHANG, TZU-YEN" Date: Sun, 19 Nov 2017 04:20:19 +0800 Subject: [PATCH] Patch handleShutdown feature (#4361) * Add handleShutdown Test of MongoStorageAdapter. * Add `handleShutdown` function in PostgresStorageAdapter, with it's test. * Replace the _pgp to _client * Use `adapter._client.$pool.ending` to ckeck is ending * Add `handleShutdown()` in ParseServer.spec.js * Trigger * Set Server to Default --- spec/MongoStorageAdapter.spec.js | 19 ++++++++++++ spec/ParseServer.spec.js | 31 +++++++++++++++++++ spec/PostgresStorageAdapter.spec.js | 22 +++++++++++++ .../Postgres/PostgresStorageAdapter.js | 7 +++++ 4 files changed, 79 insertions(+) create mode 100644 spec/PostgresStorageAdapter.spec.js diff --git a/spec/MongoStorageAdapter.spec.js b/spec/MongoStorageAdapter.spec.js index 78b2d0c1eb..e6e65e7b40 100644 --- a/spec/MongoStorageAdapter.spec.js +++ b/spec/MongoStorageAdapter.spec.js @@ -237,4 +237,23 @@ describe_only_db('mongo')('MongoStorageAdapter', () => { done(); }); }); + + it('handleShutdown, close connection', (done) => { + const adapter = new MongoStorageAdapter({ uri: databaseURI }); + + const schema = { + fields: { + array: { type: 'Array' }, + object: { type: 'Object' }, + date: { type: 'Date' }, + } + }; + + adapter.createObject('MyClass', schema, {}).then(() => { + expect(adapter.database.serverConfig.isConnected()).toEqual(true); + adapter.handleShutdown() + expect(adapter.database.serverConfig.isConnected()).toEqual(false); + done(); + }); + }); }); diff --git a/spec/ParseServer.spec.js b/spec/ParseServer.spec.js index bc78106e26..841950d076 100644 --- a/spec/ParseServer.spec.js +++ b/spec/ParseServer.spec.js @@ -33,4 +33,35 @@ describe('Server Url Checks', () => { done(); }); }); + + it('handleShutdown, close connection', (done) => { + var MongoStorageAdapter = require('../src/Adapters/Storage/Mongo/MongoStorageAdapter'); + const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter'); + const mongoURI = 'mongodb://localhost:27017/parseServerMongoAdapterTestDatabase'; + const postgresURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database'; + let databaseAdapter; + if (process.env.PARSE_SERVER_TEST_DB === 'postgres') { + databaseAdapter = new PostgresStorageAdapter({ + uri: process.env.PARSE_SERVER_TEST_DATABASE_URI || postgresURI, + collectionPrefix: 'test_', + }); + } else { + databaseAdapter = new MongoStorageAdapter({ + uri: mongoURI, + collectionPrefix: 'test_', + }); + } + const newConfiguration = Object.assign({}, defaultConfiguration, { databaseAdapter }); + const parseServer = ParseServer.start(newConfiguration, () => { + parseServer.handleShutdown(); + parseServer.server.close((err) => { + if (err) { + done.fail('Close Server Error') + } + reconfigureServer({}).then(() => { + done(); + }); + }); + }); + }); }); diff --git a/spec/PostgresStorageAdapter.spec.js b/spec/PostgresStorageAdapter.spec.js new file mode 100644 index 0000000000..18e7c83ad7 --- /dev/null +++ b/spec/PostgresStorageAdapter.spec.js @@ -0,0 +1,22 @@ +const PostgresStorageAdapter = require('../src/Adapters/Storage/Postgres/PostgresStorageAdapter'); +const databaseURI = 'postgres://localhost:5432/parse_server_postgres_adapter_test_database'; + +describe_only_db('postgres')('PostgresStorageAdapter', () => { + beforeEach(done => { + const adapter = new PostgresStorageAdapter({ uri: databaseURI }) + .deleteAllClasses() + .then(() => { + adapter.handleShutdown(); + }, fail) + .catch(done); + }); + + it('handleShutdown, close connection', (done) => { + const adapter = new PostgresStorageAdapter({ uri: databaseURI }); + + expect(adapter._client.$pool.ending).toEqual(false); + adapter.handleShutdown(); + expect(adapter._client.$pool.ending).toEqual(true); + done(); + }); +}); diff --git a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js index 4560ab9b2e..cb4574f675 100644 --- a/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js +++ b/src/Adapters/Storage/Postgres/PostgresStorageAdapter.js @@ -574,6 +574,13 @@ export class PostgresStorageAdapter { this._pgp = pgp; } + handleShutdown() { + if (!this._client) { + return + } + this._client.$pool.end(); + } + _ensureSchemaCollectionExists(conn) { conn = conn || this._client; return conn.none('CREATE TABLE IF NOT EXISTS "_SCHEMA" ( "className" varChar(120), "schema" jsonb, "isParseClass" bool, PRIMARY KEY ("className") )')