Pattern for unit testing async queue that calls main queue on completion

后端 未结 6 526
慢半拍i
慢半拍i 2020-12-04 14:52

This is related to my previous question, but different enough that I figured I\'d throw it into a new one. I have some code that runs async on a custom queue, then executes

6条回答
  •  醉话见心
    2020-12-04 15:03

    An alternate method, using semaphores and runloop churning. Note that dispatch_semaphore_wait returns nonzero if it times out.

    - (void)testFetchSources
    {
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
        [MyObject doSomethingAsynchronousWhenDone:^(BOOL success) {
            STAssertTrue(success, @"Failed to do the thing!");
            dispatch_semaphore_signal(semaphore);
        }];
    
        while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
                                     beforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    
        dispatch_release(semaphore);
    }
    

提交回复
热议问题