yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
5.4 KiB199 linesraw
1// unit-test-process.cpp
2
3#include "../../source/core/slang-http.h"
4#include "../../source/core/slang-io.h"
5#include "../../source/core/slang-process-util.h"
6#include "../../source/core/slang-random-generator.h"
7#include "../../source/core/slang-string-util.h"
8#include "unit-test/slang-unit-test.h"
9
10using namespace Slang;
11
12static SlangResult _createProcess(
13    UnitTestContext* context,
14    const char* toolName,
15    const List<String>* optArgs,
16    RefPtr<Process>& outProcess)
17{
18    CommandLine cmdLine;
19    cmdLine.setExecutableLocation(ExecutableLocation(context->executableDirectory, "test-process"));
20    cmdLine.addArg(toolName);
21    if (optArgs)
22    {
23        cmdLine.m_args.addRange(optArgs->getBuffer(), optArgs->getCount());
24    }
25
26    SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, outProcess));
27
28    return SLANG_OK;
29}
30
31static SlangResult _httpReflectTest(UnitTestContext* context)
32{
33    SlangResult finalRes = SLANG_OK;
34
35    RefPtr<Process> process;
36    SLANG_RETURN_ON_FAIL(_createProcess(context, "http-reflect", nullptr, process));
37
38    Stream* writeStream = process->getStream(StdStreamType::In);
39    RefPtr<BufferedReadStream> readStream(
40        new BufferedReadStream(process->getStream(StdStreamType::Out)));
41    RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, writeStream);
42
43    RefPtr<RandomGenerator> rand = RandomGenerator::create(10000);
44
45    for (Index i = 0; i < 100; i++)
46    {
47        if (process->isTerminated())
48        {
49            return SLANG_FAIL;
50        }
51
52        const Index size = Index(rand->nextInt32UpTo(8192));
53
54        List<Byte> buf;
55        buf.setCount(size);
56        // Build up a buffer
57        rand->nextData(buf.getBuffer(), size_t(size));
58
59        // Write the data
60        SLANG_RETURN_ON_FAIL(connection->write(buf.getBuffer(), size_t(size)));
61
62        // Wait for the response
63        SLANG_RETURN_ON_FAIL(connection->waitForResult());
64
65        // If we don't have content then something has gone wrong
66        if (!connection->hasContent())
67        {
68            finalRes = SLANG_FAIL;
69            break;
70        }
71
72        // Check the content is the same
73        auto readContent = connection->getContent();
74        if (readContent != buf.getArrayView())
75        {
76            finalRes = SLANG_FAIL;
77            break;
78        }
79
80        // Consume that packet
81        connection->consumeContent();
82    }
83
84    // Send the end
85    {
86        const char end[] = "end";
87        SLANG_RETURN_ON_FAIL(connection->write(end, SLANG_COUNT_OF(end) - 1));
88    }
89
90    process->waitForTermination();
91    return finalRes;
92}
93
94static SlangResult _countTest(UnitTestContext* context, Index size, Index crashIndex = -1)
95{
96    /* Here we are trying to test what happens if the server produces a large amount of data, and
97    we just wait for termination. Do we receive all of the data irrespective of how much there is?
98  */
99
100    List<String> args;
101    {
102        StringBuilder buf;
103        buf << size;
104
105        args.add(buf);
106
107        if (crashIndex >= 0)
108        {
109            buf.clear();
110            buf << crashIndex;
111            args.add(buf);
112        }
113    }
114
115    RefPtr<Process> process;
116    SLANG_RETURN_ON_FAIL(_createProcess(context, "count", &args, process));
117
118    ExecuteResult exeRes;
119
120#if 0
121    /* It does block on ~4k of data which matches up with the buffer size, using this mechanism only works up to 4k on windows
122    which matches the default pipe buffer size */
123    process->waitForTermination();
124#endif
125
126    SLANG_RETURN_ON_FAIL(ProcessUtil::readUntilTermination(process, exeRes));
127
128    Index v = 0;
129    for (auto line : LineParser(exeRes.standardOutput.getUnownedSlice()))
130    {
131        if (line.getLength() == 0)
132        {
133            continue;
134        }
135
136        Index value;
137        StringUtil::parseInt(line, value);
138
139        if (value != v)
140        {
141            return SLANG_FAIL;
142        }
143
144        v++;
145    }
146
147    const Index endIndex = (crashIndex >= 0) ? (crashIndex + 1) : size;
148
149    return v == endIndex ? SLANG_OK : SLANG_FAIL;
150}
151
152static SlangResult _countTests(UnitTestContext* context)
153{
154    const Index sizes[] = {1, 10, 1000, 1000, 10000, 100000};
155    for (auto size : sizes)
156    {
157        SLANG_RETURN_ON_FAIL(_countTest(context, size));
158        SLANG_RETURN_ON_FAIL(_countTest(context, size, size / 2));
159    }
160
161    return SLANG_OK;
162}
163
164static SlangResult _reflectTest(UnitTestContext* context)
165{
166    RefPtr<Process> process;
167    SLANG_RETURN_ON_FAIL(_createProcess(context, "reflect", nullptr, process));
168
169    // Write a bunch of stuff to the stream
170    Stream* readStream = process->getStream(StdStreamType::Out);
171    Stream* writeStream = process->getStream(StdStreamType::In);
172
173    List<Byte> readBuffer;
174
175    for (Index i = 0; i < 10000; i++)
176    {
177        SLANG_RETURN_ON_FAIL(StreamUtil::read(readStream, 0, readBuffer));
178
179        StringBuilder buf;
180
181        buf << i << " Hello " << i << "\n";
182        SLANG_RETURN_ON_FAIL(writeStream->write(buf.getBuffer(), buf.getLength()));
183    }
184
185    const char end[] = "end\n";
186    SLANG_RETURN_ON_FAIL(writeStream->write(end, SLANG_COUNT_OF(end) - 1));
187    writeStream->flush();
188
189    SLANG_RETURN_ON_FAIL(StreamUtil::readAll(readStream, 0, readBuffer));
190
191    return SLANG_OK;
192}
193
194SLANG_UNIT_TEST(CommandLineProcess)
195{
196    SLANG_CHECK(SLANG_SUCCEEDED(_countTests(unitTestContext)));
197    SLANG_CHECK(SLANG_SUCCEEDED(_reflectTest(unitTestContext)));
198    SLANG_CHECK(SLANG_SUCCEEDED(_httpReflectTest(unitTestContext)));
199}