diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-11-24 09:34:05 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-24 09:34:05 -0500 |
| commit | bdc61bb8db321a8d788aa739b889354540a49ee0 (patch) | |
| tree | 16c564282a28d69e77d51855a41f11bdfa80fded /tools/test-process | |
| parent | 9e084ffab37c276d40931a58633041a2e10de623 (diff) | |
Remove test-proxy (#2027)
* #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.
* Added inital processing for http headers.
* Small improvements to HttpHeader.
* First pass HTTPPacketConnection working on windows.
* Enable other Process communication tests.
* Update comments.
* WIP JSON RPC.
* Add terminate to Process.
Made JSONRPC a Util.
* Small tidy up around HTTPPacketConnection.
* Improve process termination options.
* WIP for test-server.
* Add diagnostics error handling to test-server.
* Improved JSON support.
Parsing/creating JSON-RPC messages.
* WIP JSONRPC parsing.
* First pass RttiInfo support.
* WIP converting between JSON/native types.
* Project files.
* Split out RttiUtil.
Made RttiInfo constuction thread safe.
* WIP RTTI<->JSON.
* Add diagnostics to JSON<->native conversions.
* Make RttiInfo for structs globals. Avoids problem around derived types (like pointers), being able to cause an abort.
* Add pointer support to RTTI.
Fixed some compilation issues on linux.
* Add fixed array support.
* Added Rtti unit test.
* Add rtti unit test.
* Split out quoted/unquoted key handling.
Fix bugs in JSON value/container.
Added JSON native test.
* Make default array allocator use malloc/free.
Remove the new[] handler (doesn't work on visuals studio).
* Fix for linux warning.
* Remove some test code.
* Fix issues on x86 win.
* Fix warning on aarch64.
* Fix some bugs in JSON parsing/handling.
Make Rtti work copy/dtor/ctor struct types.
* Testing JSON<->native with fixed array.
Make makeArrayView explicit if it's just a single value.
Added array type.
* Fix getting arrayView.
* Improve JSON diagnostic name.
* First pass refactor using Rtti for JSON RPC.
* First pass of test server using RTTI/JSON-RPC.
* Added JSONRPCConnection.
* Fix some naming issues.
* First pass of test-server working.
* Added unit test support for JSON-RPC test server.
* Fix compilation issues on linux around template handling.
* Typo fix.
* Fix a bug around SourceLoc lookup with JSONContainer.
* Set the console type to console for ISlangWriters.
* Small improvements to test-server.
* Small improvements in test-server.
* Small fix.
* Remove test-proxy. Make test-process a process that can be used to unit test 'Process'.
Adding mechanism to control spawning that will create a new process for every test.
* Ability to remove source manager for JSONValue.
Diffstat (limited to 'tools/test-process')
| -rw-r--r-- | tools/test-process/test-process-main.cpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/tools/test-process/test-process-main.cpp b/tools/test-process/test-process-main.cpp new file mode 100644 index 000000000..c31fec3ed --- /dev/null +++ b/tools/test-process/test-process-main.cpp @@ -0,0 +1,193 @@ +// test-process-main.cpp + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +#include "../../slang-com-helper.h" + +#include "../../source/core/slang-string.h" +#include "../../source/core/slang-io.h" +#include "../../source/core/slang-string-util.h" +#include "../../source/core/slang-process-util.h" + +#include "../../source/core/slang-test-tool-util.h" +#include "../../source/core/slang-http.h" + +namespace TestProcess +{ +using namespace Slang; + +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<Stream> stdinStream; + Process::getStdStream(Process::StreamType::StdIn, stdinStream); + + FILE* fileOut = stdout; + + List<Byte> 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 _httpReflect(int argc, const char* const* argv) +{ + SLANG_UNUSED(argc); + SLANG_UNUSED(argv); + + RefPtr<Stream> stdinStream, stdoutStream; + + Process::getStdStream(Process::StreamType::StdIn, stdinStream); + Process::getStdStream(Process::StreamType::StdOut, stdoutStream); + + RefPtr<BufferedReadStream> readStream(new BufferedReadStream(stdinStream)); + + RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, stdoutStream); + + while (connection->isActive()) + { + // Block waiting for content (or error/closed) + SLANG_RETURN_ON_FAIL(connection->waitForResult()); + + // If we have content do something with it + if (connection->hasContent()) + { + auto content = connection->getContent(); + + // If it just holds 'end' then we are done + const UnownedStringSlice slice((const char*)content.begin(), content.getCount()); + + if (slice == UnownedStringSlice::fromLiteral("end")) + { + break; + } + + // Else reflect it back + SLANG_RETURN_ON_FAIL(connection->write(content.begin(), content.getCount())); + + // Consume that content/packet + connection->consumeContent(); + } + } + + return SLANG_OK; +} + +static SlangResult execute(int argc, const char*const* argv) +{ + if (argc < 2) + { + return SLANG_FAIL; + } + + // Get the tool name + const String toolName = argv[1]; + if (toolName == "reflect") + { + return _outputReflect(); + } + else if (toolName == "count") + { + return _outputCount(argc, argv); + } + else if (toolName == "http-reflect") + { + return _httpReflect(argc, argv); + } + return SLANG_E_NOT_AVAILABLE; +} + +} // namespace TestProcess + +int main(int argc, const char* const* argv) +{ + SlangResult res = TestProcess::execute(argc, argv); + return (int)Slang::TestToolUtil::getReturnCode(res); +} |
