All files / copacetic/lib/health-strategies/redis/adapters ioredis.js

94.44% Statements 17/18
66.66% Branches 4/6
100% Functions 9/9
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62              12x 12x               7x 1x     6x 6x       5x 5x     3x 3x     2x                 2x 2x                 2x 2x             12x  
/**
 * An adapter for the ioredis redis client
 * https://github.com/luin/ioredis
 * @class
 */
class IORedisAdapter {
  constructor (IORedis) {
    this.IORedis = IORedis
    this.isConnected = false
  }
 
  /**
   * @param {String} url Redis connection url
   * @param {Number} connectTimeout How long to wait when connecting, before timing out
   */
  connect (url, connectTimeout) {
    if (this.redis) {
      return this.redis.connect()
    }
 
    return new Promise((resolve, reject) => {
      this.redis = new this.IORedis(url, {
        connectTimeout
      })
        .on('connect', () => {
          this.isConnected = true
          return resolve(true)
        })
        .on('error', (err) => {
          this.isConnected = false
          return reject(err)
        })
        .on('close', () => {
          this.isConnected = false
        })
    })
  }
 
  /**
   * Disconnects from redis
   */
  close () {
    Eif (this.redis) {
      return this.redis.disconnect()
    }
  }
 
  /**
   * Attempts to ping redis
   * @return {Promise.<Boolean>|Error}
   */
  ping () {
    Eif (this.redis) {
      return this.redis.ping()
    }
 
    return Promise.reject(new Error('Redis instance does not exist'))
  }
}
 
module.exports = IORedis => new IORedisAdapter(IORedis)