Block of unit test

Source: Internet
Author: User

Some time ago, I wrote a factory test program and encountered a small problem. When I sent a test command, the callback function was a block and the block was processed asynchronously. Results cannot be returned in a timely manner.

After checking the information, I handled it like this.

#define TestNeedsToWaitForBlock() __block BOOL blockFinished = NO#define BlockFinished() blockFinished = YES#define WaitForBlock() while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, true) && !blockFinished)

  

-(Void) setandgetstepgoal :( INT) goal {nslog (@ "test setting motion target value: % d", goal); {testneedstowaitforblock (); [commandinterface setsteps: goal completion: ^ (bool success, nsdictionary * info) {blockfinished ();}]; waitforblock () ;}{ testneedstowaitforblock (); [commandinterface getgoal: ^ (bool success, nsdictionary * info) {int devicegoal = [info [@ "Steps"] intvalue]; nslog (@ "set motion target value result: % d >>>>>>> % d", goal, devicegoal); xctassertequal (goal, devicegoal, @ "goal does not match expectations"); blockfinished () ;}]; waitforblock ();}}

The principle is actually very simple. Use cfrunloopruninmode to stop the thread from exiting and wait for the system message.

Block of unit test

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.