aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSThread+BlocksTest.m
blob: 4b685fd4e8bf90bd3696fefb3cffd4380cdbddde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//
//  GTMNSThread+BlocksTest.m
//
//  Copyright 2012 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//

#import <pthread.h>
#import "GTMSenTestCase.h"
#import "GTMNSThread+Blocks.h"

#import "GTMFoundationUnitTestingUtilities.h"

@interface GTMNSThread_BlocksTest : GTMTestCase {
 @private
  GTMSimpleWorkerThread *workerThread_;
}
@end

@implementation GTMNSThread_BlocksTest

- (void)setUp {
  workerThread_ = [[GTMSimpleWorkerThread alloc] init];
  [workerThread_ start];
}

- (void)tearDown {
  [workerThread_ stop];
  [workerThread_ release];
}

- (void)testPerformBlockOnCurrentThread {
  NSThread *currentThread = [NSThread currentThread];

  GTMUnitTestingBooleanRunLoopContext *context =
      [GTMUnitTestingBooleanRunLoopContext context];
  __block NSThread *runThread = nil;

  // Straight block runs right away (no runloop spin)
  runThread = nil;
  [context setShouldStop:NO];
  [currentThread gtm_performBlock:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertEqualObjects(runThread, currentThread);
  XCTAssertTrue([context shouldStop]);

  // Block with waiting runs immediately as well.
  runThread = nil;
  [context setShouldStop:NO];
  [currentThread gtm_performWaitingUntilDone:YES block:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertEqualObjects(runThread, currentThread);
  XCTAssertTrue([context shouldStop]);

  // Block without waiting requires a runloop spin.
  runThread = nil;
  [context setShouldStop:NO];
  [currentThread gtm_performWaitingUntilDone:NO block:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertTrue([[NSRunLoop currentRunLoop]
                 gtm_runUpToSixtySecondsWithContext:context]);
  XCTAssertEqualObjects(runThread, currentThread);
  XCTAssertTrue([context shouldStop]);
}

- (void)testPerformBlockInBackground {
  GTMUnitTestingBooleanRunLoopContext *context =
      [GTMUnitTestingBooleanRunLoopContext context];
  __block NSThread *runThread = nil;
  [NSThread gtm_performBlockInBackground:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertTrue([[NSRunLoop currentRunLoop]
                 gtm_runUpToSixtySecondsWithContext:context]);
  XCTAssertNotNil(runThread);
  XCTAssertNotEqualObjects(runThread, [NSThread currentThread]);
}

- (void)testWorkerThreadBasics {
  // Unstarted worker isn't running.
  GTMSimpleWorkerThread *worker = [[GTMSimpleWorkerThread alloc] init];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertFalse([worker isFinished]);

  // Unstarted worker can be stopped without error.
  [worker stop];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertTrue([worker isFinished]);

  // And can be stopped again
  [worker stop];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertTrue([worker isFinished]);

  // A thread we start can be stopped with correct state.
  worker = [[GTMSimpleWorkerThread alloc] init];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertFalse([worker isFinished]);
  [worker start];
  XCTAssertTrue([worker isExecuting]);
  XCTAssertFalse([worker isFinished]);
  [worker stop];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertTrue([worker isFinished]);

  // A cancel is also honored
  worker = [[GTMSimpleWorkerThread alloc] init];
  XCTAssertFalse([worker isExecuting]);
  XCTAssertFalse([worker isFinished]);
  [worker start];
  XCTAssertTrue([worker isExecuting]);
  XCTAssertFalse([worker isFinished]);
  [worker cancel];
  // And after some time we're done. We're generous here, this needs to
  // exceed the worker thread's runloop timeout.
  sleep(5);
  XCTAssertFalse([worker isExecuting]);
  XCTAssertTrue([worker isFinished]);
}

- (void)testWorkerThreadStopTiming {
  // Throw a sleep and make sure that we stop as soon as we can.
  NSDate *start = [NSDate date];
  NSConditionLock *threadLock = [[[NSConditionLock alloc] initWithCondition:0]
                                    autorelease];
  [workerThread_ gtm_performBlock:^{
    [threadLock lock];
    [threadLock unlockWithCondition:1];
    [NSThread sleepForTimeInterval:.25];
  }];
  [threadLock lockWhenCondition:1];
  [threadLock unlock];
  [workerThread_ stop];
  XCTAssertFalse([workerThread_ isExecuting]);
  XCTAssertTrue([workerThread_ isFinished]);
  XCTAssertEqualWithAccuracy(-[start timeIntervalSinceNow], 0.25, 0.25);
}

- (void)testPerformBlockOnWorkerThread {
  GTMUnitTestingBooleanRunLoopContext *context =
      [GTMUnitTestingBooleanRunLoopContext context];
  __block NSThread *runThread = nil;

  // Runs on the other thread
  runThread = nil;
  [context setShouldStop:NO];
  [workerThread_ gtm_performBlock:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertTrue([[NSRunLoop currentRunLoop]
                 gtm_runUpToSixtySecondsWithContext:context]);
  XCTAssertNotNil(runThread);
  XCTAssertEqualObjects(runThread, workerThread_);

  // Other thread no wait.
  runThread = nil;
  [context setShouldStop:NO];
  [workerThread_ gtm_performWaitingUntilDone:NO block:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertTrue([[NSRunLoop currentRunLoop]
                 gtm_runUpToSixtySecondsWithContext:context]);
  XCTAssertNotNil(runThread);
  XCTAssertEqualObjects(runThread, workerThread_);

  // Waiting requires no runloop spin
  runThread = nil;
  [context setShouldStop:NO];
  [workerThread_ gtm_performWaitingUntilDone:YES block:^{
    runThread = [NSThread currentThread];
    [context setShouldStop:YES];
  }];
  XCTAssertTrue([context shouldStop]);
  XCTAssertNotNil(runThread);
  XCTAssertEqualObjects(runThread, workerThread_);
}

- (void)testExitingBlockIsExecuting {
  NSConditionLock *threadLock = [[[NSConditionLock alloc] initWithCondition:0]
                                    autorelease];
  [workerThread_ gtm_performWaitingUntilDone:NO block:^{
    [threadLock lock];
    [threadLock unlockWithCondition:1];
    pthread_exit(NULL);
  }];
  [threadLock lockWhenCondition:1];
  [threadLock unlock];
  // Give the pthread_exit() a bit of time
  [NSThread sleepForTimeInterval:.25];
  // Did we notice the thread died? Does [... isExecuting] clean up?
  XCTAssertFalse([workerThread_ isExecuting]);
  XCTAssertTrue([workerThread_ isFinished]);
}

- (void)testExitingBlockCancel {
  NSConditionLock *threadLock = [[[NSConditionLock alloc] initWithCondition:0]
                                    autorelease];
  [workerThread_ gtm_performWaitingUntilDone:NO block:^{
    [threadLock lock];
    [threadLock unlockWithCondition:1];
    pthread_exit(NULL);
  }];
  [threadLock lockWhenCondition:1];
  [threadLock unlock];
  // Give the pthread_exit() a bit of time
  [NSThread sleepForTimeInterval:.25];
  // Cancel/stop the thread
  [workerThread_ stop];
  // Did we notice the thread died? Did we clean up?
  XCTAssertFalse([workerThread_ isExecuting]);
  XCTAssertTrue([workerThread_ isFinished]);
}

- (void)testStopFromThread {
  NSConditionLock *threadLock = [[[NSConditionLock alloc] initWithCondition:0]
                                    autorelease];
  [workerThread_ gtm_performWaitingUntilDone:NO block:^{
    [threadLock lock];
    [workerThread_ stop];  // Shold not block.
    [threadLock unlockWithCondition:1];
  }];
  // Block should complete before the stop occurs.
  [threadLock lockWhenCondition:1];
  [threadLock unlock];
  // Still need to give the thread a moment to not be executing
  sleep(1);
  XCTAssertFalse([workerThread_ isExecuting]);
  XCTAssertTrue([workerThread_ isFinished]);
}

- (void)testPThreadName {
  NSString *testName = @"InigoMontoya";
  [workerThread_ setName:testName];
  [workerThread_ gtm_performWaitingUntilDone:NO block:^{
    XCTAssertEqualObjects([workerThread_ name], testName);
    char threadName[100];
    pthread_getname_np(pthread_self(), threadName, 100);
    XCTAssertEqualObjects([NSString stringWithUTF8String:threadName], testName);
  }];
}

@end