Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
Add a close method on apn.
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge committed Jun 20, 2014
1 parent 0562629 commit 918a9ab
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -40,6 +40,16 @@ apn.send({

Additional fields can be found in [node-apn documentation](https://github.com/argon/node-apn/blob/master/doc/apn.markdown#class-apnnotification).

#### Closing connection

APN use a socket and keep it connected, so if we want to gracefully stop a process, you will need to close this connection. A close method is avalaible on the APN sender.

```js
apn.close(function () {
// the connection is closed
});
```

#### Events

##### transmitted
Expand Down
18 changes: 18 additions & 0 deletions lib/protocols/apn.js
Expand Up @@ -51,6 +51,24 @@ Sender.prototype.send = function (data) {
this.connection.pushNotification(notification, data.token);
};

/**
* Close all connections.
*
* @param {function} cb
*/

Sender.prototype.close = function (cb) {
if (! this.connection) {
if (cb) process.nextTick(cb);
return ;
}

this.connection.shutdown();
// node-apn use a delay of 2500ms before really closing the connection
// https://github.com/argon/node-apn/blob/3731fae9fefccdb7964606d819561058debf4b70/lib/connection.js#L355
if (cb) setTimeout(cb, 2600);
};

/**
* Create a new APN connection and proxify events.
*
Expand Down
58 changes: 51 additions & 7 deletions test/unit/protocols/apn.js
Expand Up @@ -6,7 +6,7 @@ var apn = require('apn');
var notify = require('../../../');

describe('APN', function () {
var pushNotificationFn;
var pushNotificationFn, apnSender;

beforeEach(function () {
pushNotificationFn = sinon.spy(function (notification) {
Expand All @@ -32,19 +32,15 @@ describe('APN', function () {
util.inherits(Connection, EventEmitter);

sinon.stub(apn, 'Connection', Connection);

apnSender = notify.apn({foo: 'bar'});
});

afterEach(function () {
apn.Connection.restore();
});

describe('#send', function () {
var apnSender;

beforeEach(function () {
apnSender = notify.apn({foo: 'bar'});
});

it('should create notification and send it', function () {
apnSender.send({
token: 'myToken',
Expand Down Expand Up @@ -126,4 +122,52 @@ describe('APN', function () {
expect(errorSpy).to.not.be.called;
});
});

describe('#close', function () {
describe('without active connection', function () {
it('should do nothing if there is no connection', function () {
apnSender.close();
});

it('should accept a callback', function (done) {
apnSender.close(done);
});
});

describe('with an active connection', function () {
var clock;

beforeEach(function () {
clock = sinon.useFakeTimers(0, 'setTimeout');
apnSender.connection = {
shutdown: sinon.spy()
};
});

afterEach(function () {
clock.restore();
});

it('should work without callback', function () {
apnSender.close();
expect(apnSender.connection.shutdown).to.be.called;
});

it('should wait 2600ms if there is a callback', function () {
var spy = sinon.spy();
apnSender.close(spy);

expect(spy).to.not.be.called;

clock.tick(1000);
expect(spy).to.not.be.called;

clock.tick(1000);
expect(spy).to.not.be.called;

clock.tick(1000);
expect(spy).to.be.called;
});
});
});
});

0 comments on commit 918a9ab

Please sign in to comment.