From 3508c104b0f4c4cf08210cbd12f888f381d151e6 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Thu, 11 Nov 2021 13:17:26 -0800 Subject: [PATCH] fix(retry-queue): Got unit tests passing --- src/lib/retry-queue.mjs | 9 +- test/unit/retry-queue.adapter.unit.mjs | 251 +++++++++++++------------ 2 files changed, 134 insertions(+), 126 deletions(-) diff --git a/src/lib/retry-queue.mjs b/src/lib/retry-queue.mjs index f553481..b093c01 100644 --- a/src/lib/retry-queue.mjs +++ b/src/lib/retry-queue.mjs @@ -9,9 +9,6 @@ pay-to-write-access-controller.js depends on this library. */ -// const PQueue = require('p-queue').default -'use strict' - import PQueue from 'p-queue' import pRetry from 'p-retry' @@ -55,7 +52,7 @@ class RetryQueue { ) return returnVal } catch (err) { - console.error('Error in addToQueue(): ', err) + console.error('Error in addToQueue()') throw err } } @@ -101,9 +98,9 @@ class RetryQueue { console.log(errorMsg) const SLEEP_TIME = 30000 console.log(`Waiting ${SLEEP_TIME} milliseconds before trying again.\n`) - await _this.bchjs.Util.sleep(SLEEP_TIME) // 30 sec + await _this.sleep(SLEEP_TIME) // 30 sec } catch (err) { - console.error('Error in handleValidationError(): ', err) + console.error('Error in handleValidationError()') throw err } } diff --git a/test/unit/retry-queue.adapter.unit.mjs b/test/unit/retry-queue.adapter.unit.mjs index 78bd3c7..e8d5139 100644 --- a/test/unit/retry-queue.adapter.unit.mjs +++ b/test/unit/retry-queue.adapter.unit.mjs @@ -32,127 +32,138 @@ describe('#retry-queue.js', () => { assert.include(err.message, 'function handler is required') } }) - // - // it('should throw an error if input object is not provided', async () => { - // try { - // const funcHandler = () => {} - // await uut.retryWrapper(funcHandler) - // assert.fail('unexpected code path') - // } catch (err) { - // assert.include(err.message, 'input object is required') - // } - // }) - // - // it('should execute the given function.', async () => { - // const inputTest = 'test' - // // func mock to execute into the retry wrapper - // const funcHandle = sinon.spy() - // - // await uut.retryWrapper(funcHandle, inputTest) - // - // assert.equal(inputTest, funcHandle.getCall(0).args[0]) - // assert.equal(funcHandle.callCount, 1) - // }) - // - // it('should call handleValidationError() when p-retry error is thrown', async () => { - // try { - // // Mock for ignore sleep time - // sandbox.stub(uut.bchjs.Util, 'sleep').resolves({}) - // - // const inputTest = 'test' - // const funcHandle = () => { - // throw new Error('test error') - // } - // uut.attempts = 1 - // await uut.retryWrapper(funcHandle, inputTest) - // assert.fail('unexpected code path') - // } catch (err) { - // assert.include(err.message, 'test error') - // } - // }) - // - // it('should retry the specific number of times before giving up', async () => { - // // Mock for ignore sleep time - // sandbox.stub(uut.bchjs.Util, 'sleep').resolves({}) - // - // const inputTest = 'test' - // const funcHandle = () => { - // throw new Error('test error') - // } - // // func handler - // const spy = sinon.spy(funcHandle) - // - // // p-retry attempts - // const attempts = 1 - // - // try { - // uut.attempts = attempts - // await uut.retryWrapper(spy, inputTest) - // assert.fail('unexpected code path') - // } catch (error) { - // assert.equal(spy.callCount, attempts + 1) - // } - // }) + + it('should throw an error if input object is not provided', async () => { + try { + const funcHandler = () => {} + await uut.retryWrapper(funcHandler) + assert.fail('unexpected code path') + } catch (err) { + assert.include(err.message, 'input object is required') + } + }) + + it('should execute the given function.', async () => { + const inputTest = 'test' + // func mock to execute into the retry wrapper + const funcHandle = sinon.spy() + + await uut.retryWrapper(funcHandle, inputTest) + + assert.equal(inputTest, funcHandle.getCall(0).args[0]) + assert.equal(funcHandle.callCount, 1) + }) + + it('should call handleValidationError() when p-retry error is thrown', async () => { + try { + // Mock for ignore sleep time + sandbox.stub(uut, 'sleep').resolves({}) + + const inputTest = 'test' + const funcHandle = () => { + throw new Error('test error') + } + uut.attempts = 1 + + await uut.retryWrapper(funcHandle, inputTest) + + assert.fail('unexpected code path') + } catch (err) { + assert.include(err.message, 'test error') + } + }) + + it('should retry the specific number of times before giving up', async () => { + // Mock for ignore sleep time + sandbox.stub(uut, 'sleep').resolves({}) + + const inputTest = 'test' + const funcHandle = () => { + throw new Error('test error') + } + // func handler + const spy = sinon.spy(funcHandle) + + // p-retry attempts + const attempts = 1 + + try { + uut.attempts = attempts + + await uut.retryWrapper(spy, inputTest) + + assert.fail('unexpected code path') + } catch (error) { + assert.equal(spy.callCount, attempts + 1) + } + }) }) - // describe('#addToQueue', () => { - // it('should throw an error if function handler is not provided', async () => { - // try { - // await uut.addToQueue() - // assert.fail('unexpected code path') - // } catch (err) { - // assert.include(err.message, 'function handler is required') - // } - // }) - // - // it('should throw an error if input object is not provided', async () => { - // try { - // const funcHandler = () => {} - // await uut.addToQueue(funcHandler) - // assert.fail('unexpected code path') - // } catch (err) { - // assert.include(err.message, 'input object is required') - // } - // }) - // - // it('should add a function and input object to the queue and execute them', async () => { - // const inputTest = 'test' - // // func mock to execute into the retry wrapper - // const funcHandle = sinon.spy() - // - // await uut.addToQueue(funcHandle, inputTest) - // assert.equal(inputTest, funcHandle.getCall(0).args[0]) - // assert.equal(funcHandle.callCount, 1) - // }) - // - // it('should catch and throw an error', async () => { - // try { - // // Mock for ignore sleep time - // sandbox.stub(uut.bchjs.Util, 'sleep').resolves({}) - // - // const inputTest = 'test' - // - // const funcHandle = () => { - // throw new Error('test error') - // } - // uut.attempts = 1 - // await uut.retryWrapper(funcHandle, inputTest) - // assert.fail('unexpected code path') - // } catch (err) { - // assert.include(err.message, 'test error') - // } - // }) - // }) + describe('#addToQueue', () => { + it('should throw an error if function handler is not provided', async () => { + try { + await uut.addToQueue() - // describe('#handleValidationError', () => { - // it('should catch and throw an error', async () => { - // try { - // await uut.handleValidationError() - // assert.fail('unexpected code path') - // } catch (err) { - // console.log(err) - // assert.include(err.message, 'Cannot read property') - // } - // }) - // }) + assert.fail('unexpected code path') + } catch (err) { + assert.include(err.message, 'function handler is required') + } + }) + + it('should throw an error if input object is not provided', async () => { + try { + const funcHandler = () => {} + + await uut.addToQueue(funcHandler) + + assert.fail('unexpected code path') + } catch (err) { + assert.include(err.message, 'input object is required') + } + }) + + it('should add a function and input object to the queue and execute them', async () => { + const inputTest = 'test' + // func mock to execute into the retry wrapper + const funcHandle = sinon.spy() + + await uut.addToQueue(funcHandle, inputTest) + + assert.equal(inputTest, funcHandle.getCall(0).args[0]) + assert.equal(funcHandle.callCount, 1) + }) + + it('should catch and throw an error', async () => { + try { + // Mock for ignore sleep time + sandbox.stub(uut, 'sleep').resolves({}) + + const inputTest = 'test' + + const funcHandle = () => { + throw new Error('test error') + } + + uut.attempts = 1 + await uut.retryWrapper(funcHandle, inputTest) + + assert.fail('unexpected code path') + } catch (err) { + assert.include(err.message, 'test error') + } + }) + }) + + describe('#handleValidationError', () => { + it('should catch and throw an error', async () => { + try { + await uut.handleValidationError() + + assert.fail('unexpected code path') + } catch (err) { + // console.log(err) + assert.include(err.message, 'Cannot read property') + } + }) + }) })