Skip to content

Commit

Permalink
Fix issue _PushStatus stuck sending (#3808)
Browse files Browse the repository at this point in the history
* Adds test for not set device tokens

* Properly filter the installations without a deviceToken

* nit for slower PG test

* nit
  • Loading branch information
flovilmart authored May 12, 2017
1 parent 4a724e8 commit 88de01f
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
76 changes: 76 additions & 0 deletions spec/PushController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,80 @@ describe('PushController', () => {
done();
});
});

it('should not enqueue push when device token is not set', (done) => {
var auth = {
isMaster: true
}
var pushAdapter = {
send: function(body, installations) {
const promises = installations.map((device) => {
if (!device.deviceToken) {
// Simulate error when device token is not set
return Promise.reject();
}
return Promise.resolve({
transmitted: true,
device: device,
})
});

return Promise.all(promises);
},
getValidPushTypes: function() {
return ["ios"];
}
}

var pushController = new PushController();
const payload = {
data: {
alert: 'hello',
},
push_time: new Date().getTime() / 1000
}

var installations = [];
while(installations.length != 5) {
const installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_" + installations.length);
installation.set("deviceToken","device_token_" + installations.length)
installation.set("badge", installations.length);
installation.set("originalBadge", installations.length);
installation.set("deviceType", "ios");
installations.push(installation);
}

while(installations.length != 15) {
const installation = new Parse.Object("_Installation");
installation.set("installationId", "installation_" + installations.length);
installation.set("badge", installations.length);
installation.set("originalBadge", installations.length);
installation.set("deviceType", "ios");
installations.push(installation);
}

reconfigureServer({
push: { adapter: pushAdapter }
}).then(() => {
var config = new Config(Parse.applicationId);
return Parse.Object.saveAll(installations).then(() => {
return pushController.sendPush(payload, {}, config, auth);
}).then(() => new Promise(resolve => setTimeout(resolve, 100)));
}).then(() => {
const query = new Parse.Query('_PushStatus');
return query.find({useMasterKey: true}).then((results) => {
expect(results.length).toBe(1);
const pushStatus = results[0];
expect(pushStatus.get('numSent')).toBe(5);
expect(pushStatus.get('status')).toBe('succeeded');
done();
});
}).catch((err) => {
console.error(err);
fail('should not fail');
done();
});

});
});
6 changes: 5 additions & 1 deletion src/Push/PushQueue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ParseMessageQueue } from '../ParseMessageQueue';
import rest from '../rest';
import { isPushIncrementing } from './utils';
import deepcopy from 'deepcopy';

const PUSH_CHANNEL = 'parse-server-push';
const DEFAULT_BATCH_SIZE = 100;
Expand All @@ -27,7 +28,10 @@ export class PushQueue {
// Order by badge (because the payload is badge dependant)
// and createdAt to fix the order
const order = isPushIncrementing(body) ? 'badge,createdAt' : 'createdAt';

where = deepcopy(where);
if (!where.hasOwnProperty('deviceToken')) {
where['deviceToken'] = {'$exists': true};
}
return Promise.resolve().then(() => {
return rest.find(config,
auth,
Expand Down

0 comments on commit 88de01f

Please sign in to comment.