Skip to content

Commit

Permalink
Merge 6937891 into 7416ea6
Browse files Browse the repository at this point in the history
  • Loading branch information
eordano committed Jan 20, 2015
2 parents 7416ea6 + 6937891 commit bf9321b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ Peer.prototype._readMessage = function() {
var message = Messages.parseMessage(this.network, this.dataBuffer);

if (message) {
this.emit('message', message);
this.emit(message.command, message);
this._readMessage();
}
Expand Down
Binary file added test/connection.log
Binary file not shown.
51 changes: 51 additions & 0 deletions test/peer.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
'use strict';

var _ = require('lodash');
var chai = require('chai');
var Net = require('net');
var Socks5Client = require('socks5-client');

/* jshint unused: false */
var should = chai.should();
var expect = chai.expect;
var sinon = require('sinon');
var fs = require('fs');

var bitcore = require('bitcore');
var P2P = require('../');
Expand All @@ -15,6 +18,54 @@ var Networks = bitcore.Networks;

describe('Peer', function() {

describe('Integration test', function() {
it('parses this stream of data from a connection', function(callback) {
var peer = new P2P.Peer('');
var stub = sinon.stub();
var dataCallback;
var connectCallback;
var expected = {
version: 1,
verack: 1,
inv: 18,
addr: 4
};
var received = {
version: 0,
verack: 0,
inv: 0,
addr: 0
};
stub.on = function() {
if (arguments[0] === 'data') {
dataCallback = arguments[1];
}
if (arguments[0] === 'connect') {
connectCallback = arguments[1];
}
};
stub.write = function() {
};
stub.connect = function() {
connectCallback();
};
peer._getSocket = function() {
return stub;
};
peer.on('connect', function() {
dataCallback(fs.readFileSync('./test/connection.log'));
});
peer.on('message', function(message) {
received[message.command]++;
if (_.isEqual(received, expected)) {
callback();
}
});
peer.connect();
});
});


it('should be able to create instance', function() {
var peer = new Peer('localhost');
peer.host.should.equal('localhost');
Expand Down

0 comments on commit bf9321b

Please sign in to comment.