From e691ede0cf2fc534fab8325e9750806ff1cc90ea Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Fri, 14 Jun 2019 10:07:15 -0400 Subject: [PATCH] Fix breakage in tests around db extension registration. --- .../src/TestUtils/MockSSKEnvironment.m | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m b/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m index 315e7bfae6..09294a8182 100644 --- a/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m +++ b/SignalServiceKit/src/TestUtils/MockSSKEnvironment.m @@ -131,11 +131,27 @@ NS_ASSUME_NONNULL_BEGIN - (void)configure { [self.databaseStorage clearGRDBStorageForTests]; + __block dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); [OWSStorage registerExtensionsWithMigrationBlock:^() { dispatch_semaphore_signal(semaphore); }]; - dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); + + // Registering extensions is a complicated process than can move + // on and off the main thread. While we wait for it to complete, + // we need to process the run loop so that the work on the main + // thread can be completed. + while (YES) { + // Wait up to 10 ms. + BOOL success + = dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_MSEC))) == 0; + if (success) { + break; + } + + // Process a single "source" (e.g. item) on the default run loop. + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.0, false); + } } @end