Skip to content

Commit

Permalink
Dedupe Tessels found on LAN connections. Fixes gh-719 (#805)
Browse files Browse the repository at this point in the history
The problem here was:

1. Nothing was actually being put into `this.discovered`
2. Even if there had been, the test we did have would've passed anyway because it just put a number in the list and then emitted a number. That would assume that the thing put into `this.discovered` had the same object identity as the thing being checked, which is wrong.

Signed-off-by: Rick Waldron <waldron.rick@gmail.com>
  • Loading branch information
rwaldron committed Jul 21, 2016
1 parent 25188c3 commit 19686e4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/lan-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,18 @@ LAN.Scanner.prototype.start = function() {
});

// When the browser finds a new device
this.browser.on('update', (data) => {
this.browser.on('update', (discovered) => {
try {
debug(`Device found: ${data.fullname}`);
// Check if it's a Tessel
if (this.discovered.indexOf(data) > -1) {
debug(`Device found: ${discovered.fullname}`);

// Check against already discovered devices
if (this.discovered.find(known => known.host === discovered.host)) {
return;
}
// Add to list of discovered devices
this.discovered.push(discovered);

var connection = new LAN.Connection(data);

this.emit('connection', connection);
this.emit('connection', new LAN.Connection(discovered));
} catch (err) {
this.emit('error', err);
}
Expand Down
49 changes: 44 additions & 5 deletions test/unit/lan-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,30 +200,69 @@ exports['LAN.Scanner.prototype.start'] = {
},

updateDiscovered: function(test) {
test.expect(1);
test.expect(3);

var data = {};
var connectionHandler = this.sandbox.spy();

this.scanner.start();
test.equal(this.scanner.discovered.length, 0);
this.scanner.on('connection', connectionHandler);
this.scanner.browser.emit('update', data);

test.equal(connectionHandler.callCount, 1);
test.equal(this.scanner.discovered.length, 1);
test.done();
},

updateDiscoveredExists: function(test) {
test.expect(1);
test.expect(4);

var connectionHandler = this.sandbox.spy();
var discovery = {
addresses: ['192.168.1.9'],
query: ['_tessel._tcp.local'],
type: [{
name: 'tessel',
protocol: 'tcp',
subtypes: [],
description: undefined
}],
port: 22,
fullname: 'bishop._tessel._tcp.local',
txt: [''],
host: 'bishop.local',
interfaceIndex: 0,
networkInterface: 'en0'
};

var connection = {
auth: {
host: '192.168.1.9',
port: 22,
username: 'root',
passphrase: '',
privateKey: undefined,
readyTimeout: 5000
},
ip: '192.168.1.9',
host: 'bishop.local',
connectionType: 'LAN',
authorized: false,
ssh: undefined
};

test.equal(this.scanner.discovered.length, 0);

this.scanner.start();
this.scanner.on('connection', connectionHandler);
this.scanner.discovered.push(1);
this.scanner.browser.emit('update', 1);
this.scanner.start();

this.scanner.discovered.push(connection);
test.equal(this.scanner.discovered.length, 1);
this.scanner.browser.emit('update', discovery);

test.equal(connectionHandler.callCount, 0);
test.equal(this.scanner.discovered.length, 1);
test.done();
},

Expand Down

0 comments on commit 19686e4

Please sign in to comment.