March 3, 2015 · Programming Testing

Callback hell! Don't blame the test code...

I discovered a bug today! Incredible. Jenkins sent me that wonderful e-mail to tell me that our app was failing some test cases.

I could not figure out the reason for the failure.... Mocha was telling me it timed out after 20000 ms. I initially thought the reason could be a missing done() function in my test code, but after several verifications; Nothing!

I later realize I was thinking about it all wrong. I wrote these test cases according to the specifications of Mocha. How could I be wrong? I then realized that the build only started failing after a merge done to include a new feature. That feature had a specific function that was missing a return callback. So it was something like this:

f1 = (d1, d2, callback)->
        #wtv code "operation done."

Thats it! There was no callback(null, data).

The fix was quite simple. Just add callback(null, data) at the end and the timeout dissappeared. See below:

f1 = (d1, d2, callback)->
        #wtv code "operation done."
        callback(null, data) #solves it.

Async code is a different paradigm and sometimes this can lead to simple mistakes. Async in the backend is starting to become a norm, so I highly doubt people will be making mistakes like this again.

So the lesson is; Trust your test code and don't always adjust your test code to change the test result. Also, callbacks need to be traced.... otherwise you might get silly or illogical outputs from your application.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket
Comments powered by Disqus