@@ -52,10 +52,35 @@ - (void) setUp
52
52
53
53
[NRLogger clearLog ];
54
54
55
+ // Open a file descriptor for the file
56
+ self.fileDescriptor = open ([[NRLogger logFilePath ] fileSystemRepresentation ], O_EVTONLY);
57
+ if (self.fileDescriptor < 0 ) {
58
+ XCTFail (@" Failed to open file descriptor" );
59
+ return ;
60
+ }
61
+
62
+ // Set up dispatch source for file monitoring
63
+ self.source = dispatch_source_create (DISPATCH_SOURCE_TYPE_VNODE, self.fileDescriptor , DISPATCH_VNODE_WRITE, DISPATCH_TARGET_QUEUE_DEFAULT);
64
+
65
+ __weak typeof (self) weakSelf = self;
66
+ dispatch_source_set_cancel_handler (self.source , ^{
67
+ if (weakSelf.fileDescriptor ) {
68
+ close (weakSelf.fileDescriptor );
69
+ weakSelf.fileDescriptor = 0 ;
70
+ }
71
+ });
72
+
55
73
56
74
}
57
75
- (void ) tearDown
58
76
{
77
+ if (self.fileDescriptor > 0 ) {
78
+ close (self.fileDescriptor );
79
+ }
80
+ if (self.source ) {
81
+ dispatch_source_cancel (self.source );
82
+ }
83
+
59
84
[NRMAMeasurements removeMeasurementConsumer: helper];
60
85
helper = nil ;
61
86
@@ -68,18 +93,6 @@ - (void) tearDown
68
93
69
94
- (void ) testNRLogger {
70
95
71
- XCTestExpectation *delayExpectation1 = [self expectationWithDescription: @" Waiting for Log Queue" ];
72
-
73
- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
74
- [delayExpectation1 fulfill ];
75
- });
76
-
77
- [self waitForExpectationsWithTimeout: 5 handler: ^(NSError * _Nullable error) {
78
- if (error) {
79
- XCTFail (@" Timeout error" );
80
- }
81
- }];
82
-
83
96
[NewRelic logInfo: @" Info Log..." ];
84
97
[NewRelic logError: @" Error Log..." ];
85
98
[NewRelic logVerbose: @" Verbose Log..." ];
@@ -93,20 +106,9 @@ - (void) testNRLogger {
93
106
@" additionalAttribute2" : @" attribute2"
94
107
}];
95
108
96
- XCTestExpectation *delayExpectation2 = [self expectationWithDescription: @" Waiting for Log Queue" ];
97
-
98
- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
99
- [delayExpectation2 fulfill ];
100
- });
109
+ sleep (5 );
101
110
102
- [self waitForExpectationsWithTimeout: 5 handler: ^(NSError * _Nullable error) {
103
- if (error) {
104
- XCTFail (@" Timeout error" );
105
- }
106
- }];
107
-
108
111
NSError * error;
109
- NSString *path = [NRLogger logFilePath ];
110
112
NSData * logData = [NRLogger logFileData: &error];
111
113
if (error){
112
114
NSLog (@" %@ " , error.localizedDescription );
@@ -173,19 +175,21 @@ - (void) testRemoteLogLevels {
173
175
174
176
// Set the remote log level to Debug.
175
177
[NRLogger setRemoteLogLevel: NRLogLevelDebug];
176
-
177
- XCTestExpectation *delayExpectation1 = [self expectationWithDescription: @" Waiting for Log Queue" ];
178
-
179
- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
180
- [delayExpectation1 fulfill ];
181
- });
182
-
183
- [self waitForExpectationsWithTimeout: 5 handler: ^(NSError * _Nullable error) {
184
- if (error) {
185
- XCTFail (@" Timeout error" );
178
+
179
+ __block BOOL operationCompleted = NO ;
180
+ __block int count = 0 ;
181
+ dispatch_source_set_event_handler (self.source , ^{
182
+ count++;
183
+ if (count == 7 ){
184
+ // Fulfill the expectation when a write is detected
185
+ sleep (1 );
186
+ operationCompleted = YES ;
186
187
}
187
- }];
188
-
188
+ });
189
+
190
+ // Start monitoring
191
+ dispatch_resume (self.source );
192
+
189
193
// Seven messages should reach the remote log file for upload.
190
194
191
195
[NewRelic logInfo: @" Info Log..." ];
@@ -200,18 +204,19 @@ - (void) testRemoteLogLevels {
200
204
@" additionalAttribute1" : @" attribute1" ,
201
205
@" additionalAttribute2" : @" attribute2"
202
206
}];
203
-
204
- XCTestExpectation *delayExpectation2 = [self expectationWithDescription: @" Waiting for Log Queue" ];
205
-
206
- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
207
- [delayExpectation2 fulfill ];
208
- });
209
-
210
- [self waitForExpectationsWithTimeout: 5 handler: ^(NSError * _Nullable error) {
211
- if (error) {
212
- XCTFail (@" Timeout error" );
213
- }
214
- }];
207
+
208
+ // Set a timeout duration
209
+ NSTimeInterval timeout = 30.0 ;
210
+ NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow: timeout];
211
+
212
+ // Run the run loop until the operation completes or the timeout is reached
213
+ while (!operationCompleted && [timeoutDate timeIntervalSinceNow ] > 0 ) {
214
+ // Allow other scheduled run loop activities to proceed
215
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1 ]];
216
+ }
217
+ if (!operationCompleted) {
218
+ NSLog (@" Failed to detect 7 writes to the log file." );
219
+ }
215
220
216
221
NSError * error;
217
222
NSData * logData = [NRLogger logFileData: &error];
@@ -264,20 +269,21 @@ - (void) testLocalLogLevels {
264
269
// Set the remote log level to Info.
265
270
[NRLogger setRemoteLogLevel: NRLogLevelInfo];
266
271
267
- XCTestExpectation *delayExpectation1 = [self expectationWithDescription: @" Waiting for Log Queue" ];
268
-
269
- dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
270
- [delayExpectation1 fulfill ];
271
- });
272
-
273
- [self waitForExpectationsWithTimeout: 5 handler: ^(NSError * _Nullable error) {
274
- if (error) {
275
- XCTFail (@" Timeout error" );
272
+ __block BOOL operationCompleted = NO ;
273
+ __block int count = 0 ;
274
+ dispatch_source_set_event_handler (self.source , ^{
275
+ count++;
276
+ if (count == 4 ){
277
+ // Fulfill the expectation when a write is detected
278
+ sleep (1 );
279
+ operationCompleted = YES ;
276
280
}
277
- }];
278
-
281
+ });
282
+
283
+ // Start monitoring
284
+ dispatch_resume (self.source );
285
+
279
286
// Seven messages should reach the remote log file for upload.
280
-
281
287
[NewRelic logInfo: @" Info Log..." ];
282
288
[NewRelic logError: @" Error Log..." ];
283
289
[NewRelic logVerbose: @" Verbose Log..." ];
@@ -291,18 +297,18 @@ - (void) testLocalLogLevels {
291
297
@" additionalAttribute2" : @" attribute2"
292
298
}];
293
299
294
- XCTestExpectation *delayExpectation2 = [ self expectationWithDescription: @" Waiting for Log Queue " ];
295
-
296
- dispatch_after ( dispatch_time (DISPATCH_TIME_NOW, ( int64_t )( 2 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
297
- [delayExpectation2 fulfill ];
298
- });
299
-
300
- [ self waitForExpectationsWithTimeout: 5 handler: ^( NSError * _Nullable error) {
301
- if (error) {
302
- XCTFail ( @" Timeout error " );
303
- }
304
- }] ;
305
-
300
+ // Set a timeout duration
301
+ NSTimeInterval timeout = 30.0 ;
302
+ NSDate *timeoutDate = [ NSDate dateWithTimeIntervalSinceNow: timeout];
303
+
304
+ // Run the run loop until the operation completes or the timeout is reached
305
+ while (!operationCompleted && [timeoutDate timeIntervalSinceNow ] > 0 ) {
306
+ // Allow other scheduled run loop activities to proceed
307
+ [[ NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [ NSDate dateWithTimeIntervalSinceNow: 0.1 ]];
308
+ }
309
+ if (!operationCompleted) {
310
+ NSLog ( @" Failed to detect 4 writes to the log file. " ) ;
311
+ }
306
312
NSError * error;
307
313
NSData * logData = [NRLogger logFileData: &error];
308
314
if (error){
@@ -352,8 +358,19 @@ - (void) testAutoCollectedLogs {
352
358
[NRLogger setRemoteLogLevel: NRLogLevelDebug];
353
359
XCTAssertTrue ([NRAutoLogCollector redirectStandardOutputAndError ]);
354
360
355
- sleep (5 );
356
-
361
+ __block BOOL operationCompleted = NO ;
362
+ __block int count = 0 ;
363
+ dispatch_source_set_event_handler (self.source , ^{
364
+ count++;
365
+ if (count == 5 ){
366
+ // Fulfill the expectation when a write is detected
367
+ sleep (1 );
368
+ operationCompleted = YES ;
369
+ }
370
+ });
371
+
372
+ // Start monitoring
373
+ dispatch_resume (self.source );
357
374
// Three messages should reach the remote log file for upload.
358
375
NSLog (@" NSLog Test \n\n " );
359
376
os_log_t customLog = os_log_create (" com.agent.tests" , " logTest" );
@@ -363,7 +380,19 @@ - (void) testAutoCollectedLogs {
363
380
os_log_error (customLog, " This is an error os_log message.\n " );
364
381
os_log_fault (customLog, " This is a fault os_log message.\n " );
365
382
366
- sleep (5 );
383
+ // Set a timeout duration
384
+ NSTimeInterval timeout = 30.0 ;
385
+ NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow: timeout];
386
+
387
+ // Run the run loop until the operation completes or the timeout is reached
388
+ while (!operationCompleted && [timeoutDate timeIntervalSinceNow ] > 0 ) {
389
+ // Allow other scheduled run loop activities to proceed
390
+ [[NSRunLoop currentRunLoop ] runMode: NSDefaultRunLoopMode beforeDate: [NSDate dateWithTimeIntervalSinceNow: 0.1 ]];
391
+ }
392
+ if (!operationCompleted) {
393
+ NSLog (@" Failed to detect 5 writes to the log file." );
394
+ }
395
+
367
396
[NRAutoLogCollector restoreStandardOutputAndError ];
368
397
369
398
NSError * error;
0 commit comments