'use strict';

var drain = require('it-drain');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var drain__default = /*#__PURE__*/_interopDefaultLegacy(drain);

function testTimeout(fn) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const start = Date.now();
      let res = fn();
      if (res[Symbol.asyncIterator]) {
        res = drain__default["default"](res);
      }
      res.then(result => {
        const timeTaken = Date.now() - start;
        if (timeTaken < 100) {
          return resolve();
        }
        reject(new Error(`API call did not time out after ${ timeTaken }ms, got ${ JSON.stringify(result, null, 2) }`));
      }, err => {
        if (err.name === 'TimeoutError') {
          return resolve();
        }
        const timeTaken = Date.now() - start;
        reject(new Error(`Expected TimeoutError after ${ timeTaken }ms, got ${ err.stack }`));
      });
    }, 10);
  });
}

module.exports = testTimeout;
