Skip to content

Commit

Permalink
test: in addition to cancellation support
Browse files Browse the repository at this point in the history
  • Loading branch information
analog-nico committed Jul 30, 2016
1 parent 94c2e30 commit 54d27f6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/fixtures/memory-usage/aborted-requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

// Run with: node --expose-gc ./test/fixtures/memory-usage/aborted-requests.js

var _ = require('lodash');
var Bluebird = require('bluebird');
var rp = require('../../../');
// var rp = require('request');


var requests = [];
function createRequest(retain) {

var request = rp('http://localhost:4000');
request.abort();

if (retain) {
requests.push(request);
}

}

function checkMemory() {
global.gc(); // Requires the --expose-gc option
console.log(process.memoryUsage().heapUsed + ' bytes in use');
}

Bluebird.resolve()
.delay(1000)
.then(checkMemory)
.then(function () {

_.times(100000, function () {

createRequest(false); // Should be garbage collected because no reference is kept

});

})
.delay(1000)
.then(checkMemory)
.then(function () {

_.times(100000, function () {

createRequest(true); // Should NOT be garbage collected because reference is kept in requests

});

})
.delay(1000)
.then(checkMemory);
22 changes: 22 additions & 0 deletions test/spec/request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,23 @@ describe('Request-Promise', function () {
});

it('.cancel() cancelling the Bluebird promise and aborting the request', function (done) {

var req = rp('http://localhost:4000/503');
req.once('abort', done);

req.cancel();

});

it('.cancel() on promises chained from the Bluebird promise, aborting the request', function (done) {

var req = rp('http://localhost:4000/503');
req.once('abort', done);

req.then(function noop() { }).cancel();

});

});

describe('should still allow to require Request independently', function () {
Expand Down Expand Up @@ -172,4 +179,19 @@ describe('Request-Promise', function () {

});

it('should not resolve the promise if .abort() is used', function (done) {

var request = rp('http://localhost:4000/200');

request.promise()
.finally(function () {
done(new Error('The promise should not be resolved'));
});

request.abort();

setTimeout(done, 100);

});

});

0 comments on commit 54d27f6

Please sign in to comment.