Friday, July 26, 2013

Asynchronous code testing in Android

Testing asynchronous calls it is not always an easy task.

Let's take as reference this simple interface:


The first thing is: how can we test it through an Android unit test class?


We can't test the value inside the callback. Infact, an async call runs usually in worker thread. When the thread has done some computation, it calls the callback. The simplest case is when the callback is run in the same worker thread.

But still, we need to find a way to block the main thread, otherwise the test class will simply don't wait before the callback is called! Remember that the method testDoSync() is called in the main thread.

We can use a semaphore, and make the test method wait until the callback is delivered. Then we can easily test the return value. The Semaphore class has some interesting properties: first, we can use it to put a thread in wait; the acquire() method stops the thread until a resource is available. As we have created the Semaphore with 0 resources available, this will cause the next call to acquire() to stop the thread.
But what if there is the unlikely case that the listener, returns before the acquire() call is made? No worries, as the signal() will simply increment the number of resources available and so the acquire() method will not nlock but will return straight away. Yay, this is for sure a better way to handle threads synchronization that the usual pattern wait-notify!
This is the code that covers this case:


And now, the worst case. Let's imagine that the code run by the worker thread do some stuff, and then it posts the results of its work in the calling thread (for instance through a handler). Then, onValueChanged() will never be called. Why? It is because we needed to stop the main thread. The main thread is a looper thread: normally a thread do some stuff and then it dies. Instead, a thread with a looper processes events by looping continuously. Another thread can post events on it: the events are put in the thread queue and they are processed one at time. If we block the main thread, the event posted by the worker thread will NEVER get processed.
How to handle this case?
We still need to block the main thread. But we need to launch the callback from a secondary thread with the looper.
We will use a facility offered by android: HandlerThread.

It will create a thread with looper for us; then the callback will be called in the secondary thread looper that is not blocked, and from that thread we can release the semaphore of the main thread.
Finally, remember to quit() the thread handler (that will quit the embedded looper too and will cause the death of that thread).

81 comments:

  1. Nice blog having great concept about Android Development Thanks for sharing with us.

    ReplyDelete
  2. Thanks for your informative article on ios mobile application development. Your article helped me to explore the future of mobile apps developers. Having sound knowledge on mobile application development will help you to float in mobile application development. iOS Training | iOS Training Institutes in Chennai

    ReplyDelete
  3. The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..Website Development Bangalore | Web Design Company Bangalore

    ReplyDelete
  4. Hey You may like iphone app development company from Bangalore for any types of mobile apps......

    ReplyDelete
  5. A nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.

    iOS Training in Chennai

    ReplyDelete
  6. your information is really awesome as well as it is very excellent and i got more interesting information from your blog.iOS Training in Chennai

    ReplyDelete
  7. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.If you want to apply converted studying for Hadoop Real-Time Training and Live High-Level Knowledge instructor Trained from Hadoop Programme Training Course to reach us
    Hadoop Training in Chennai
    Hadoop Training With Placement in Chennai

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. Wedding makeup and hair burlington Ontario

    ReplyDelete
  11. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks; from every one of us.

    Best Java Training Institute Chennai


    ReplyDelete
  12. Very well written blog and I always love to read blogs like these because they offer very good information to readers with very less amount of words....thanks for sharing your info with us and keep sharing.

    Hadoop Training in Chennai
    Hadoop Training in Bangalore
    Big data training in tambaram

    ReplyDelete
  13. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    python training in chennai | python training in bangalore

    python online training | python training in pune

    python training in chennai | python training in bangalore

    python training in tambaram | python training in velachery

    ReplyDelete
  14. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
    Devops training in tambaram
    Devops training in velachery
    Devops training in annanagar
    DevOps online Training

    Devops Training in Chennai

    Devops Training in Bangalore

    Devops training in sholinganallur

    ReplyDelete
  15. It was worth visiting your blog and I have bookmarked your blog. Hope to visit again
    python training in tambaram
    python training in annanagar
    python training in OMR

    ReplyDelete
  16. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    java training in omr | oracle training in chennai

    java training in annanagar | java training in chennai

    ReplyDelete
  17. Pleasant Tips..Thanks for Sharing….We keep up hands on approach at work and in the workplace, keeping our business pragmatic, which recommends we can help you with your tree clearing and pruning in an invaluable and fit way.
    python training in annanagar
    python training in chennai
    python training in chennai
    python training in Bangalore

    ReplyDelete
  18. Hi,

    Thanks for sharing a very interesting article about Asynchronous code testing in Android. This is very useful information for online blog review readers. Keep it up such a nice posting like this.

    From,
    WondersMind,
    Web Development Company Bangalore

    ReplyDelete
  19. Very nice post here and thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    Good discussion. Thank you.
    Anexas
    Six Sigma Training in Abu Dhabi
    Six Sigma Training in Dammam
    Six Sigma Training in Riyadh

    ReplyDelete
  20. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
    Data science training in tambaram | Data Science training in anna nagar
    Data Science training in chennai | Data science training in Bangalore
    Data Science training in marathahalli | Data Science training in btm

    ReplyDelete
  21. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Devops training in sholinganallur
    Devops training in velachery

    ReplyDelete
  22. I’m experiencing some small security issues with my latest blog, and I’d like to find something safer. Do you have any suggestions?
    industrial course in chennai

    ReplyDelete
  23. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.angularjs-Training in tambaram

    angularjs-Training in sholinganallur

    angularjs-Training in velachery

    angularjs Training in bangalore

    angularjs Training in bangalore

    ReplyDelete
  24. Fantastic work! This is the type of information that should follow collective approximately the web. Embarrassment captivating position Google for not positioning this transmit higher! Enlarge taking place greater than and visit my web situate
    Data Science training in rajaji nagar | Data Science Training in Bangalore | Data Science with Python training in chennai

    Data Science training in electronic city | Data Science training in USA

    Data science training in pune | Data science training in kalyan nagar


    ReplyDelete
  25. Thanks for the good words! Really appreciated. Great post. I’ve been commenting a lot on a few blogs recently, but I hadn’t thought about my approach until you brought it up. 
    python training institute in marathahalli | python training institute in btm

    ReplyDelete
  26. This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
    python training institute in marathahalli | python training institute in btm | Data Science training in Chennai

    ReplyDelete
  27. You truly did more than visitors’ expectations. Thank you for rendering these helpful, trusted, edifying and also cool thoughts on the topic to Kate.
    best safety training in chennai

    ReplyDelete
  28. A lot of new things learnt from this post.thanks for sharing the information.keep posting on new things.
    aws online training
    aws training in hyderabad
    amazon web services(AWS) online training

    ReplyDelete
  29. Hello! This is my first visit to When I initially commented, I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added
    industrial course in chennai

    ReplyDelete

  30. The author has made such a great effort in unveiling these nice guidelines and I hope that they will assist readers.It is most knowledgeable information like this

    Android Training
    Android Training in Chennai


    ReplyDelete
  31. Really you have done great job,There are may person searching about that now they will find enough resources by your post

    python training institute in marathahalli
    python training institute in btm
    Python training course in Chennai

    ReplyDelete
  32. I really like the dear information you offer in your articles. I’m able to bookmark your site and show the kids check out up here generally. Im fairly positive theyre likely to be informed a great deal of new stuff here than anyone
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete
  33. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.
    iosh safety course in chennai

    ReplyDelete
  34. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage
    contribution from other ones on this subject while our own child is truly discovering a great deal.
    Have fun with the remaining portion of the year.

    Selenium training in bangalore | best selenium training in bangalore

    ReplyDelete




  35. Thanks For Sharing Your information Please keep updating us The Information Shared Is Very valuable Time Just Went On redaing The article Python Online Training Data Science Online Training Aws Online Training Hadoop Online Training

    ReplyDelete
  36. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    angularjs online training

    apache spark online training

    informatica mdm online training

    devops online training

    aws online training

    ReplyDelete
  37. Great Post,really it was very helpful for us.
    Thanks a lot for sharing!
    I found this blog to be very useful!!
    JAVA training in Bangalore

    ReplyDelete
  38. I have to search sites with relevant information on given topic and provide Digital marketing in pune them to teacher our opinion and the article.

    ReplyDelete
  39. The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. It is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. Machine Learning Training in Bangalore

    ReplyDelete
  40. Hats off to your presence of mind...I really enjoyed reading your blog. I really appreciate your information which you shared with us.his is very good content you share on this blog. it's very informative and provide me future related information.
    Data Science Training In Chennai

    Data Science Online Training In Chennai

    Data Science Training In Bangalore

    Data Science Training In Hyderabad

    Data Science Training In Coimbatore

    Data Science Training

    Data Science Online Training

    ReplyDelete
  41. interesting piece of information, I had come to know about your web-page from my friend, i have read atleast eight posts of yours by now, and let me tell you, your blog gives the best and the most interesting information.
    Salesforce Training in Chennai

    Salesforce Online Training in Chennai

    Salesforce Training in Bangalore

    Salesforce Training in Hyderabad

    Salesforce training in ameerpet

    Salesforce Training in Pune

    Salesforce Online Training

    Salesforce Training

    ReplyDelete
  42. I am very happy to read this. This is the kind of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best posting.

    AI Training in Hyderabad

    ReplyDelete
  43. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. data science course in kanpur

    ReplyDelete
  44. Great informative tips to explore such brain exercises. This blog contains such helpful content that everyone can understand very easily. Asan Bazaar

    ReplyDelete
  45. Between describe board recently return. Deep stage ever half can.

    ReplyDelete
  46. This comment has been removed by the author.

    ReplyDelete
  47. Wonderful information, thanks a lot for sharing kind of information. Your website gives the best and the most interesting information. Thanks!!
    Python Full Stack course in Kukatpally

    ReplyDelete