Skip to content

Commit

Permalink
Using sleep instead of the wait
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruin-NR committed Oct 24, 2024
1 parent 9713254 commit 5ca94aa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Agent/Utilities/NRAutoLogCollector.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

}

+ (void) redirectStandardOutputAndError;
+ (BOOL) redirectStandardOutputAndError;
+ (void) restoreStandardOutputAndError;

@end
10 changes: 6 additions & 4 deletions Agent/Utilities/NRAutoLogCollector.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ @interface NRAutoLogCollector()

@implementation NRAutoLogCollector

+ (void) redirectStandardOutputAndError {
+ (BOOL) redirectStandardOutputAndError {
// Create pipes for stdout and stderr
if (pipe(stdoutPipe) == -1 || pipe(stderrPipe) == -1) {
return;
return false;
}

// Save the original stdout and stderr file descriptors
Expand All @@ -34,7 +34,7 @@ + (void) redirectStandardOutputAndError {
close(stdoutPipe[1]);
close(stderrPipe[0]);
close(stderrPipe[1]);
return;
return false;
}

// Redirect stdout and stderr to the write ends of the pipes
Expand All @@ -45,7 +45,7 @@ + (void) redirectStandardOutputAndError {
close(stderrPipe[1]);
close(saved_stdout);
close(saved_stderr);
return;
return false;
}
close(stdoutPipe[1]); // Close the original write end of the stdout pipe
close(stderrPipe[1]); // Close the original write end of the stderr pipe
Expand All @@ -62,6 +62,8 @@ + (void) redirectStandardOutputAndError {
atexit_b(^{
[NRAutoLogCollector restoreStandardOutputAndError];
});

return true;
}

+ (void) readAndLog:(int) fd {
Expand Down
26 changes: 3 additions & 23 deletions Tests/Unit-Tests/NewRelicAgentTests/Uncategorized/NRLoggerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -260,19 +260,9 @@ - (void) testAutoCollectedLogs {
[NRMAFlags enableFeatures: NRFeatureFlag_RedirectStdOutStdErr];
// Set the remote log level to debug.
[NRLogger setRemoteLogLevel:NRLogLevelDebug];
[NRAutoLogCollector redirectStandardOutputAndError];
XCTAssertTrue([NRAutoLogCollector redirectStandardOutputAndError]);

XCTestExpectation *delayExpectation1 = [self expectationWithDescription:@"Waiting for Log Queue"];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[delayExpectation1 fulfill];
});

[self waitForExpectationsWithTimeout:7 handler:^(NSError * _Nullable error) {
if (error) {
XCTFail(@"Timeout error");
}
}];
sleep(1);

// Three messages should reach the remote log file for upload.
NSLog(@"NSLog Test \n\n");
Expand All @@ -283,17 +273,7 @@ - (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");

XCTestExpectation *delayExpectation2 = [self expectationWithDescription:@"Waiting for Log Queue"];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[delayExpectation2 fulfill];
});

[self waitForExpectationsWithTimeout:8 handler:^(NSError * _Nullable error) {
if (error) {
XCTFail(@"Timeout error");
}
}];
sleep(1);
[NRAutoLogCollector restoreStandardOutputAndError];

NSError* error;
Expand Down

0 comments on commit 5ca94aa

Please sign in to comment.