Every so often, you run across some action, which just fails, where the best response it to just try it again. This is particularly true when dealing with an external source, like a database or web service, which can have network or other temporary problems, which would have cleared up when you repeat the call seconds later.
Often, these actions fail by throwing an exception which makes the process of trying the call again rather cumbersome. Having to deal with this a number of times in one application, I decided to wrap the full procedure up in an angular service, which I share here with you:
Sleep function
First we need to implement a sleep
function, that will just wait for a specified interval of milliseconds while returning a promise.
You would think that the best fit is to use $timeout
.
The problem is, it has a bad impact on your unit tests….
The reason is, if you use $timeout
, you will have at the end of your test to call $timeout.flush()
as many times as $timeout
was called in your implementation.
Instead, if we use the standard javascript setTimeout
function, we only have to call once $rootScope.$apply()
at the end of our test and we’re done.
Let’s start by defining our service with an empty implementation:
In addition to sleep
, we will expose 2 other functions toAsync
and retry
, that we’ll explain later.
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 |
|
For the implementation of sleep
we are going to generate our own promise, call setTimeout
, and not forget to wrap the callback in a $rootScope.$apply()
call.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Before we continue, let’s write some unit tests to verify the implementation. I’m using here jasmine 2.0 which comes with a new syntax for async testing.
We have 2 options here:
First we can use the new async syntax and It’s pretty simple : when testing an async function (aka a promise), you add a done
parameter to your it
, and you call done()
when the promise returns some result.
In addition, if your implementation uses setTimeout, you add a call to $rootScope.$apply()
. The caveat of this method is that the test will wait for the timeout to callback. So it is more an end to end test than an unit test.
The second method, is to use the clock mockup provided by jasmine, that executes synchronously any code using setTimeout
or setInterval
.
It goes like this:
- call
jasmine.clock().install()
in thebeforeEach
- call
jasmine.clock().tick(xxx)
in the test passing the time in milliseconds that should have elapsed - call
jasmine.clock().uninstall()
in theafterEach
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 43 44 45 46 47 48 49 50 51 52 53 54 |
|
We have a fully tested sleep
function.
toAsync function
The next function that we need to write is a function that will transform a passed function into a promise.
That way we can retry on fail, either standard functions, or promises.
The trick here, if you want your unit test to pass, is to encapsulate the call to the passed function in a try catch block.
It is similar to $q.when
, but instead of passing a value or a promise, we pass a function or a promise.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
And the unit tests :
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
The mockHelper
object holds our unit test functions, so we can reuse and spy on them across the tests.
This is usefull for checking how many times they are called and with which parameters.
retry function
This is the last piece of the puzzle. To be as generic as possible, our function will accept a set of parameters in order to control how many times we should retry on fail, the interval between each trial, and also an interval multiplicator if we want to add some extra delay between each trial.
The implementation is straight forward, using the building blocks we previously wrote.
In the first part we just do some argument checking, and assign default values.
In the second part, we recursivly call resolver, which, execute the passed action, and if an expection is detected, do a sleep
and retry.
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 |
|
Here are the unit tests:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
That’s it. Our service is fully unit tested (100% coverage!!!), and we can reuse it anywhere in our applications.
The repository is available here.
Let me know what you think and happy coding.
Avi Haiat