Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add support for socket timeouts #52

Open
wants to merge 1 commit into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 16, 2013
  1. @SPodjasek
This page is out of date. Refresh to see the latest.
Showing with 96 additions and 1 deletion.
  1. +3 −1 index.js
  2. +93 −0 test/socket_timeout.js
View
4 index.js
@@ -25,7 +25,9 @@ module.exports = function (opts, cb) {
);
server.on(connectionEvent, function (stream) {
var src = stream._bouncyStream = stealthBuffer();
-
+ // Node default timeout is 120 sec.
+ if(opts.socketTimeout)
+ stream.setTimeout(opts.socketTimeout);
// hack to work around a node 0.10 bug:
// https://github.com/joyent/node/commit/e11668b244ee62d9997d4871f368075b8abf8d45
if (/^v0\.10\.\d+$/.test(process.version)) {
View
93 test/socket_timeout.js
@@ -0,0 +1,93 @@
+var test = require('tap').test;
+var bouncy = require('../');
+var http = require('http');
+var net = require('net');
+
+test('socket timeout', function (t) {
+ t.plan(10);
+
+ var s0 = http.createServer(function (req, res) {
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ setTimeout((function() {
+ res.write('ping');
+ res.end();
+ }), 1500);
+ });
+ s0.listen(connect);
+
+ var s1 = bouncy({socketTimeout: 1000}, function (req, bounce) {
+ var s = bounce(s0.address().port);
+ t.ok(s instanceof net.Stream, 'bounce() @ 1000 returns a stream');
+ });
+ s1.listen(connect);
+
+ var s2 = bouncy({socketTimeout: 2000}, function (req, bounce) {
+ var s = bounce(s0.address().port);
+ t.ok(s instanceof net.Stream, 'bounce() @ 2000 returns a stream');
+ });
+ s2.listen(connect);
+
+ var s3 = bouncy(function (req, bounce) {
+ var s = bounce(s0.address().port);
+ t.ok(s instanceof net.Stream, 'bounce() @ default returns a stream');
+ });
+ s3.listen(connect);
+
+ function done () {
+ s0.close();
+ s1.close();
+ s2.close();
+ s3.close();
+ t.end();
+ }
+
+ var connected = 0;
+ function connect () {
+ if (++connected !== 4) return;
+ request(s1,
+ function () { t.ok(true, "timed out @ 1000") },
+ function () { t.fail("should fail with timeout") },
+ function () {
+ request(s2,
+ function () { t.fail("shouldn't fail") },
+ function (data) { t.equal(data, 'ping', "received proper response @ 2000") },
+ function () {
+ request(s3,
+ function () { t.fail("shouldn't fail") },
+ function (data) { t.equal(data, 'ping', "received proper response @ default timeout"); },
+ function() {
+ done();
+ }
+ );
+ }
+ );
+ }
+ );
+ }
+
+ function request (s, error, success, close) {
+ var opts = {
+ method : 'GET',
+ host : 'localhost',
+ headers : {
+ host : 'example.com',
+ connection : 'close'
+ },
+ port : s.address().port,
+ path : '/',
+ };
+ var req = http.request(opts, function (res) {
+ t.equal(res.statusCode, 200, "response status is 200/OK")
+ t.equal(res.headers['content-type'], 'text/plain', "response type is text/plain");
+
+ var data = '';
+ res.on('data', function (buf) {
+ data += buf.toString();
+ });
+
+ res.on('end', function () { success(data); close(); });
+ });
+ req.on('error', function () { error(); close(); });
+ req.end();
+ }
+});
Something went wrong with that request. Please try again.