mirror of
https://github.com/intel/compute-runtime.git
synced 2026-01-05 09:09:04 +08:00
test: StreamCapture on pipes
Enhance StreamCapture class to mimic gtest's CaptureStdout/Stderr functionality. Store data in a pipe instead of a temporary file like gtest resulting in faster test execution Signed-off-by: Marcel Skierkowski <marcel.skierkowski@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
767755df94
commit
a52260ce63
@@ -20,29 +20,46 @@
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
class StdoutCapture {
|
||||
class StreamCapture {
|
||||
public:
|
||||
void captureStdout() {
|
||||
captureStream(stdout, pipefdStdout, saveStdout);
|
||||
}
|
||||
|
||||
void captureStderr() {
|
||||
captureStream(stderr, pipefdStderr, saveStderr);
|
||||
}
|
||||
|
||||
std::string getCapturedStdout() {
|
||||
return getCapturedStream(stdout, pipefdStdout, saveStdout);
|
||||
}
|
||||
|
||||
std::string getCapturedStderr() {
|
||||
return getCapturedStream(stderr, pipefdStderr, saveStderr);
|
||||
}
|
||||
|
||||
private:
|
||||
void captureStream(FILE *stream, int pipefd[2], int &savedFd) {
|
||||
#ifdef _WIN32
|
||||
_pipe(pipefd, 4096, O_TEXT);
|
||||
fflush(stdout);
|
||||
saveStdout = _dup(_fileno(stdout));
|
||||
_dup2(pipefd[1], _fileno(stdout));
|
||||
fflush(stream);
|
||||
savedFd = _dup(_fileno(stream));
|
||||
_dup2(pipefd[1], _fileno(stream));
|
||||
_close(pipefd[1]);
|
||||
#else
|
||||
fflush(stdout);
|
||||
fflush(stream);
|
||||
pipe(pipefd);
|
||||
saveStdout = dup(fileno(stdout));
|
||||
dup2(pipefd[1], fileno(stdout));
|
||||
savedFd = dup(fileno(stream));
|
||||
dup2(pipefd[1], fileno(stream));
|
||||
close(pipefd[1]);
|
||||
#endif
|
||||
}
|
||||
|
||||
std::string getCapturedStdout() {
|
||||
std::string getCapturedStream(FILE *stream, int pipefd[2], int &savedFd) {
|
||||
#ifdef _WIN32
|
||||
fflush(stdout);
|
||||
_dup2(saveStdout, _fileno(stdout));
|
||||
_close(saveStdout);
|
||||
fflush(stream);
|
||||
_dup2(savedFd, _fileno(stream));
|
||||
_close(savedFd);
|
||||
|
||||
char buffer[4096];
|
||||
int count = _read(pipefd[0], buffer, sizeof(buffer) - 1);
|
||||
@@ -52,9 +69,9 @@ class StdoutCapture {
|
||||
}
|
||||
return "";
|
||||
#else
|
||||
fflush(stdout);
|
||||
dup2(saveStdout, fileno(stdout));
|
||||
close(saveStdout);
|
||||
fflush(stream);
|
||||
dup2(savedFd, fileno(stream));
|
||||
close(savedFd);
|
||||
|
||||
constexpr size_t bufferSize = 4096;
|
||||
char buffer[bufferSize];
|
||||
@@ -67,7 +84,8 @@ class StdoutCapture {
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
int pipefd[2];
|
||||
int pipefdStdout[2];
|
||||
int pipefdStderr[2];
|
||||
int saveStdout;
|
||||
};
|
||||
int saveStderr;
|
||||
};
|
||||
Reference in New Issue
Block a user