From 8a9e518371df03b3f382e0fe869da83751fdda0b Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 10 Nov 2021 17:33:22 -0500 Subject: Interprocess communication via pipes (#2009) * #include an absolute path didn't work - because paths were taken to always be relative. * Use 'Process' to communicate with an command line tool. * Remove slang-win-stream * Tidy up windows ProcessUtil. * First version of BufferedReadStream. * Windows working IPC for steams. * Test proxy count option. * Split Process/ProcessUtil. Process is platform dependant. ProcessUtil are functions that are platform independent. * First implementation of Unix Process interface. * Unix process compiles on cygwin. * Fix typo in unix process. * Separate unix pipe stream error of invalid access, from pipe availability. * Fix in standard line extraction. * Make fd non blocking. * Fix issues with Windows Process streams. * Added UnixPipe. * Some fixes around UnixPipeStream. * Make a unix stream closed explicit. * Hack to debug linux process/stream. * Revert to old linux pipe handling. * Pass executable path for unit tests. Split out CommandLine into own source. * Small improvements in process/command line. * Check process behavior with crash. * Make stderr and stdout unbuffered for crash testing. * Only turn disable buffering in crash test. * Disable crash test on CI. * Fix crash on clang/linux. * Enable crash test. Remove _appendBuffer as can use StreamUtil functionality. --- tools/test-proxy/test-proxy-main.cpp | 113 ++++++++++++++++++++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) (limited to 'tools/test-proxy/test-proxy-main.cpp') diff --git a/tools/test-proxy/test-proxy-main.cpp b/tools/test-proxy/test-proxy-main.cpp index 2b6c59047..70efc8907 100644 --- a/tools/test-proxy/test-proxy-main.cpp +++ b/tools/test-proxy/test-proxy-main.cpp @@ -12,6 +12,7 @@ #include "../../source/core/slang-io.h" #include "../../source/core/slang-writer.h" #include "../../source/core/slang-string-util.h" +#include "../../source/core/slang-process-util.h" #include "../../source/core/slang-shared-library.h" @@ -111,10 +112,109 @@ static SlangResult _createSlangSession(const char* exePath, int argc, const char return SLANG_OK; } -static SlangResult execute(int argc, const char* const* argv) +static void _makeStdStreamsUnbuffered() +{ + // Make these streams unbuffered + setvbuf(stdout, nullptr, _IONBF, 0); + setvbuf(stderr, nullptr, _IONBF, 0); +} + +static SlangResult _outputCount(int argc, const char* const* argv) +{ + if (argc < 3) + { + return SLANG_FAIL; + } + // Get the count + const Index count = StringToInt(argv[2]); + + // If we want to crash + Index crashIndex = -1; + if (argc > 3) + { + // When we crash, we want to make sure everything is flushed... + _makeStdStreamsUnbuffered(); + crashIndex = StringToInt(argv[3]); + } + + FILE* fileOut = stdout; + + StringBuilder buf; + for (Index i = 0; i < count; ++i) + { + buf.Clear(); + buf << i << "\n"; + + fwrite(buf.getBuffer(), 1, buf.getLength(), fileOut); + + if (i == crashIndex) + { + // Use to simulate a crash. + SLANG_BREAKPOINT(0); + throw; + } + } + + // NOTE! There is no flush here, we want to see if everything works if we just stream out. + return SLANG_OK; +} + +static SlangResult _outputReflect() +{ + // Read lines from std input. + // When hit line with just 'end', terminate + + // Get in as Stream + + RefPtr stdinStream; + Process::getStdStream(Process::StreamType::StdIn, stdinStream); + + FILE* fileOut = stdout; + + List buffer; + + Index lineCount = 0; + Index startIndex = 0; + + while (true) + { + SLANG_RETURN_ON_FAIL(StreamUtil::read(stdinStream, 0, buffer)); + + while (true) + { + UnownedStringSlice slice((const char*)buffer.begin() + startIndex, (const char*)buffer.end()); + UnownedStringSlice line; + + if (!StringUtil::extractLine(slice, line) || slice.begin() == nullptr) + { + break; + } + + // Process the line + if (line == UnownedStringSlice::fromLiteral("end")) + { + return SLANG_OK; + } + + // Write the text to the output stream + fwrite(line.begin(), 1, line.getLength(), fileOut); + fputc('\n', fileOut); + + lineCount++; + + // Move the start index forward + const Index newStartIndex = slice.begin() ? Index(slice.begin() - (const char*)buffer.getBuffer()) : buffer.getCount(); + SLANG_ASSERT(newStartIndex > startIndex); + startIndex = newStartIndex; + } + } +} + +static SlangResult execute(int argc, const char*const* argv) { typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc; + if (argc < 2) { return SLANG_FAIL; @@ -125,6 +225,16 @@ static SlangResult execute(int argc, const char* const* argv) // The 'exeName' of the tool. const String toolName = argv[1]; + if (toolName == "reflect") + { + return _outputReflect(); + } + + if (toolName == "count") + { + return _outputCount(argc, argv); + } + { StringBuilder sharedLibToolBuilder; sharedLibToolBuilder.append(toolName); @@ -203,6 +313,7 @@ static SlangResult execute(int argc, const char* const* argv) unitTestContext.slangGlobalSession = session; unitTestContext.workDirectory = ""; unitTestContext.enabledApis = RenderApiFlags(enabledApis); + unitTestContext.executableDirectory = exePath.getBuffer(); auto testCount = testModule->getTestCount(); SLANG_ASSERT(testIndex >= 0 && testIndex < testCount); -- cgit v1.2.3