From 9b1bbb92a74ab4eaf4b52a8e20cb27b710d1c2cd Mon Sep 17 00:00:00 2001 From: Jannis R Date: Fri, 8 Feb 2019 13:42:32 +0100 Subject: [PATCH] test for retry :white_check_mark: --- retry.js | 4 ++-- test/index.js | 1 + test/retry.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 test/retry.js diff --git a/retry.js b/retry.js index 6b550378..6155d037 100644 --- a/retry.js +++ b/retry.js @@ -2,7 +2,7 @@ const retry = require('p-retry') -const request = require('./lib/request') +const _request = require('./lib/request') const createClient = require('.') const defaultRetryOpts = { @@ -11,7 +11,7 @@ const defaultRetryOpts = { minTimeout: 5 * 1000 } -const createClientWithRetry = (profile, userAgent, retryOpts = defaultRetryOpts) => { +const createClientWithRetry = (profile, userAgent, retryOpts = defaultRetryOpts, request = _request) => { const requestWithRetry = (profile, userAgent, opt, data) => { const attempt = () => { return request(profile, userAgent, opt, data) diff --git a/test/index.js b/test/index.js index d9847d0a..e759f763 100644 --- a/test/index.js +++ b/test/index.js @@ -11,3 +11,4 @@ require('./cmta') require('./sbahn-muenchen') require('./saarfahrplan') require('./throttle') +require('./retry') diff --git a/test/retry.js b/test/retry.js new file mode 100644 index 00000000..719d8d40 --- /dev/null +++ b/test/retry.js @@ -0,0 +1,47 @@ +'use strict' + +const test = require('tape') + +const createClientWithRetry = require('../retry') +const vbbProfile = require('../p/vbb') +const _request = require('../lib/request') + +const userAgent = 'public-transport/hafas-client:test' +const spichernstr = '900000042101' + +test('retry works', (t) => { + // for the first 3 calls, return different kinds of errors + let calls = 0 + const failingRequest = (profile, userAgent, opt, data) => { + calls++ + if (calls === 1) { + const err = new Error('HAFAS error') + err.isHafasError = true + return Promise.reject(err) + } + if (calls === 2) { + const err = new Error('fetch error') + err.code = 'EFOO' + return Promise.reject(err) + } + if (calls < 4) return Promise.reject(new Error('generic error')) + return Promise.resolve([]) + } + + const client = createClientWithRetry(vbbProfile, userAgent, { + retries: 3, + minTimeout: 100, + factor: 2, + randomize: false + }, failingRequest) + + t.plan(1 + 4) + client.departures(spichernstr, {duration: 1}) + .then(deps => t.deepEqual(deps, [], 'resolved with invalid value')) + .catch(t.ifError) + + setTimeout(() => t.equal(calls, 1), 50) // buffer + setTimeout(() => t.equal(calls, 2), 150) // 100 + buffer + setTimeout(() => t.equal(calls, 3), 350) // 100 + 200 + buffer + setTimeout(() => t.equal(calls, 4), 750) // 100 + 200 + 400 + buffer +})