aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar dmaclach <dmaclach@gmail.com>2018-11-19 08:31:27 -0800
committerGravatar GitHub <noreply@github.com>2018-11-19 08:31:27 -0800
commit17aaac660353e33625d2c9aa7cd29b0d14949795 (patch)
treedb175310f1a009fadfaee56df067606a09023c1c
parent24bcc705890de306fd75b611bfda96500ffbd6f1 (diff)
Fix up race between NSThread setName and pthread_getname on the thread. (#213)
-rw-r--r--Foundation/GTMNSThread+BlocksTest.m27
1 files changed, 24 insertions, 3 deletions
diff --git a/Foundation/GTMNSThread+BlocksTest.m b/Foundation/GTMNSThread+BlocksTest.m
index b7e0cdb..e051de9 100644
--- a/Foundation/GTMNSThread+BlocksTest.m
+++ b/Foundation/GTMNSThread+BlocksTest.m
@@ -316,12 +316,33 @@ static const int kThreadMethoduSleep = 10000;
- (void)testPThreadName {
NSString *testName = @"InigoMontoya";
[workerThread_ setName:testName];
+ // There is actually a race between setting the thread name in NSThread and it
+ // being visible in the thread itself. A thread's name can only be set from
+ // inside the thread (pthread_setname) so we are assuming that NSThread has
+ // some internal wiring to kick the runloop and update the value for the
+ // thread on the thread itself.
+ // If we run our "check" block too soon, we will check the value on the
+ // thread before it's been set.
+ // Therefore we loop over it with some sleep to give the thread a chance
+ // to update it's internals.
[workerThread_ gtm_performWaitingUntilDone:YES block:^{
XCTAssertEqualObjects([workerThread_ name], testName);
- char threadName[100];
- pthread_getname_np(pthread_self(), threadName, 100);
- XCTAssertEqualObjects([NSString stringWithUTF8String:threadName], testName);
}];
+ __block BOOL finished = NO;
+ for (int i = 0; i < 100; i++) {
+ [workerThread_ gtm_performWaitingUntilDone:YES block:^{
+ char threadName[100];
+ pthread_getname_np(pthread_self(), threadName, 100);
+ NSString *nsThreadName = [NSString stringWithUTF8String:threadName];
+ finished = ([testName compare:nsThreadName] == NSOrderedSame);
+ }];
+ if (finished) {
+ break;
+ } else {
+ usleep(kThreadMethoduSleep);
+ }
+ }
+ XCTAssertTrue(finished, @"pthread name did not change to %@", testName);
}
@end