32
32
33
33
extern BOOL getGULLoggerDebugMode (void );
34
34
35
- extern NSUserDefaults *getGULLoggerUsetDefaults (void );
35
+ extern CFStringRef getGULLoggerUsetDefaultsSuiteName (void );
36
+ extern dispatch_queue_t getGULLoggerCounterQueue (void );
36
37
37
38
static NSString *const kMessageCode = @" I-COR000001" ;
38
39
39
40
@interface GULLoggerTest : XCTestCase
40
41
41
42
@property (nonatomic ) NSString *randomLogString;
42
-
43
- @property (nonatomic , strong ) NSUserDefaults *defaults;
43
+ @property (nonatomic ) NSUserDefaults *loggerDefaults;
44
44
45
45
@end
46
46
@@ -50,14 +50,18 @@ - (void)setUp {
50
50
[super setUp ];
51
51
GULResetLogger ();
52
52
53
- // Stub NSUserDefaults for cleaner testing.
54
- _defaults = [[ NSUserDefaults alloc ] initWithSuiteName: @" com.google.logger_test " ];
53
+ self. loggerDefaults = [[ NSUserDefaults alloc ]
54
+ initWithSuiteName: CFBridgingRelease ( getGULLoggerUsetDefaultsSuiteName ()) ];
55
55
}
56
56
57
57
- (void )tearDown {
58
- [super tearDown ];
58
+ // Make sure all async operations have finished before starting a new test.
59
+ [self drainQueue: getGULClientQueue ()];
60
+ [self drainQueue: getGULLoggerCounterQueue ()];
59
61
60
- _defaults = nil ;
62
+ self.loggerDefaults = nil ;
63
+
64
+ [super tearDown ];
61
65
}
62
66
63
67
- (void )testMessageCodeFormat {
@@ -158,93 +162,58 @@ - (void)testGULLoggerLevelValues {
158
162
- (void )testGetErrorWarningNumberBeforeLogDontCrash {
159
163
GULResetLogger ();
160
164
161
- XCTestExpectation *getErrorCountExpectation =
162
- [self expectationWithDescription: @" getErrorCountExpectation" ];
163
- XCTestExpectation *getWarningsCountExpectation =
164
- [self expectationWithDescription: @" getWarningsCountExpectation" ];
165
-
166
- GULNumberOfErrorsLogged (^(NSInteger count) {
167
- [getErrorCountExpectation fulfill ];
168
- });
169
-
170
- GULNumberOfWarningsLogged (^(NSInteger count) {
171
- [getWarningsCountExpectation fulfill ];
172
- });
173
-
174
- [self waitForExpectations: @[ getErrorCountExpectation, getWarningsCountExpectation ]
175
- timeout: 0.5
176
- enforceOrder: YES ];
165
+ XCTAssertNoThrow (GULNumberOfErrorsLogged ());
166
+ XCTAssertNoThrow (GULNumberOfWarningsLogged ());
177
167
}
178
168
179
169
- (void )testErrorNumberIncrement {
180
- [getGULLoggerUsetDefaults () setInteger: 10 forKey: kGULLoggerErrorCountKey ];
170
+ [self .loggerDefaults setInteger: 10 forKey: kGULLoggerErrorCountKey ];
181
171
182
172
GULLogError (@" my service" , NO , kMessageCode , @" Message." );
183
173
184
- XCTestExpectation *getErrorCountExpectation =
185
- [self expectationWithDescription: @" getErrorCountExpectation" ];
186
-
187
- GULNumberOfErrorsLogged (^(NSInteger count) {
188
- [getErrorCountExpectation fulfill ];
189
- XCTAssertEqual (count, 11 );
190
- });
191
-
192
- [self waitForExpectationsWithTimeout: 0.5 handler: NULL ];
174
+ [self drainQueue: getGULLoggerCounterQueue ()];
175
+ XCTAssertEqual (GULNumberOfErrorsLogged (), 11 );
193
176
}
194
177
195
178
- (void )testWarningNumberIncrement {
196
- [getGULLoggerUsetDefaults () setInteger: 5 forKey: kGULLoggerWarningCountKey ];
179
+ [self .loggerDefaults setInteger: 5 forKey: kGULLoggerWarningCountKey ];
197
180
198
181
GULLogWarning (@" my service" , NO , kMessageCode , @" Message." );
199
182
200
- XCTestExpectation *getWarningsCountExpectation =
201
- [self expectationWithDescription: @" getWarningsCountExpectation" ];
202
-
203
- GULNumberOfWarningsLogged (^(NSInteger count) {
204
- [getWarningsCountExpectation fulfill ];
205
- XCTAssertEqual (count, 6 );
206
- });
207
-
208
- [self waitForExpectationsWithTimeout: 0.5 handler: NULL ];
183
+ [self drainQueue: getGULLoggerCounterQueue ()];
184
+ XCTAssertEqual (GULNumberOfWarningsLogged (), 6 );
209
185
}
210
186
211
187
- (void )testResetIssuesCount {
212
- [getGULLoggerUsetDefaults () setInteger: 3 forKey: kGULLoggerErrorCountKey ];
213
- [getGULLoggerUsetDefaults () setInteger: 4 forKey: kGULLoggerWarningCountKey ];
188
+ [self .loggerDefaults setInteger: 3 forKey: kGULLoggerErrorCountKey ];
189
+ [self .loggerDefaults setInteger: 4 forKey: kGULLoggerWarningCountKey ];
214
190
215
191
GULResetNumberOfIssuesLogged ();
216
192
217
- XCTestExpectation *getErrorCountExpectation =
218
- [self expectationWithDescription: @" getErrorCountExpectation" ];
219
- XCTestExpectation *getWarningsCountExpectation =
220
- [self expectationWithDescription: @" getWarningsCountExpectation" ];
221
-
222
- GULNumberOfErrorsLogged (^(NSInteger count) {
223
- [getErrorCountExpectation fulfill ];
224
- XCTAssertEqual (count, 0 );
225
- });
226
-
227
- GULNumberOfWarningsLogged (^(NSInteger count) {
228
- [getWarningsCountExpectation fulfill ];
229
- XCTAssertEqual (count, 0 );
230
- });
193
+ XCTAssertEqual (GULNumberOfErrorsLogged (), 0 );
194
+ XCTAssertEqual (GULNumberOfWarningsLogged (), 0 );
195
+ }
231
196
232
- [self waitForExpectations: @[ getErrorCountExpectation, getWarningsCountExpectation ]
233
- timeout: 0.5
234
- enforceOrder: YES ];
197
+ - (void )testNumberOfIssuesLoggedNoDeadlock {
198
+ [self dispatchSyncNestedDispatchCount: 100
199
+ queue: getGULLoggerCounterQueue ()
200
+ block: ^{
201
+ XCTAssertNoThrow (GULNumberOfErrorsLogged ());
202
+ XCTAssertNoThrow (GULNumberOfWarningsLogged ());
203
+ }];
235
204
}
236
205
237
206
// Helper functions.
238
207
- (BOOL )logExists {
239
- [self drainGULClientQueue ];
208
+ [self drainQueue: getGULClientQueue () ];
240
209
NSString *correctMsg =
241
210
[NSString stringWithFormat: @" %@ [%@ ] %@ " , @" my service" , kMessageCode , self .randomLogString];
242
211
return [self messageWasLogged: correctMsg];
243
212
}
244
213
245
- - (void )drainGULClientQueue {
214
+ - (void )drainQueue : ( dispatch_queue_t ) queue {
246
215
dispatch_semaphore_t workerSemaphore = dispatch_semaphore_create (0 );
247
- dispatch_async ( getGULClientQueue () , ^{
216
+ dispatch_barrier_async (queue , ^{
248
217
dispatch_semaphore_signal (workerSemaphore);
249
218
});
250
219
dispatch_semaphore_wait (workerSemaphore, DISPATCH_TIME_FOREVER);
@@ -272,5 +241,19 @@ - (BOOL)messageWasLogged:(NSString *)message {
272
241
#pragma clang pop
273
242
}
274
243
244
+ - (void )dispatchSyncNestedDispatchCount : (NSInteger )count
245
+ queue : (dispatch_queue_t )queue
246
+ block : (dispatch_block_t )block {
247
+ if (count < 0 ) {
248
+ return ;
249
+ }
250
+
251
+ dispatch_sync (queue, ^{
252
+ [self dispatchSyncNestedDispatchCount: count - 1 queue: queue block: block];
253
+ block ();
254
+ NSLog (@" %@ , depth: %ld " , NSStringFromSelector (_cmd), (long )count);
255
+ });
256
+ }
257
+
275
258
@end
276
259
#endif
0 commit comments