Skip to content

Commit

Permalink
Trying to detect writes for logging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Dec 16, 2024
1 parent 5464124 commit 1ef1da5
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@
NSString* category;
NSString* name;
}
@property (nonatomic) int fileDescriptor;
@property (nonatomic, strong) dispatch_source_t source;
@end
88 changes: 77 additions & 11 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,35 @@ - (void) setUp

[NRLogger clearLog];

// Open a file descriptor for the file
self.fileDescriptor = open([[NRLogger logFilePath] fileSystemRepresentation], O_EVTONLY);
if (self.fileDescriptor < 0) {
XCTFail(@"Failed to open file descriptor");
return;
}

// Set up dispatch source for file monitoring
self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, self.fileDescriptor, DISPATCH_VNODE_WRITE, DISPATCH_TARGET_QUEUE_DEFAULT);

__weak typeof(self) weakSelf = self;
dispatch_source_set_cancel_handler(self.source, ^{
if (weakSelf.fileDescriptor) {
close(weakSelf.fileDescriptor);
weakSelf.fileDescriptor = 0;
}
});


}
- (void) tearDown
{
if (self.fileDescriptor > 0) {
close(self.fileDescriptor);
}
if (self.source) {
dispatch_source_cancel(self.source);
}

[NRMAMeasurements removeMeasurementConsumer:helper];
helper = nil;

Expand All @@ -82,7 +107,7 @@ - (void) testNRLogger {
}];

sleep(5);

NSError* error;
NSData* logData = [NRLogger logFileData:&error];
if(error){
Expand Down Expand Up @@ -151,8 +176,22 @@ - (void) testRemoteLogLevels {
// Set the remote log level to Debug.
[NRLogger setRemoteLogLevel:NRLogLevelDebug];

sleep(1);

// Set up the expectation
XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription:@"File has been modified"];

__block int count = 0;
dispatch_source_set_event_handler(self.source, ^{
count++;
if(count == 7){
// Fulfill the expectation when a write is detected
sleep(1);
[fileWrittenExpectation fulfill];
}
});

// Start monitoring
dispatch_resume(self.source);

// Seven messages should reach the remote log file for upload.

[NewRelic logInfo: @"Info Log..."];
Expand All @@ -168,7 +207,7 @@ - (void) testRemoteLogLevels {
@"additionalAttribute2": @"attribute2"
}];

sleep(5);
[self waitForExpectationsWithTimeout:5 handler:nil];

NSError* error;
NSData* logData = [NRLogger logFileData:&error];
Expand Down Expand Up @@ -221,10 +260,23 @@ - (void) testLocalLogLevels {
// Set the remote log level to Info.
[NRLogger setRemoteLogLevel:NRLogLevelInfo];

sleep(1);

// Set up the expectation
XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription:@"File has been modified"];

__block int count = 0;
dispatch_source_set_event_handler(self.source, ^{
count++;
if(count == 4){
// Fulfill the expectation when a write is detected
sleep(1);
[fileWrittenExpectation fulfill];
}
});

// Start monitoring
dispatch_resume(self.source);

// Seven messages should reach the remote log file for upload.

[NewRelic logInfo: @"Info Log..."];
[NewRelic logError: @"Error Log..."];
[NewRelic logVerbose:@"Verbose Log..."];
Expand All @@ -238,7 +290,7 @@ - (void) testLocalLogLevels {
@"additionalAttribute2": @"attribute2"
}];

sleep(5);
[self waitForExpectationsWithTimeout:5 handler:nil];

NSError* error;
NSData* logData = [NRLogger logFileData:&error];
Expand Down Expand Up @@ -289,8 +341,21 @@ - (void) testAutoCollectedLogs {
[NRLogger setRemoteLogLevel:NRLogLevelDebug];
XCTAssertTrue([NRAutoLogCollector redirectStandardOutputAndError]);

sleep(1);

// Set up the expectation
XCTestExpectation *fileWrittenExpectation = [self expectationWithDescription:@"File has been modified"];

__block int count = 0;
dispatch_source_set_event_handler(self.source, ^{
count++;
if(count == 5){
// Fulfill the expectation when a write is detected
sleep(1);
[fileWrittenExpectation fulfill];
}
});

// Start monitoring
dispatch_resume(self.source);
// Three messages should reach the remote log file for upload.
NSLog(@"NSLog Test \n\n");
os_log_t customLog = os_log_create("com.agent.tests", "logTest");
Expand All @@ -300,7 +365,8 @@ - (void) testAutoCollectedLogs {
os_log_error(customLog, "This is an error os_log message.\n");
os_log_fault(customLog, "This is a fault os_log message.\n");

sleep(5);
[self waitForExpectationsWithTimeout:5 handler:nil];

[NRAutoLogCollector restoreStandardOutputAndError];

NSError* error;
Expand Down

0 comments on commit 1ef1da5

Please sign in to comment.