Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
5 changed files
with
663 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ results | |
|
||
npm-debug.log | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"node": true, | ||
"indent": 2, | ||
"expr": true, | ||
"asi": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
var chai = require('chai'), | ||
assert = chai.assert, | ||
expect = chai.expect, | ||
bbt = require('../index'); | ||
|
||
var token = '' | ||
|
||
function createConnection() { | ||
return new bbt.Connector({ | ||
apiKey: process.env.APIKEY, | ||
secretKey: process.env.SECRETKEY | ||
}); | ||
} | ||
|
||
function createMqttConnection(ssl) { | ||
return new bbt.Stream({transport: { | ||
type: 'mqtt', | ||
apiKey: process.env.APIKEY, | ||
secretKey: process.env.SECRETKEY | ||
}}); | ||
} | ||
|
||
describe('beebotte.mqtt', function() { | ||
|
||
var mqttclient | ||
var msg = null | ||
|
||
function onSub (message) { | ||
msg = message.data | ||
} | ||
|
||
before(function() { | ||
mqttclient = createMqttConnection(true) | ||
}) | ||
|
||
it('should receive connected event upon connection', function (done){ | ||
|
||
var connected = false | ||
|
||
setTimeout(function () { | ||
if (!connected) { | ||
done('Failed to receive connected event after 5 seconds from connection') | ||
} | ||
}, 15000) | ||
|
||
mqttclient.on('connected', function() { | ||
connected = true | ||
done() | ||
}) | ||
}) | ||
|
||
it('should subscribe to channel with success', function (done){ | ||
|
||
var subscribed = false | ||
|
||
setTimeout(function () { | ||
if (!subscribed) { | ||
done('Failed to subscribe after 2 seconds from subscription request') | ||
} | ||
}, 5000) | ||
|
||
mqttclient.on('subscribed', function (sub) { | ||
expect(sub.channel).to.be.equal('test') | ||
expect(sub.resource).to.be.equal('test') | ||
subscribed = true | ||
done() | ||
}) | ||
|
||
mqttclient.subscribe('test', 'test', {read: true, write: true}, onSub) | ||
}) | ||
|
||
it('should receive published message with success', function (done){ | ||
|
||
setTimeout(function () { | ||
expect(msg).to.be.equal('msg1') | ||
done() | ||
}, 500) | ||
|
||
mqttclient.publish('test', 'test', 'msg1') | ||
}) | ||
|
||
it('should receive written message with success', function (done){ | ||
|
||
setTimeout(function () { | ||
expect(msg).to.be.equal('msg2') | ||
done() | ||
}, 500) | ||
|
||
mqttclient.write('test', 'test', 'msg2') | ||
}) | ||
|
||
it('should unsubscribe from channel with success', function (done){ | ||
|
||
var unsubscribed = false | ||
|
||
setTimeout(function () { | ||
if (!unsubscribed) { | ||
done('Failed to unsubscribe after 2 seconds from unsubscription request') | ||
} | ||
}, 5000) | ||
|
||
mqttclient.on('unsubscribed', function (sub) { | ||
expect(sub.channel).to.be.equal('test') | ||
expect(sub.resource).to.be.equal('test') | ||
unsubscribed = true | ||
done() | ||
}) | ||
|
||
mqttclient.unsubscribe('test', 'test') | ||
}) | ||
|
||
it('should disconnect with success', function (done){ | ||
|
||
var disconnected = false | ||
|
||
setTimeout(function () { | ||
if (!disconnected) { | ||
done('Failed to receive disconnected event after 2 seconds from disconnection') | ||
} | ||
}, 5000) | ||
|
||
mqttclient.on('disconnected', function () { | ||
disconnected = true | ||
done() | ||
}) | ||
|
||
mqttclient.disconnect() | ||
}) | ||
}) |
Oops, something went wrong.