<commit_before>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()
  })
})
<commit_msg>#29: enhance error catching in MQTT tests<commit_after>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() {

  this.timeout(10000)

  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(new Error('Failed to receive disconnected event after 2 seconds from disconnection'))
      } else {
        done()
      }
    }, 5000)

    mqttclient.on('disconnected', function () {
      disconnected = true
    })

    mqttclient.disconnect()
  })
})

{'fraction_non_alphanumeric': 0.13707865168539327, 'fraction_numerical': 0.011927398444252377, 'mean_word_length': 2.8728246318607766, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6833997', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1724, 'n_words': 425}