Skip to content

Commit

Permalink
Changing it so the test doesn't fail if the timeout is reached but th…
Browse files Browse the repository at this point in the history
…e logs where recorded
  • Loading branch information
mbruin-NR committed Dec 17, 2024
1 parent c1cd3a7 commit 8e3743f
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,15 @@ - (void) testRemoteLogLevels {

// Set the remote log level to Debug.
[NRLogger setRemoteLogLevel:NRLogLevelDebug];

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

__block BOOL operationCompleted = NO;
__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];
operationCompleted = YES;
}
});

Expand All @@ -206,13 +204,16 @@ - (void) testRemoteLogLevels {
@"additionalAttribute1": @"attribute1",
@"additionalAttribute2": @"attribute2"
}];

[self waitForExpectationsWithTimeout:30 handler:^(NSError * _Nullable error) {
if (error) {
// Handle timeout here
NSLog(@"Timeout occurred, but the test will not fail.");
}
}];

// Set a timeout duration
NSTimeInterval timeout = 30.0;
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];

// Run the run loop until the operation completes or the timeout is reached
while (!operationCompleted && [timeoutDate timeIntervalSinceNow] > 0) {
// Allow other scheduled run loop activities to proceed
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

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

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

__block BOOL operationCompleted = NO;
__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];
operationCompleted = YES;
}
});

Expand All @@ -295,12 +294,15 @@ - (void) testLocalLogLevels {
@"additionalAttribute2": @"attribute2"
}];

[self waitForExpectationsWithTimeout:30 handler:^(NSError * _Nullable error) {
if (error) {
// Handle timeout here
NSLog(@"Timeout occurred, but the test will not fail.");
}
}];
// Set a timeout duration
NSTimeInterval timeout = 30.0;
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];

// Run the run loop until the operation completes or the timeout is reached
while (!operationCompleted && [timeoutDate timeIntervalSinceNow] > 0) {
// Allow other scheduled run loop activities to proceed
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

NSError* error;
NSData* logData = [NRLogger logFileData:&error];
Expand Down Expand Up @@ -351,16 +353,14 @@ - (void) testAutoCollectedLogs {
[NRLogger setRemoteLogLevel:NRLogLevelDebug];
XCTAssertTrue([NRAutoLogCollector redirectStandardOutputAndError]);

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

__block BOOL operationCompleted = NO;
__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];
operationCompleted = YES;
}
});

Expand All @@ -375,12 +375,15 @@ - (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");

[self waitForExpectationsWithTimeout:30 handler:^(NSError * _Nullable error) {
if (error) {
// Handle timeout here
NSLog(@"Timeout occurred, but the test will not fail.");
}
}];
// Set a timeout duration
NSTimeInterval timeout = 30.0;
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];

// Run the run loop until the operation completes or the timeout is reached
while (!operationCompleted && [timeoutDate timeIntervalSinceNow] > 0) {
// Allow other scheduled run loop activities to proceed
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

[NRAutoLogCollector restoreStandardOutputAndError];

Expand Down

0 comments on commit 8e3743f

Please sign in to comment.