Skip to content

Commit

Permalink
Enhance tests - check topic
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought committed Aug 11, 2019
1 parent 83a6198 commit 56ae4b5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mqtt": "^3.0.0",
"nyc": "^14.1.1",
"pre-commit": "^1.2.2",
"qlobber": "^3.1.0",
"snazzy": "^8.0.0",
"standard": "^13.1.0",
"tape": "^4.11.0"
Expand Down
52 changes: 38 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var mqtt = require('mqtt')
var aedes = require('aedes')
var stats = require('./stats')
var net = require('net')
var QlobberTrue = require('qlobber').QlobberTrue
var matcher = new QlobberTrue({ wildcard_one: '+', wildcard_some: '#' })
var port = 1889
var clients = 0
var server
Expand Down Expand Up @@ -42,30 +44,42 @@ function connect (s, opts = {}) {
return client
}

function checkTopic (actual, expected) {
matcher.clear()
matcher.add(expected)
var bool = matcher.match(expected, actual)
matcher.clear()
return bool
}

test('Connect a client and subscribe to get total number of clients', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/clients/total'
var subscriber = connect(setup())

subscriber.subscribe('$SYS/+/clients/total')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.equal('1', message.toString(), 'clients connected')
subscriber.end()
t.end()
})
})

test('Connect a client and subscribe to get maximum number of clients', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/clients/maximum'
var s = setup()
var subscriber = connect(s, { clientId: 'subscriber' })
var additionalClient = connect(s, { clientId: 'client' })

subscriber.subscribe('$SYS/+/clients/maximum')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.equal('2', message.toString(), 'clients connected')
subscriber.end()
additionalClient.end()
Expand All @@ -74,15 +88,17 @@ test('Connect a client and subscribe to get maximum number of clients', function
})

test('Connect a client and subscribe to get current broker time', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/time'
var s = setup()
var subscriber = connect(s, { clientId: 'subscriber' })
var additionalClient = connect(s, { clientId: 'client' })

subscriber.subscribe('$SYS/+/time')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.equal(s.instance.stats.time.toISOString(), message.toString(), 'current broker time')
subscriber.end()
additionalClient.end()
Expand All @@ -91,14 +107,16 @@ test('Connect a client and subscribe to get current broker time', function (t) {
})

test('Connect a client and subscribe to get broker up-time', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/uptime'
var s = setup()
var subscriber = connect(s)

subscriber.subscribe('$SYS/+/uptime')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
var seconds = Math.round((s.instance.stats.time - s.instance.stats.started) / 1000)
t.equal(seconds.toString(), message.toString(), 'Broker uptime')
subscriber.end()
Expand All @@ -107,45 +125,51 @@ test('Connect a client and subscribe to get broker up-time', function (t) {
})

test('Connect a client and subscribe to get the number of published messages', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/messages/publish/sent'
var publisher = connect(setup())

publisher.subscribe('$SYS/+/messages/publish/sent', onSub)
publisher.subscribe(sysTopic, onSub)

function onSub () {
publisher.publish('publishing', 'hey there')
publisher.publish('publishing', 'hey there')
}

publisher.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.equal('2', message.toString(), 'number of published messages')
publisher.end()
})
})

test('Connect a client and and subscribe to get current heap usage', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/memory/heap/current'
var subscriber = connect(setup())

subscriber.subscribe('$SYS/+/memory/heap/current')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.pass(message.toString(), 'bytes of heap used currently')
subscriber.end()
t.end()
})
})

test('Connect a client and and subscribe to get maximum heap usage', function (t) {
t.plan(1)
t.plan(2)

var sysTopic = '$SYS/+/memory/heap/maximum'
var subscriber = connect(setup())

subscriber.subscribe('$SYS/+/memory/heap/maximum')
subscriber.subscribe(sysTopic)

subscriber.on('message', function (topic, message) {
t.ok(checkTopic(topic, sysTopic))
t.pass(message.toString(), 'max bytes of heap used till now')
subscriber.end()
t.end()
Expand Down

0 comments on commit 56ae4b5

Please sign in to comment.