@@ -7,7 +7,8 @@ const ParseServer = require('parse-server').default;
7
7
const CustomAuth = require ( './CustomAuth' ) ;
8
8
const { TestUtils } = require ( 'parse-server' ) ;
9
9
const Parse = require ( '../../node' ) ;
10
- const fs = require ( 'fs' ) ;
10
+ const { resolvingPromise } = require ( '../../lib/node/promiseUtils' ) ;
11
+ const fs = require ( 'fs' ) . promises ;
11
12
const path = require ( 'path' ) ;
12
13
const dns = require ( 'dns' ) ;
13
14
const MockEmailAdapterWithOptions = require ( './support/MockEmailAdapterWithOptions' ) ;
@@ -21,6 +22,7 @@ const port = 1337;
21
22
const mountPath = '/parse' ;
22
23
const serverURL = 'http://localhost:1337/parse' ;
23
24
let didChangeConfiguration = false ;
25
+ const distFiles = { } ;
24
26
25
27
/*
26
28
To generate the auth data below, the Twitter app "GitHub CI Test App" has
@@ -91,17 +93,41 @@ const defaultConfiguration = {
91
93
} ) ,
92
94
} ;
93
95
94
- const openConnections = { } ;
96
+ const openConnections = new Set ( ) ;
95
97
let parseServer ;
96
98
99
+ const destroyConnections = ( ) => {
100
+ for ( const socket of openConnections . values ( ) ) {
101
+ socket . destroy ( ) ;
102
+ }
103
+ openConnections . clear ( ) ;
104
+ } ;
105
+
106
+ const shutdownServer = async _parseServer => {
107
+ const closePromise = resolvingPromise ( ) ;
108
+ _parseServer . server . on ( 'close' , ( ) => {
109
+ closePromise . resolve ( ) ;
110
+ } ) ;
111
+ await Promise . all ( [
112
+ _parseServer . config . databaseController . adapter . handleShutdown ( ) ,
113
+ _parseServer . liveQueryServer . shutdown ( ) ,
114
+ ] ) ;
115
+ _parseServer . server . close ( error => {
116
+ if ( error ) {
117
+ console . error ( 'Failed to close Parse Server' , error ) ;
118
+ }
119
+ } ) ;
120
+ destroyConnections ( ) ;
121
+ await closePromise ;
122
+ expect ( openConnections . size ) . toBe ( 0 ) ;
123
+ parseServer = undefined ;
124
+ } ;
125
+
97
126
const reconfigureServer = async ( changedConfiguration = { } ) => {
98
127
if ( parseServer ) {
99
- await parseServer . handleShutdown ( ) ;
100
- await new Promise ( resolve => parseServer . server . close ( resolve ) ) ;
101
- parseServer = undefined ;
128
+ await shutdownServer ( parseServer ) ;
102
129
return reconfigureServer ( changedConfiguration ) ;
103
130
}
104
-
105
131
didChangeConfiguration = Object . keys ( changedConfiguration ) . length !== 0 ;
106
132
const newConfiguration = Object . assign ( { } , defaultConfiguration , changedConfiguration || { } , {
107
133
mountPath,
@@ -113,8 +139,7 @@ const reconfigureServer = async (changedConfiguration = {}) => {
113
139
return reconfigureServer ( newConfiguration ) ;
114
140
}
115
141
const app = parseServer . expressApp ;
116
- for ( const fileName of [ 'parse.js' , 'parse.min.js' ] ) {
117
- const file = fs . readFileSync ( path . resolve ( __dirname , `./../../dist/${ fileName } ` ) ) . toString ( ) ;
142
+ for ( const [ fileName , file ] of Object . entries ( distFiles ) ) {
118
143
app . get ( `/${ fileName } ` , ( _req , res ) => {
119
144
res . send ( `<html><head>
120
145
<meta charset="utf-8">
@@ -132,17 +157,10 @@ const reconfigureServer = async (changedConfiguration = {}) => {
132
157
</body></html>` ) ;
133
158
} ) ;
134
159
}
135
- app . get ( '/clear/:fast' , ( req , res ) => {
136
- const { fast } = req . params ;
137
- TestUtils . destroyAllDataPermanently ( fast ) . then ( ( ) => {
138
- res . send ( '{}' ) ;
139
- } ) ;
140
- } ) ;
141
160
parseServer . server . on ( 'connection' , connection => {
142
- const key = `${ connection . remoteAddress } :${ connection . remotePort } ` ;
143
- openConnections [ key ] = connection ;
161
+ openConnections . add ( connection ) ;
144
162
connection . on ( 'close' , ( ) => {
145
- delete openConnections [ key ] ;
163
+ openConnections . delete ( connection ) ;
146
164
} ) ;
147
165
} ) ;
148
166
return parseServer ;
@@ -155,12 +173,21 @@ global.Container = Parse.Object.extend('Container');
155
173
global . TestPoint = Parse . Object . extend ( 'TestPoint' ) ;
156
174
global . TestObject = Parse . Object . extend ( 'TestObject' ) ;
157
175
global . reconfigureServer = reconfigureServer ;
176
+ global . shutdownServer = shutdownServer ;
177
+ global . openConnections = openConnections ;
158
178
159
179
beforeAll ( async ( ) => {
180
+ const promise = [ 'parse.js' , 'parse.min.js' ] . map ( fileName => {
181
+ return fs . readFile ( path . resolve ( __dirname , `./../../dist/${ fileName } ` ) , 'utf8' ) . then ( file => {
182
+ distFiles [ fileName ] = file ;
183
+ } ) ;
184
+ } ) ;
185
+ await Promise . all ( promise ) ;
160
186
await reconfigureServer ( ) ;
161
187
Parse . initialize ( 'integration' ) ;
162
188
Parse . CoreManager . set ( 'SERVER_URL' , serverURL ) ;
163
189
Parse . CoreManager . set ( 'MASTER_KEY' , 'notsosecret' ) ;
190
+ Parse . CoreManager . set ( 'REQUEST_ATTEMPT_LIMIT' , 1 ) ;
164
191
} ) ;
165
192
166
193
afterEach ( async ( ) => {
@@ -172,11 +199,4 @@ afterEach(async () => {
172
199
}
173
200
} ) ;
174
201
175
- afterAll ( ( ) => {
176
- // Jasmine process counts as one open connection
177
- if ( Object . keys ( openConnections ) . length > 1 ) {
178
- console . warn ( 'There were open connections to the server left after the test finished' ) ;
179
- }
180
- } ) ;
181
-
182
202
module . exports = { twitterAuthData } ;
0 commit comments