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