From 17aaac660353e33625d2c9aa7cd29b0d14949795 Mon Sep 17 00:00:00 2001 From: dmaclach Date: Mon, 19 Nov 2018 08:31:27 -0800 Subject: Fix up race between NSThread setName and pthread_getname on the thread. (#213) --- Foundation/GTMNSThread+BlocksTest.m | 27 ++++++++++++++++++++++++--- 1 file 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 -- cgit v1.2.3