Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
add extra tests for peer and peers logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejBaj committed Feb 23, 2017
2 parents a3c6175 + 0bb62c4 commit a8142ba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/unit/logic/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var chai = require('chai');
var express = require('express');
var ip = require('ip');
var _ = require('lodash');
var sinon = require('sinon');
var node = require('../../node.js');
Expand All @@ -18,6 +19,7 @@ describe('peer', function () {
describe('accept', function () {

it('should accept valid peer', function () {
var peer = new Peer({});
var __peer = peer.accept(randomPeer);
['height', 'ip', 'port', 'state'].forEach(function (property) {
node.expect(__peer[property]).equals(randomPeer[property]);
Expand All @@ -30,8 +32,19 @@ describe('peer', function () {
node.expect(__peer.port).to.equal(0);
node.expect(__peer.ip).to.be.undefined;
node.expect(__peer.state).to.equal(1);
node.expect(__peer.height).to.equal(1);
// node.expect(__peer.string).to.equal(1);
node.expect(__peer.height).to.be.undefined;
node.expect(__peer.string).to.be.undefined;
});

it('should accept peer with ip as long', function () {
var __peer = peer.accept({ip: ip.toLong(randomPeer.ip)});
node.expect(__peer.ip).to.equal(randomPeer.ip);
});

it('should convert dappid to array', function () {
var __peer = peer.accept({dappid: 'random-dapp-id'});
node.expect(__peer.dappid).to.be.an('array');
node.expect(_.isEqual(__peer.dappid, ['random-dapp-id'])).to.be.ok;
});
});

Expand Down
42 changes: 42 additions & 0 deletions test/unit/logic/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,27 @@ describe('peers', function () {
removeAll();
});

it('should not update height with insertOnly param', function () {
removeAll();

peers.upsert(randomPeer);
var list = peers.list();
var inserted = list[0];
node.expect(list.length).equal(1);
node.expect(arePeersEqual(inserted, randomPeer)).to.be.ok;

var modifiedPeer = _.clone(randomPeer);
modifiedPeer.height += 1;
peers.upsert(modifiedPeer, true);
list = peers.list();
var updated = list[0];
node.expect(list.length).equal(1);
node.expect(arePeersEqual(updated, modifiedPeer)).to.be.not.ok;
node.expect(arePeersEqual(updated, randomPeer)).to.be.ok;

removeAll();
});

it('should insert peer with different ports', function () {
removeAll();

Expand Down Expand Up @@ -226,6 +247,20 @@ describe('peers', function () {
node.expect(peers.list()[0].state).equal(1);
});

it('should do nothing when unban not added peer', function () {
removeAll();
peers.upsert(randomPeer);
node.expect(peers.list().length).equal(1);
node.expect(peers.list()[0].state).equal(2);

var differentPeer = _.clone(randomPeer);
differentPeer.port += 1;

peers.unban(differentPeer);
node.expect(peers.list().length).equal(1);
node.expect(arePeersEqual(peers.list()[0], randomPeer)).to.be.ok;
});

});

describe('remove', function () {
Expand All @@ -246,4 +281,11 @@ describe('peers', function () {
node.expect(peers.list().length).equal(0);
});
});

describe('bind', function () {

it('should be ok', function () {
peers.bind({});
});
});
});

0 comments on commit a8142ba

Please sign in to comment.