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/slang-unit-test/unit-test-process.cpp | 125 ++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 tools/slang-unit-test/unit-test-process.cpp (limited to 'tools/slang-unit-test/unit-test-process.cpp') diff --git a/tools/slang-unit-test/unit-test-process.cpp b/tools/slang-unit-test/unit-test-process.cpp new file mode 100644 index 000000000..3dccf1eaf --- /dev/null +++ b/tools/slang-unit-test/unit-test-process.cpp @@ -0,0 +1,125 @@ +// unit-test-process.cpp + +#include "../../source/core/slang-string-util.h" +#include "../../source/core/slang-process-util.h" +#include "../../source/core/slang-io.h" + +#include "tools/unit-test/slang-unit-test.h" + +using namespace Slang; + +static SlangResult _countTest(UnitTestContext* context, Index size, Index crashIndex = -1) +{ + RefPtr process; + + /* Here we are trying to test what happens if the server produces a large amount of data, and + we just wait for termination. Do we receive all of the data irrespective of how much there is? */ + + { + CommandLine cmdLine; + + cmdLine.setExecutable(context->executableDirectory, "test-proxy"); + cmdLine.addArg("count"); + + StringBuilder buf; + buf << size; + + cmdLine.addArg(buf); + + if (crashIndex >= 0) + { + buf.Clear(); + buf << crashIndex; + cmdLine.addArg(buf); + } + + SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, process)); + } + + ExecuteResult exeRes; + +#if 0 + /* It does block on ~4k of data which matches up with the buffer size, using this mechanism only works up to 4k on windows + which matches the default pipe buffer size */ + process->waitForTermination(); +#endif + + SLANG_RETURN_ON_FAIL(ProcessUtil::readUntilTermination(process, exeRes)); + + Index v = 0; + for (auto line : LineParser(exeRes.standardOutput.getUnownedSlice())) + { + if (line.getLength() == 0) + { + continue; + } + + Index value; + StringUtil::parseInt(line, value); + + if (value != v) + { + return SLANG_FAIL; + } + + v++; + } + + const Index endIndex = (crashIndex >= 0) ? (crashIndex + 1) : size; + + return v == endIndex ? SLANG_OK : SLANG_FAIL; +} + +static SlangResult _countTests(UnitTestContext* context) +{ + const Index sizes[] = { 1, 10, 1000, 1000, 10000, 100000 }; + for (auto size : sizes) + { + SLANG_RETURN_ON_FAIL(_countTest(context, size)); + SLANG_RETURN_ON_FAIL(_countTest(context, size, size / 2)); + } + + return SLANG_OK; +} + +static SlangResult _reflectTest(UnitTestContext* context) +{ + RefPtr process; + { + CommandLine cmdLine; + cmdLine.setExecutable(context->executableDirectory, "test-proxy"); + cmdLine.addArg("reflect"); + SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, process)); + } + + // Write a bunch of stuff to the stream + Stream* readStream = process->getStream(Process::StreamType::StdOut); + Stream* writeStream = process->getStream(Process::StreamType::StdIn); + + List readBuffer; + + for (Index i = 0; i < 10000; i++) + { + SLANG_RETURN_ON_FAIL(StreamUtil::read(readStream, 0, readBuffer)); + + StringBuilder buf; + + buf << i << " Hello " << i << "\n"; + SLANG_RETURN_ON_FAIL(writeStream->write(buf.getBuffer(), buf.getLength())); + } + + const char end[] = "end\n"; + SLANG_RETURN_ON_FAIL(writeStream->write(end, SLANG_COUNT_OF(end) - 1)); + writeStream->flush(); + + SLANG_RETURN_ON_FAIL(StreamUtil::readAll(readStream, 0, readBuffer)); + + return SLANG_OK; +} + +SLANG_UNIT_TEST(CommandLineProcess) +{ + SLANG_CHECK(SLANG_SUCCEEDED(_countTests(unitTestContext))); + + SLANG_CHECK(SLANG_SUCCEEDED(_reflectTest(unitTestContext))); +} -- cgit v1.2.3