Skip to content

Commit 893f1d3

Browse files
authored
Remove test delays (#5579)
* Changing __indexBuildCompletionCallbackForTests callback to serverStartComplete * Improving serverStartComplete callback to avoid production unhandled promise rejection * Add test to check inexistence of unhandled promise rejection on server fail * Removing some hooks delays * Removing delay after reconfigureServer * Improving code style
1 parent 2f161c2 commit 893f1d3

11 files changed

+143
-106
lines changed

spec/CloudCode.spec.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,13 @@ describe('Cloud Code', () => {
453453
Parse.Cloud.afterSave('AfterSaveTest', function(req) {
454454
const obj = new Parse.Object('AfterSaveProof');
455455
obj.set('proof', req.object.id);
456-
obj.save();
456+
obj.save().then(test);
457457
});
458458

459459
const obj = new Parse.Object('AfterSaveTest');
460460
obj.save();
461461

462-
setTimeout(function() {
462+
function test() {
463463
const query = new Parse.Query('AfterSaveProof');
464464
query.equalTo('proof', obj.id);
465465
query.find().then(
@@ -472,7 +472,7 @@ describe('Cloud Code', () => {
472472
done();
473473
}
474474
);
475-
}, 500);
475+
}
476476
});
477477

478478
it('test afterSave ran on created object and returned a promise', function(done) {
@@ -729,15 +729,15 @@ describe('Cloud Code', () => {
729729
Parse.Cloud.afterDelete('AfterDeleteTest', function(req) {
730730
const obj = new Parse.Object('AfterDeleteProof');
731731
obj.set('proof', req.object.id);
732-
obj.save();
732+
obj.save().then(test);
733733
});
734734

735735
const obj = new Parse.Object('AfterDeleteTest');
736736
obj.save().then(function() {
737737
obj.destroy();
738738
});
739739

740-
setTimeout(function() {
740+
function test() {
741741
const query = new Parse.Query('AfterDeleteProof');
742742
query.equalTo('proof', obj.id);
743743
query.find().then(
@@ -750,7 +750,7 @@ describe('Cloud Code', () => {
750750
done();
751751
}
752752
);
753-
}, 500);
753+
}
754754
});
755755

756756
it('test cloud function return types', function(done) {

spec/EnableExpressErrorHandler.spec.js

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,45 @@ describe('Enable express error handler', () => {
1717
masterKey: masterKey,
1818
serverURL: serverUrl,
1919
enableExpressErrorHandler: true,
20-
__indexBuildCompletionCallbackForTests: promise => {
21-
promise.then(() => {
22-
expect(Parse.applicationId).toEqual('anOtherTestApp');
23-
const app = express();
24-
app.use('/parse', parseServer);
20+
serverStartComplete: () => {
21+
expect(Parse.applicationId).toEqual('anOtherTestApp');
22+
const app = express();
23+
app.use('/parse', parseServer);
2524

26-
server = app.listen(12667);
25+
server = app.listen(12667);
2726

28-
app.use(function(err, req, res, next) {
29-
next;
30-
lastError = err;
31-
});
27+
app.use(function(err, req, res, next) {
28+
next;
29+
lastError = err;
30+
});
3231

33-
request({
34-
method: 'PUT',
35-
url: serverUrl + '/classes/AnyClass/nonExistingId',
36-
headers: {
37-
'X-Parse-Application-Id': appId,
38-
'X-Parse-Master-Key': masterKey,
39-
'Content-Type': 'application/json',
40-
},
41-
body: { someField: 'blablabla' },
32+
request({
33+
method: 'PUT',
34+
url: serverUrl + '/classes/AnyClass/nonExistingId',
35+
headers: {
36+
'X-Parse-Application-Id': appId,
37+
'X-Parse-Master-Key': masterKey,
38+
'Content-Type': 'application/json',
39+
},
40+
body: { someField: 'blablabla' },
41+
})
42+
.then(() => {
43+
fail('Should throw error');
4244
})
43-
.then(() => {
44-
fail('Should throw error');
45-
})
46-
.catch(response => {
47-
const reqError = response.data;
48-
expect(reqError).toBeDefined();
49-
expect(lastError).toBeDefined();
45+
.catch(response => {
46+
const reqError = response.data;
47+
expect(reqError).toBeDefined();
48+
expect(lastError).toBeDefined();
5049

51-
expect(lastError.code).toEqual(101);
52-
expect(lastError.message).toEqual('Object not found.');
50+
expect(lastError.code).toEqual(101);
51+
expect(lastError.message).toEqual('Object not found.');
5352

54-
expect(lastError.code).toEqual(reqError.code);
55-
expect(lastError.message).toEqual(reqError.error);
56-
})
57-
.then(() => {
58-
server.close(done);
59-
});
60-
});
53+
expect(lastError.code).toEqual(reqError.code);
54+
expect(lastError.message).toEqual(reqError.error);
55+
})
56+
.then(() => {
57+
server.close(done);
58+
});
6159
},
6260
})
6361
);

spec/FilesController.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe('FilesController', () => {
4646
const logController = new LoggerController(new WinstonLoggerAdapter());
4747

4848
reconfigureServer({ filesAdapter: mockAdapter })
49-
.then(() => new Promise(resolve => setTimeout(resolve, 1000)))
5049
.then(() => new Parse.File('yolo.txt', [1, 2, 3], 'text/plain').save())
5150
.then(
5251
() => done.fail('should not succeed'),

spec/ParseLiveQueryServer.spec.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,10 @@ describe('ParseLiveQueryServer', function() {
158158
classNames: ['Yolo'],
159159
},
160160
startLiveQueryServer: true,
161-
__indexBuildCompletionCallbackForTests: promise => {
162-
promise.then(() => {
163-
expect(parseServer.liveQueryServer).not.toBeUndefined();
164-
expect(parseServer.liveQueryServer.server).toBe(parseServer.server);
165-
parseServer.server.close(() => done());
166-
});
161+
serverStartComplete: () => {
162+
expect(parseServer.liveQueryServer).not.toBeUndefined();
163+
expect(parseServer.liveQueryServer.server).toBe(parseServer.server);
164+
parseServer.server.close(done);
167165
},
168166
});
169167
});
@@ -181,16 +179,14 @@ describe('ParseLiveQueryServer', function() {
181179
liveQueryServerOptions: {
182180
port: 22347,
183181
},
184-
__indexBuildCompletionCallbackForTests: promise => {
185-
promise.then(() => {
186-
expect(parseServer.liveQueryServer).not.toBeUndefined();
187-
expect(parseServer.liveQueryServer.server).not.toBe(
188-
parseServer.server
189-
);
190-
parseServer.liveQueryServer.server.close(() => {
191-
parseServer.server.close(() => done());
192-
});
193-
});
182+
serverStartComplete: () => {
183+
expect(parseServer.liveQueryServer).not.toBeUndefined();
184+
expect(parseServer.liveQueryServer.server).not.toBe(
185+
parseServer.server
186+
);
187+
parseServer.liveQueryServer.server.close(
188+
parseServer.server.close.bind(parseServer.server, done)
189+
);
194190
},
195191
});
196192
});

spec/ParseServer.spec.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const MongoStorageAdapter = require('../lib/Adapters/Storage/Mongo/MongoStorageA
66
const PostgresStorageAdapter = require('../lib/Adapters/Storage/Postgres/PostgresStorageAdapter')
77
.default;
88
const ParseServer = require('../lib/ParseServer').default;
9+
const path = require('path');
10+
const { spawn } = require('child_process');
911

1012
describe('Server Url Checks', () => {
1113
let server;
@@ -62,20 +64,38 @@ describe('Server Url Checks', () => {
6264
}
6365
const newConfiguration = Object.assign({}, defaultConfiguration, {
6466
databaseAdapter,
65-
__indexBuildCompletionCallbackForTests: promise => {
66-
promise.then(() => {
67-
parseServer.handleShutdown();
68-
parseServer.server.close(err => {
69-
if (err) {
70-
done.fail('Close Server Error');
71-
}
72-
reconfigureServer({}).then(() => {
73-
done();
74-
});
67+
serverStartComplete: () => {
68+
parseServer.handleShutdown();
69+
parseServer.server.close(err => {
70+
if (err) {
71+
done.fail('Close Server Error');
72+
}
73+
reconfigureServer({}).then(() => {
74+
done();
7575
});
7676
});
7777
},
7878
});
7979
const parseServer = ParseServer.start(newConfiguration);
8080
});
81+
82+
it('does not have unhandled promise rejection in the case of load error', done => {
83+
const parseServerProcess = spawn(
84+
path.resolve(__dirname, './support/FailingServer.js')
85+
);
86+
let stdout;
87+
let stderr;
88+
parseServerProcess.stdout.on('data', data => {
89+
stdout = data.toString();
90+
});
91+
parseServerProcess.stderr.on('data', data => {
92+
stderr = data.toString();
93+
});
94+
parseServerProcess.on('close', code => {
95+
expect(code).toEqual(1);
96+
expect(stdout).toBeUndefined();
97+
expect(stderr).toContain('MongoNetworkError:');
98+
done();
99+
});
100+
});
81101
});

spec/PostgresInitOptions.spec.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ function createParseServer(options) {
2828
const parseServer = new ParseServer.default(
2929
Object.assign({}, defaultConfiguration, options, {
3030
serverURL: 'http://localhost:12666/parse',
31-
__indexBuildCompletionCallbackForTests: promise => {
32-
promise.then(() => {
31+
serverStartComplete: error => {
32+
if (error) {
33+
reject(error);
34+
} else {
3335
expect(Parse.applicationId).toEqual('test');
3436
const app = express();
3537
app.use('/parse', parseServer.app);
3638

3739
const server = app.listen(12666);
3840
Parse.serverURL = 'http://localhost:12666/parse';
3941
resolve(server);
40-
}, reject);
42+
}
4143
},
4244
})
4345
);

spec/helper.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,13 @@ const reconfigureServer = changedConfiguration => {
150150
defaultConfiguration,
151151
changedConfiguration,
152152
{
153-
__indexBuildCompletionCallbackForTests: indexBuildPromise =>
154-
indexBuildPromise.then(() => {
153+
serverStartComplete: error => {
154+
if (error) {
155+
reject(error);
156+
} else {
155157
resolve(parseServer);
156-
}, reject),
158+
}
159+
},
157160
mountPath: '/1',
158161
port,
159162
}

spec/index.spec.js

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -295,30 +295,28 @@ describe('server', () => {
295295
appId: 'aTestApp',
296296
masterKey: 'aTestMasterKey',
297297
serverURL: 'http://localhost:12666/parse',
298-
__indexBuildCompletionCallbackForTests: promise => {
299-
promise.then(() => {
300-
expect(Parse.applicationId).toEqual('aTestApp');
301-
const app = express();
302-
app.use('/parse', parseServer.app);
303-
304-
const server = app.listen(12666);
305-
const obj = new Parse.Object('AnObject');
306-
let objId;
307-
obj
308-
.save()
309-
.then(obj => {
310-
objId = obj.id;
311-
const q = new Parse.Query('AnObject');
312-
return q.first();
313-
})
314-
.then(obj => {
315-
expect(obj.id).toEqual(objId);
316-
server.close(done);
317-
})
318-
.catch(() => {
319-
server.close(done);
320-
});
321-
});
298+
serverStartComplete: () => {
299+
expect(Parse.applicationId).toEqual('aTestApp');
300+
const app = express();
301+
app.use('/parse', parseServer.app);
302+
303+
const server = app.listen(12666);
304+
const obj = new Parse.Object('AnObject');
305+
let objId;
306+
obj
307+
.save()
308+
.then(obj => {
309+
objId = obj.id;
310+
const q = new Parse.Query('AnObject');
311+
return q.first();
312+
})
313+
.then(obj => {
314+
expect(obj.id).toEqual(objId);
315+
server.close(done);
316+
})
317+
.catch(() => {
318+
server.close(done);
319+
});
322320
},
323321
})
324322
);
@@ -332,7 +330,8 @@ describe('server', () => {
332330
appId: 'anOtherTestApp',
333331
masterKey: 'anOtherTestMasterKey',
334332
serverURL: 'http://localhost:12667/parse',
335-
__indexBuildCompletionCallbackForTests: promise => {
333+
serverStartComplete: error => {
334+
const promise = error ? Promise.reject(error) : Promise.resolve();
336335
promise
337336
.then(() => {
338337
expect(Parse.applicationId).toEqual('anOtherTestApp');

spec/support/FailingServer.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
3+
const ParseServer = require('../../lib/index').ParseServer;
4+
5+
ParseServer.start({
6+
appId: 'test',
7+
masterKey: 'test',
8+
databaseURI:
9+
'mongodb://doesnotexist:27017/parseServerMongoAdapterTestDatabase',
10+
});

src/Options/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export interface ParseServerOptions {
177177
/* Live query server configuration options (will start the liveQuery server) */
178178
liveQueryServerOptions: ?LiveQueryServerOptions;
179179

180-
__indexBuildCompletionCallbackForTests: ?() => void;
180+
serverStartComplete: ?(error: ?Error) => void;
181181
}
182182

183183
export interface CustomPagesOptions {

0 commit comments

Comments
 (0)