Sunday, October 16, 2011

How to wait while an NSTimer runs in a unit test

Suppose you were a lowly noob to iOS development. Further suppose you had a Cool Idea (tm) which involved timers. The internets might rapidly guide you to NSTimer and you might decide to try to get it to log to the console in a unit test. The most obvious approach seems to be to setup a timer to tick frequently, lets say every 0.1 seconds, and setup a timer callback that logs something, then make a test that sleeps for a couple of seconds. Presumably during the sleep period we'll see a bunch of timer output. The code might look like this (inside an XCode 4.2 test implementation class):

- (void)onTimerTick:(NSTimer*)timer

- (void)testTimerBasics
    NSLog(@"timer time");
    [NSTimer scheduledTimerWithTimeInterval:0.1
    [timer fire]; //manually calling fire DOES log 'MY TIMER TICKED'
    NSLog(@"about to wait");    
    [NSThread sleepForTimeInterval:2.0]; //absolutely no logs of 'MY TIMER TICKED' occur; somehow the time doesn't fire during a thread sleep :(
    NSLog(@"wait time is over");    

Sadly absolutely no log messages are printed during our two second sleep ([NSThread sleepForTimeInterval:2.0]) ; WTF?!

After much Google and literally in the midst of typing a Stack Overflow question I came across a question involving waiting for something else that mentioned NSRunLoop in passing. The very existence of a run loop class suggests an answer: our tests run on the same thread as the run loop. This means if we put the run loop to sleep nothing gets processed. Instead of sleep we need some sort of "run the run loop for a while" approach. Luckily it turns out that NSRunLoop provides a runUntilDate API so we can re-write the test above as follows:

- (void)onTimerTick:(NSTimer*)timer

- (void)testTimerBasics
    NSLog(@"timer time");
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.1
    //[timer fire];
    NSDate *runUntil = [NSDate dateWithTimeIntervalSinceNow: 3.0 ];
    NSLog(@"about to wait");    
    [[NSRunLoop currentRunLoop] runUntilDate:runUntil];
    NSLog(@"wait time is over");    
We've found the right magic incantation! Knuth would be proud.

Speaking of magic incantations, I am using the SyntaxHighlighter libraries hosted @ However, there is no Objectionable-C brush there so I took the one posted @ and updated it the casing and namespace names to the newer highlighter standard. The updated brush looks like this: = function()
 var datatypes = 'char bool BOOL double float int long short id void';
 var keywords = 'IBAction IBOutlet SEL YES NO readwrite readonly nonatomic nil NULL ';
 keywords += 'super self copy ';
 keywords += 'break case catch class const copy __finally __exception __try ';
 keywords += 'const_cast continue private public protected __declspec ';
 keywords += 'default delete deprecated dllexport dllimport do dynamic_cast ';
 keywords += 'else enum explicit extern if for friend goto inline ';
 keywords += 'mutable naked namespace new noinline noreturn nothrow ';
 keywords += 'register reinterpret_cast return selectany ';
 keywords += 'sizeof static static_cast struct switch template this ';
 keywords += 'thread throw true false try typedef typeid typename union ';
 keywords += 'using uuid virtual volatile whcar_t while';
 // keywords += '@property @selector @interface @end @implementation @synthesize ';
 this.regexList = [
  { regex:,  css: 'comments' },  // one line comments
  { regex:,  css: 'comments' },  // multiline comments
  { regex:,  css: 'string' },   // double quoted strings
  { regex:,  css: 'string' },   // single quoted strings
  { regex: new RegExp('^ *#.*', 'gm'),      css: 'preprocessor' },  // preprocessor
  { regex: new RegExp(this.GetKeywords(datatypes), 'gm'),  css: 'datatypes' },  // datatypes
  { regex: new RegExp(this.GetKeywords(keywords), 'gm'),  css: 'keyword' },   // keyword
  { regex: new RegExp('\\bNS\\w+\\b', 'g'),     css: 'keyword' },   // keyword
  { regex: new RegExp('@\\w+\\b', 'g'),      css: 'keyword' },   // keyword
 this.CssClass = 'dp-objc';
 this.Style = '.dp-objc .datatypes { color: #2E8B57; font-weight: bold; }'; 
} = new;  = ['objc'];


Naviya Nair said...

I have read your blog its very attractive and impressive. I like it your blog.

Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

Priya Kannan said...

It's interesting that many of the bloggers to helped clarify a few things for me as well as giving.Most of ideas can be nice content.The people to give them a good shake to get your point and across the command .
IOS Training in Chennai

Swathi Samala said...

very valuable information ios Online Training Bangalore

Unknown said...

Really Good blog post.provided a helpful information.I hope that you will post more updates like this
ios Online Training Hyderabad

soukya reddy said...

The information which you have provided is very good. It is very useful who is looking for ios Online Training Bangalore

likitha said...

I have read your blog its very attractive and impressive. I like your blog. ios Online course Bangalore

akhila priya said...

I really enjoy the blog.Much thanks again. Really Great ios Online Course Bangalore

SRI said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

rpa Training in Chennai

rpa Training in bangalore

rpa Training in pune

blueprism Training in Chennai

blueprism Training in bangalore

blueprism Training in pune

rpa online training

digi mark said...

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.

automation anywhere training in chennai

automation anywhere training in bangalore

automation anywhere training in pune

automation anywhere online training

blueprism online training

rpa Training in sholinganallur

rpa Training in annanagar


shalinipriya said...

This is very good content you share on this blog. it's very informative and provide me future related information.
Data Science training in marathahalli
Data Science training in btm
Data Science training in rajaji nagar
Data Science training in chennai
Data Science training in kalyan nagar
Data Science training in electronic city
Data Science training in USA

simbu said...

This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.

java training in marathahalli | java training in btm layout

java training in jayanagar | java training in electronic city

java training in chennai | java training in USA

selenium training in chennai

pragya pragya said...

python training institute in chennai
python training in velachery
python training institute in chennai

shethal said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
Devops training in velachery
Devops training in annanagar
Devops training in sholinganallur
Devops training in tambaram

param jothi said...

Great content thanks for sharing this informative blog which provided me technical information keep posting.
Blueprism training in Chennai

Blueprism online training

Blue Prism Training in Pune

chitra pragya said...

I think you have a long story to share and i am glad after long time finally you cam and shared your experience.

angularjs Training

in chennai

angularjs Training in chennai

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

cynthia williams said...

I have to thank for sharing this blog, it is really helpful.
RPA Training in Chennai
Robotics Process Automation Training in Chennai
RPA courses in Chennai
RPA Training
RPA course

Dwarakesh babu said...

I’m planning to start my blog soon, but I’m a little lost on everything. Would you suggest starting with a free platform like Word Press or go for a paid option? There are so many choices out there that I’m completely confused. Any suggestions? Thanks a lot.
Best AWS Training in Marathahalli | AWS Training in Marathahalli
Amazon Web Services Training in Anna Nagar, Chennai |Best AWS Training in Anna Nagar, Chennai
AWS Training in Velachery | Best AWS Course in Velachery,Chennai
Best AWS Training in Chennai | AWS Training Institutes |Chennai,Velachery

Vikky cmd said...

I am really happy with your blog because your article is very unique and powerful for new reader.
Click here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training

mouni yoga said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
python training in rajajinagar | Python training in btm | Python training in usa

LindaJasmine said...

Interesting Post. I liked your style of writing. It is very unique. Thanks for Posting.

Node JS Training in Chennai
Node JS Course in Chennai
Node JS Advanced Training
Node JS Training Institute in chennai
Node JS Training Institutes in chennai
Node JS Course

Shiva Shakthi said...

The blog you had post is verymuch useful for us to know about the Web designing. thanks for your information sharing ith us.
Web Designing Institute in Coimbatore
Web Designing Course
Web Design Classes
Website Design Course
Learning Web Design

Sumaya Manzoor said...

All your points are excellent, keep doing great work
Selenium Training in Chennai
selenium testing training in chennai
iOS Training in Chennai
Digital Marketing Training in Chennai
android training center in chennai
best android training center in chennai

yuva prithika said...

This is really too useful and have more ideas and keep sharing many techniques. Eagerly waiting for your new blog keep doing more.
Aws Training in Bangalore
Aws Course in Bangalore
Best Aws Training in Bangalore
hadoop classes in bangalore
Java Training in Bangalore
Best Java Training Institutes in Bangalore

Vicky Ram said...

Thank you for sharing this post.

Article submission sites

Anbarasan14 said...

Thanks for sharing a worthy information. This is really helpful. Keep doing more.

English Speaking Course in JP Nagar Bangalore
Best Spoken English Coaching Center in JP Nagar
Spoken English Classes in Bangalore JP Nagar
French Training Institutes in JP Nagar
French Coaching Classes in JP Nagar
French Courses in JP Nagar
Best French Classes near me

sharmi chithra said...

Nice post. I learned some new information. Thanks for sharing.

Xamarin Training in Chennai
Xamarin Course in Chennai
Xamarin Training
Xamarin Course
Xamarin Training Course
Xamarin Classes
Best Xamarin Course

LindaJasmine said...

Thanks for sharing such an amazing post. Your style of writing is very unique. It made me mesmerized in your words. Keep on writing.

Informatica Training in Chennai
Informatica Training Center Chennai
Best Informatica Training in Chennai
Informatica course in Chennai
Informatica Training center in Chennai
Informatica Training
Learn Informatica
Informatica course

Sai Elakiyaa said...

Great explanation, your article inspired me the most. Thanks for sharing.
Selenium Training in Chennai
Best Selenium Training Institute in Chennai
ios developer training in chennai
Digital Marketing Training in Chennai
.Net coaching centre in chennai
Future of testing professional
Different functions in testing
Digital marketing Training institute in chennai

mercyroy said...

I really thank you for your innovative post.I have never read a creative ideas like your after i will follow your posts which is very much help for my career.
Java Certification course in Bangalore
Java Courses in Nolambur
Java Training in Ashok Nagar
Java Training in Kelambakkam

venu bharath said...

Awesome Post . Your way of expressing things makes reading very enjoyable. Thanks for posting.
Ethical Hacking Course in Chennai
Hacking Course in Chennai
Ethical Hacking Training in Chennai
Certified Ethical Hacking Course in Chennai
Ethical Hacking Course
Ethical Hacking Certification
IELTS coaching in Chennai
IELTS Training in Chennai

cynthia williams said...

I have to thank for sharing this blog, really helpful. Keep sharing more like this.
ReactJS Training in Chennai
AngularJS Training Institute in Chennai
AngularJS Training in Chennai
AWS Training in Chennai
DevOps Certification in Chennai
Robotics Process Automation Training in Chennai
R Programming Training in Chennai
Data Science Course in Chennai

Sadhana Rathore said...

Your site is amazing and your blogs are informative. I have bookmarked this blog for my reference.
Automation Anywhere Training in Chennai
Automation Training Institute in Chennai
Automation Anywhere course in Chennai
Blue Prism Training Institute in Chennai
Blue Prism Training in Chennai
RPA course in Chennai

tamilsasi said...

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.
devops online training

aws online training

data science with python online training

data science online training

rpa online training

Sai Bk said...

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.

Microsoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training

venu bharath said...

You are an excellent writer. Amazing use of words. Waiting for your future updates.
Blockchain certification
Blockchain course
Blockchain Training
Blockchain Training Chennai
Blockchain Training in T Nagar
Blockchain Training in OMR

Vicky Ram said...

Nice post. I learned some new information. Thanks for sharing.

Guest posting sites

suresh said...

Excellent Article. Thanks Admin
Hadoop Big Data Training

Python Training in Chennai

kamal said...

indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.

Sathya said...

Interesting information and attractive.This blog is really rocking... Yes, the post is very interesting and I really like it.I never seen articles like this. I meant it's so knowledgeable, informative, and good looking site. I appreciate your hard work. Good job.
Kindly visit us @
Sathya Online Shopping
Online AC Price | Air Conditioner Online | AC Offers Online | AC Online Shopping
Inverter AC | Best Inverter AC | Inverter Split AC
Buy Split AC Online | Best Split AC | Split AC Online
LED TV Sale | Buy LED TV Online | Smart LED TV | LED TV Price
Laptop Price | Laptops for Sale | Buy Laptop | Buy Laptop Online
Full HD TV Price | LED HD TV Price
Buy Ultra HD TV | Buy Ultra HD TV Online
Buy Mobile Online | Buy Smartphone Online in India

ajay prakash said...

Excellent post gained so much of information, Keep posting like this.
Aviation Courses in Chennai
Air hostess training in Chennai
Airline Courses in Chennai
airport ground staff training courses in Chennai
Aviation Academy in Chennai
air hostess training in Chennai
airport management courses in Chennai
ground staff training in Chennai

Post a Comment