All files / copacetic/lib/health-strategies/generic ping.js

100% Statements 10/10
100% Branches 2/2
100% Functions 3/3
100% Lines 10/10

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 431x 1x   1x                 9x   9x                 5x   5x         5x             2x       1x  
const precond = require('precond')
const humanInterval = require('human-interval')
 
const HealthStrategy = require('../strategy')
 
/**
 * Ping an adapter to test a connection
 * @class
 * @extends HealthStrategy
 */
class PingStrategy extends HealthStrategy {
  constructor (adapter, { timeout } = {}) {
    super(timeout)
 
    this.adapter = adapter
  }
 
  /**
   * @param {String} url The connection url
   * @param {String} timeout How long to wait until giving up on a connection attempt
   * @return {Promise}
   */
  check (url, timeout = this.timeout) {
    const connectTimeoutMS = humanInterval(timeout)
 
    precond.checkIsNumber(
      connectTimeoutMS,
      'An invalid format was used for Ping Interval, check human-interval for examples'
    )
 
    return this.adapter.ping(url, connectTimeoutMS)
  }
 
  /**
   * Destroys the connection
   */
  cleanup () {
    return this.adapter.close()
  }
}
 
module.exports = PingStrategy