@@ -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
@@ -82,7 +107,7 @@ - (void) testNRLogger {
82
107
}];
83
108
84
109
sleep (5 );
85
-
110
+
86
111
NSError * error;
87
112
NSData * logData = [NRLogger logFileData: &error];
88
113
if (error){
@@ -151,8 +176,22 @@ - (void) testRemoteLogLevels {
151
176
// Set the remote log level to Debug.
152
177
[NRLogger setRemoteLogLevel: NRLogLevelDebug];
153
178
154
- sleep (1 );
155
-
179
+ // Set up the expectation
180
+ XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription: @" File has been modified" ];
181
+
182
+ __block int count = 0 ;
183
+ dispatch_source_set_event_handler (self.source , ^{
184
+ count++;
185
+ if (count == 7 ){
186
+ // Fulfill the expectation when a write is detected
187
+ sleep (1 );
188
+ [fileWrittenExpectation fulfill ];
189
+ }
190
+ });
191
+
192
+ // Start monitoring
193
+ dispatch_resume (self.source );
194
+
156
195
// Seven messages should reach the remote log file for upload.
157
196
158
197
[NewRelic logInfo: @" Info Log..." ];
@@ -168,7 +207,7 @@ - (void) testRemoteLogLevels {
168
207
@" additionalAttribute2" : @" attribute2"
169
208
}];
170
209
171
- sleep ( 5 ) ;
210
+ [ self waitForExpectationsWithTimeout: 5 handler: nil ] ;
172
211
173
212
NSError * error;
174
213
NSData * logData = [NRLogger logFileData: &error];
@@ -221,10 +260,23 @@ - (void) testLocalLogLevels {
221
260
// Set the remote log level to Info.
222
261
[NRLogger setRemoteLogLevel: NRLogLevelInfo];
223
262
224
- sleep (1 );
225
-
263
+ // Set up the expectation
264
+ XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription: @" File has been modified" ];
265
+
266
+ __block int count = 0 ;
267
+ dispatch_source_set_event_handler (self.source , ^{
268
+ count++;
269
+ if (count == 4 ){
270
+ // Fulfill the expectation when a write is detected
271
+ sleep (1 );
272
+ [fileWrittenExpectation fulfill ];
273
+ }
274
+ });
275
+
276
+ // Start monitoring
277
+ dispatch_resume (self.source );
278
+
226
279
// Seven messages should reach the remote log file for upload.
227
-
228
280
[NewRelic logInfo: @" Info Log..." ];
229
281
[NewRelic logError: @" Error Log..." ];
230
282
[NewRelic logVerbose: @" Verbose Log..." ];
@@ -238,7 +290,7 @@ - (void) testLocalLogLevels {
238
290
@" additionalAttribute2" : @" attribute2"
239
291
}];
240
292
241
- sleep ( 5 ) ;
293
+ [ self waitForExpectationsWithTimeout: 5 handler: nil ] ;
242
294
243
295
NSError * error;
244
296
NSData * logData = [NRLogger logFileData: &error];
@@ -289,8 +341,21 @@ - (void) testAutoCollectedLogs {
289
341
[NRLogger setRemoteLogLevel: NRLogLevelDebug];
290
342
XCTAssertTrue ([NRAutoLogCollector redirectStandardOutputAndError ]);
291
343
292
- sleep (1 );
293
-
344
+ // Set up the expectation
345
+ XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription: @" File has been modified" ];
346
+
347
+ __block int count = 0 ;
348
+ dispatch_source_set_event_handler (self.source , ^{
349
+ count++;
350
+ if (count == 5 ){
351
+ // Fulfill the expectation when a write is detected
352
+ sleep (1 );
353
+ [fileWrittenExpectation fulfill ];
354
+ }
355
+ });
356
+
357
+ // Start monitoring
358
+ dispatch_resume (self.source );
294
359
// Three messages should reach the remote log file for upload.
295
360
NSLog (@" NSLog Test \n\n " );
296
361
os_log_t customLog = os_log_create (" com.agent.tests" , " logTest" );
@@ -300,7 +365,8 @@ - (void) testAutoCollectedLogs {
300
365
os_log_error (customLog, " This is an error os_log message.\n " );
301
366
os_log_fault (customLog, " This is a fault os_log message.\n " );
302
367
303
- sleep (5 );
368
+ [self waitForExpectationsWithTimeout: 5 handler: nil ];
369
+
304
370
[NRAutoLogCollector restoreStandardOutputAndError ];
305
371
306
372
NSError * error;
0 commit comments