source: trip-planner-front/node_modules/needle/test/socket_cleanup_spec.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.8 KB
Line 
1var should = require('should'),
2 needle = require('./../'),
3 fs = require('fs'),
4 https = require('https'),
5 stream = require('stream');
6
7describe('socket cleanup', function(){
8
9 var outFile = 'test/tmp';
10 var httpAgent, readStream, writeStream
11
12 var file = 'ubuntu-21.04-desktop-amd64.iso',
13 url = 'https://releases.ubuntu.com/21.04/' + file;
14
15 function getActiveSockets() {
16 return Object.keys(httpAgent.sockets).length
17 }
18
19 before(function() {
20 httpAgent = new https.Agent({
21 keepAlive : true,
22 maxSockets : 1
23 });
24 })
25
26 after(function() {
27 httpAgent.destroy()
28 fs.unlinkSync(outFile);
29 })
30
31 it('should cleanup sockets on ERR_STREAM_PREMATURE_CLOSE (using .pipe)', function(done) {
32 getActiveSockets().should.eql(0);
33
34 var resp = needle.get(url, { agent: httpAgent });
35 var writable = fs.createWriteStream(outFile);
36 resp.pipe(writable);
37
38 writable.on('close', function(e) {
39 if (!resp.done) resp.abort();
40 })
41
42 setTimeout(function() {
43 getActiveSockets().should.eql(1);
44 writable.destroy();
45 }, 50);
46
47 setTimeout(function() {
48 getActiveSockets().should.eql(0);
49 done();
50 }, 500); // takes a bit
51 })
52
53 it('should cleanup sockets on ERR_STREAM_PREMATURE_CLOSE (using stream.pipeline)', function(done) {
54 if (!stream.pipeline)
55 return done()
56
57 getActiveSockets().should.eql(0);
58
59 var resp = needle.get(url, { agent: httpAgent });
60 var writable = fs.createWriteStream(outFile);
61
62 stream.pipeline(resp, writable, function(err) {
63 err.code.should.eql('ERR_STREAM_PREMATURE_CLOSE')
64 // if (err) resp.request.destroy();
65 });
66
67 setTimeout(function() {
68 getActiveSockets().should.eql(1);
69 writable.destroy();
70 }, 50);
71
72 setTimeout(function() {
73 getActiveSockets().should.eql(0);
74 done();
75 }, 1000); // takes a bit
76
77 })
78
79})
Note: See TracBrowser for help on using the repository browser.