mirror of
https://github.com/fullstack-cash/fullstack-gatsby-theme-bch-wallet.git
synced 2026-09-22 17:22:05 -07:00
fix(retry-queue): Got unit tests passing
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user