<commit_before>var test = require('tap').test;
var bouncy = require('../');
var net = require('net');
var EventEmitter = require('events').EventEmitter;

test('destroy the socket during piping', function (t) {
    t.plan(2);
    
    var p0 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
    var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
    
    var s0 = bouncy(function (req, bounce) {
        t.equal(req.headers.host, 'lulzy');
        
        var c = net.createConnection(p1, function () {
            bounce(c);
        });
    });
    
    s0.listen(p0, function () {
        var c = net.createConnection(p0, function () {
            c.write('POST /lul HTTP/1.1\r\n');
            c.write('Host: lulzy\r\n');
            c.write('Foo: bar\r\n');
            c.write('Content-Length: 7\r\n');
            c.write('\r\n');
            
            setTimeout(function () {
                t.throws(function () {
                    c.write('a=3&b=4');
                });
                c.end();
                s0.close();
                s1.close();
                t.end();
            }, 50);
        });
    });
    
    var s1 = net.createServer(function (c) {
        setTimeout(function () {
            c.destroy();
        }, 20);
    });
    s1.listen(p1);
});
<commit_msg>fix raw destroy test to check try/catch for 0.6 and onerror for 0.8<commit_after>var test = require('tap').test;
var bouncy = require('../');
var net = require('net');
var EventEmitter = require('events').EventEmitter;

test('destroy the socket during piping', function (t) {
    t.plan(2);
    
    var p0 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
    var p1 = Math.floor(Math.random() * (Math.pow(2,16) - 1e4) + 1e4);
    
    var s0 = bouncy(function (req, bounce) {
        t.equal(req.headers.host, 'lulzy');
        
        var c = net.connect(p1, function () {
            bounce(c);
        });
    });
    
    s0.listen(p0, function () {
        var c = net.connect(p0, function () {
            c.write('POST /lul HTTP/1.1\r\n');
            c.write('Host: lulzy\r\n');
            c.write('Foo: bar\r\n');
            c.write('Content-Length: 7\r\n');
            c.write('\r\n');
            
            setTimeout(function () {
                function onerror (err) {
                    t.equal(
                        err.message, 'This socket is closed.',
                        'write to closed socket emits an error'
                    );
                }
                
                c.once('error', onerror); // node 0.8 emits an error
                try {
                    c.write('a=3&b=4');
                }
                catch (err) {
                    onerror(err); // node 0.6 throws
                }
            }, 50);
        });
        
        t.on('end', function () {
            c.end();
        });
    });
    
    var s1 = net.createServer(function (c) {
        setTimeout(function () {
            c.destroy();
        }, 20);
    });
    s1.listen(p1);
    
    t.on('end', function () {
        s0.close();
        s1.close();
    });
});

{'fraction_non_alphanumeric': 0.17063870352716873, 'fraction_numerical': 0.02542103590721322, 'mean_word_length': 1.3651389932381668, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2054999', 'n_tokens_mistral': 1059, 'n_tokens_neox': 1016, 'n_words': 196}