Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Failure stress test #85

Closed
wants to merge 2 commits into from

1 participant

@joseprl89

Implemented an stress test in folder test.

Once the test starts, it will periodically send a notification to APNS. Once the user unplugs the ethernet cable or disconnects the WIFI, the errorCallback or the on "Error" method should be called.

Currently, I can't see a relation between the network state and the output of this script (i.e. when I unplug the ethernet cable, I don't see the errors, sometimes, when I plug back, the errors are displayed (many at a time)...)

@joseprl89 joseprl89 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 8, 2013
Commits on Mar 12, 2013
  1. Added buffers notification option

    Josep authored
This page is out of date. Refresh to see the latest.
Showing with 79 additions and 10 deletions.
  1. +21 −10 lib/connection.js
  2. +58 −0 test/stress.js
View
31 lib/connection.js
@@ -38,6 +38,7 @@ if(process.env.DEBUG) {
* @config {Number} [cacheLength=100] Number of notifications to cache for error purposes (See Readme)
* @config {Boolean} [autoAdjustCache=false] Whether the cache should grow in response to messages being lost after errors. (Will still emit a 'cacheTooSmall' event)
* @config {Number} [connectionTimeout=0] The duration the socket should stay alive with no activity in milliseconds. 0 = Disabled.
+ * @config {Boolean} [buffersNotifications=true] Whether to buffer notifications and resend them on reconnections.
*/
function Connection (options) {
if(false === (this instanceof Connection)) {
@@ -59,7 +60,8 @@ function Connection (options) {
errorCallback: undefined,
cacheLength: 100,
autoAdjustCache: true,
- connectionTimeout: 0
+ connectionTimeout: 0,
+ buffersNotifications: true,
};
util.extend(this.options, options);
@@ -73,8 +75,11 @@ function Connection (options) {
this.currentId = 0;
this.cachedNotifications = [];
- this.notificationBuffer = [];
-
+
+ if ( this.options.buffersNotifications ) {
+ this.notificationBuffer = [];
+ }
+
events.EventEmitter.call(this);
};
@@ -236,9 +241,11 @@ Connection.prototype.socketDrained = function() {
return;
}
debug("Socket writeable");
- if (this.notificationBuffer.length > 0) {
- debug("Sending notification from buffer");
- this.sendNotification(this.notificationBuffer.shift());
+ if ( this.options.buffersNotifications ) {
+ if (this.notificationBuffer.length > 0) {
+ debug("Sending notification from buffer");
+ this.sendNotification(this.notificationBuffer.shift());
+ }
}
};
@@ -280,9 +287,11 @@ Connection.prototype.restartConnection = function() {
this.socket = undefined;
this.deferredConnection = undefined;
- if (this.notificationBuffer.length) {
- debug("Notification queue has %d items, resending the first", this.notificationBuffer.length);
- this.sendNotification(this.notificationBuffer.shift());
+ if ( this.options.buffersNotifications ) {
+ if (this.notificationBuffer.length) {
+ debug("Notification queue has %d items, resending the first", this.notificationBuffer.length);
+ this.sendNotification(this.notificationBuffer.shift());
+ }
}
};
@@ -290,7 +299,9 @@ Connection.prototype.restartConnection = function() {
* @private
*/
Connection.prototype.bufferNotification = function (notification) {
- this.notificationBuffer.push(notification);
+ if ( this.options.buffersNotifications ) {
+ this.notificationBuffer.push(notification);
+ }
};
/**
View
58 test/stress.js
@@ -0,0 +1,58 @@
+// Require apn
+var apns = require('apn');
+
+// This function should be called every time a notification is not properly sent to APNS.
+var callbackFunction = function(err, notification){
+ console.log("Fail callback Called : " + err);
+ console.log("notification: " + notification);
+};
+
+var deviceToken = "INSERT YOUR TOKEN HERE";
+
+// APNS Connection Options.
+var options = {
+ cert: null, /* Certificate file path */
+ certData: null, /* String or Buffer containing certificate data, if supplied uses this instead of cert file path */
+ key: null, /* Key file path */
+ keyData: null, /* String or Buffer containing key data, as certData */
+ passphrase: null, /* A passphrase for the Key file */
+ ca: null, /* String or Buffer of CA data to use for the TLS connection */
+ pfx: null, /* File path for private key, certificate and CA certs in PFX or PKCS12 format. If supplied will be used instead of certificate and key above */
+ pfxData: null, /* PFX or PKCS12 format data containing the private key, certificate and CA certs. If supplied will be used instead of loading from disk. */
+ gateway: 'gateway.sandbox.push.apple.com',
+ port: 2195, /* gateway port */
+ rejectUnauthorized: true, /* Value of rejectUnauthorized property to be passed through to tls.connect() */
+ enhanced: true, /* enable enhanced format */
+ cacheLength: 100, /* Number of notifications to cache for error purposes */
+ autoAdjustCache: true, /* Whether the cache should grow in response to messages being lost after errors. */
+ connectionTimeout: 60000, /* The duration the socket should stay alive with no activity in milliseconds. 0 = Disabled. */
+ errorCallback: callbackFunction /* Callback when error occurs function(err,notification) */
+};
+
+// Creating the connection and the device to send notifications.
+var apnsConnection = new apns.Connection(options);
+var myDevice = new apns.Device(deviceToken);
+
+//////////////////////////////////////////////////////////////////////////////////////
+// This method runs every 2 seconds, and tries to send a notification to the device.//
+//////////////////////////////////////////////////////////////////////////////////////
+
+var periodicTask = function () {
+
+ var note = new apns.Notification();
+
+ note.expiry = Math.floor(Date.now() / 1000) + 3600; // Expires 1 hour from now.
+ note.badge = 3;
+ note.sound = "ping.aiff";
+ note.alert = "You have a new message";
+ note.payload = {'messageFrom': 'Caroline'};
+ note.device = myDevice;
+ apnsConnection.sendNotification(note);
+
+ console.log("Sent notification...");
+ setTimeout(periodicTask, 2 * 1000);
+}
+
+// Start running periodically the task.
+periodicTask();
+
Something went wrong with that request. Please try again.