Skip to content

Commit

Permalink
Allows a wifi success response to come in subsequent stdout packets
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyman727 committed Feb 12, 2016
1 parent 501dd4e commit bb04839
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
9 changes: 1 addition & 8 deletions lib/tessel/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Tessel.prototype.setWiFiState = function(enable) {
// End the connection
remoteProcess.close();
}, Tessel.__wifiConnectionTimeout);
remoteProcess.stdout.once('data', function(data) {
remoteProcess.stdout.on('data', function(data) {
if (data.indexOf('signal') > -1) {
logs.info('Successfully connected!');
clearTimeout(timeout);
Expand All @@ -167,13 +167,6 @@ Tessel.prototype.setWiFiState = function(enable) {
message: '' // resolve doesn't report messages
};
remoteProcess.close();
} else {
clearTimeout(timeout);
result = {
type: 'reject',
message: 'Unable to connect. Please check your wifi credentials and try again.'
};
remoteProcess.close();
}
});
remoteProcess.on('close', function() {
Expand Down
51 changes: 50 additions & 1 deletion test/unit/wifi.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
password: 'not_gonna_work'
};
var errMessage = 'Unable to connect to the network.';

// Set the timeout to be small so we don't wait forever for the test to complete
Tessel.__wifiConnectionTimeout = 10;
// Test is expecting several closes...;
this.tessel._rps.on('control', (command) => {
if (command.toString() === 'ubus call iwinfo info {"device":"wlan0"}') {
Expand Down Expand Up @@ -405,6 +406,54 @@ module.exports['Tessel.prototype.connectToNetwork'] = {
test.ok(this.getWifiInfo.callCount, 1);
test.done();
});
},
// Sometimes the keyword for success (signal) is not in the first batch
// of stdout data
batchedResponse: function(test) {
test.expect(9);
var creds = {
ssid: 'tank',
password: 'should work'
};

this.tessel._rps.on('control', (command) => {
if (command.toString() === 'ubus call iwinfo info {"device":"wlan0"}') {
// Write to stderr so it completes as expected
// Wrap in setImmediate to make sure listener is set up before emitting
setImmediate(() => {
// First write some random data
this.tessel._rps.stdout.emit('data', 'do not have success yet');
// then write the keyword we're looking for for success
this.tessel._rps.stdout.emit('data', 'signal');
});
} else {
setImmediate(() => {
// Remove any listeners on stderr so we don't break anything when we write to it
this.tessel._rps.stdout.removeAllListeners();

// Continue
this.tessel._rps.emit('close');
});
}
});

this.tessel.connectToNetwork(creds)
.then(() => {
test.equal(this.setNetworkSSID.callCount, 1);
test.equal(this.setNetworkPassword.callCount, 1);
test.equal(this.setNetworkEncryption.callCount, 1);
test.equal(this.commitWirelessCredentials.callCount, 1);
test.equal(this.reconnectWifi.callCount, 1);
test.ok(this.setNetworkSSID.lastCall.calledWith(creds.ssid));
test.ok(this.setNetworkPassword.lastCall.calledWith(creds.password));
test.ok(this.setNetworkEncryption.lastCall.calledWith('psk2'));
test.ok(this.getWifiInfo.callCount, 1);
test.done();
})
.catch((error) => {
test.fail('Should not have received an error with batched response', error);
test.done();
});
}
};

Expand Down

0 comments on commit bb04839

Please sign in to comment.