Skip to content

Commit 88de01f

Browse files
authored
Fix issue _PushStatus stuck sending (#3808)
* Adds test for not set device tokens * Properly filter the installations without a deviceToken * nit for slower PG test * nit
1 parent 4a724e8 commit 88de01f

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

spec/PushController.spec.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,4 +657,80 @@ describe('PushController', () => {
657657
done();
658658
});
659659
});
660+
661+
it('should not enqueue push when device token is not set', (done) => {
662+
var auth = {
663+
isMaster: true
664+
}
665+
var pushAdapter = {
666+
send: function(body, installations) {
667+
const promises = installations.map((device) => {
668+
if (!device.deviceToken) {
669+
// Simulate error when device token is not set
670+
return Promise.reject();
671+
}
672+
return Promise.resolve({
673+
transmitted: true,
674+
device: device,
675+
})
676+
});
677+
678+
return Promise.all(promises);
679+
},
680+
getValidPushTypes: function() {
681+
return ["ios"];
682+
}
683+
}
684+
685+
var pushController = new PushController();
686+
const payload = {
687+
data: {
688+
alert: 'hello',
689+
},
690+
push_time: new Date().getTime() / 1000
691+
}
692+
693+
var installations = [];
694+
while(installations.length != 5) {
695+
const installation = new Parse.Object("_Installation");
696+
installation.set("installationId", "installation_" + installations.length);
697+
installation.set("deviceToken","device_token_" + installations.length)
698+
installation.set("badge", installations.length);
699+
installation.set("originalBadge", installations.length);
700+
installation.set("deviceType", "ios");
701+
installations.push(installation);
702+
}
703+
704+
while(installations.length != 15) {
705+
const installation = new Parse.Object("_Installation");
706+
installation.set("installationId", "installation_" + installations.length);
707+
installation.set("badge", installations.length);
708+
installation.set("originalBadge", installations.length);
709+
installation.set("deviceType", "ios");
710+
installations.push(installation);
711+
}
712+
713+
reconfigureServer({
714+
push: { adapter: pushAdapter }
715+
}).then(() => {
716+
var config = new Config(Parse.applicationId);
717+
return Parse.Object.saveAll(installations).then(() => {
718+
return pushController.sendPush(payload, {}, config, auth);
719+
}).then(() => new Promise(resolve => setTimeout(resolve, 100)));
720+
}).then(() => {
721+
const query = new Parse.Query('_PushStatus');
722+
return query.find({useMasterKey: true}).then((results) => {
723+
expect(results.length).toBe(1);
724+
const pushStatus = results[0];
725+
expect(pushStatus.get('numSent')).toBe(5);
726+
expect(pushStatus.get('status')).toBe('succeeded');
727+
done();
728+
});
729+
}).catch((err) => {
730+
console.error(err);
731+
fail('should not fail');
732+
done();
733+
});
734+
735+
});
660736
});

src/Push/PushQueue.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ParseMessageQueue } from '../ParseMessageQueue';
22
import rest from '../rest';
33
import { isPushIncrementing } from './utils';
4+
import deepcopy from 'deepcopy';
45

56
const PUSH_CHANNEL = 'parse-server-push';
67
const DEFAULT_BATCH_SIZE = 100;
@@ -27,7 +28,10 @@ export class PushQueue {
2728
// Order by badge (because the payload is badge dependant)
2829
// and createdAt to fix the order
2930
const order = isPushIncrementing(body) ? 'badge,createdAt' : 'createdAt';
30-
31+
where = deepcopy(where);
32+
if (!where.hasOwnProperty('deviceToken')) {
33+
where['deviceToken'] = {'$exists': true};
34+
}
3135
return Promise.resolve().then(() => {
3236
return rest.find(config,
3337
auth,

0 commit comments

Comments
 (0)