1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
// test-context.cpp
#include "test-context.h"
#include "../../source/compiler-core/slang-language-server-protocol.h"
#include "../../source/core/slang-io.h"
#include "../../source/core/slang-shared-library.h"
#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-test-tool-util.h"
#include <stdio.h>
#include <stdlib.h>
using namespace Slang;
thread_local int slangTestThreadIndex = 0;
TestContext::TestContext()
{
/// if we are testing on arm, debug, we may want to increase the connection timeout
#if (SLANG_PROCESSOR_ARM || SLANG_PROCESSOR_ARM_64) && defined(_DEBUG)
// 10 mins(!). This seems to be the order of time needed for timeout on a CI ARM test system on
// debug
connectionTimeOutInMs = 1000 * 60 * 10;
#endif
}
void TestContext::setThreadIndex(int index)
{
slangTestThreadIndex = index;
}
void TestContext::setMaxTestRunnerThreadCount(int count)
{
m_jsonRpcConnections.setCount(count);
m_testRequirements.setCount(count);
m_reporters.setCount(count);
for (auto& reporter : m_reporters)
{
reporter = nullptr;
}
}
void TestContext::setTestRequirements(TestRequirements* req)
{
m_testRequirements[slangTestThreadIndex] = req;
}
TestRequirements* TestContext::getTestRequirements() const
{
return m_testRequirements[slangTestThreadIndex];
}
void TestContext::setTestReporter(TestReporter* reporter)
{
m_reporters[slangTestThreadIndex] = reporter;
}
TestReporter* TestContext::getTestReporter()
{
return m_reporters[slangTestThreadIndex];
}
SlangResult TestContext::locateFileCheck()
{
DefaultSharedLibraryLoader* loader = DefaultSharedLibraryLoader::getSingleton();
SLANG_RETURN_ON_FAIL(loader->loadSharedLibrary("slang-llvm", m_fileCheckLibrary.writeRef()));
if (!m_fileCheckLibrary)
{
return SLANG_FAIL;
}
using CreateFileCheckFunc = SlangResult (*)(const SlangUUID&, void**);
auto fn = reinterpret_cast<CreateFileCheckFunc>(
m_fileCheckLibrary->findFuncByName("createLLVMFileCheck_V1"));
if (!fn)
{
return SLANG_FAIL;
}
return fn(SLANG_IID_PPV_ARGS(m_fileCheck.writeRef()));
}
Result TestContext::init(const char* inExePath)
{
SlangGlobalSessionDesc desc = {};
desc.enableGLSL = true;
SLANG_RETURN_ON_FAIL(slang::createGlobalSession(&desc, m_session.writeRef()));
exePath = inExePath;
SLANG_RETURN_ON_FAIL(TestToolUtil::getExeDirectoryPath(inExePath, exeDirectoryPath));
SLANG_RETURN_ON_FAIL(TestToolUtil::getDllDirectoryPath(inExePath, dllDirectoryPath));
SLANG_RETURN_ON_FAIL(locateFileCheck());
return SLANG_OK;
}
TestContext::~TestContext()
{
if (m_languageServerConnection)
{
m_languageServerConnection->sendCall(
LanguageServerProtocol::ExitParams::methodName,
JSONValue::makeInt(0));
}
}
TestContext::InnerMainFunc TestContext::getInnerMainFunc(const String& dirPath, const String& name)
{
{
SharedLibraryTool* tool = m_sharedLibTools.tryGetValue(name);
if (tool)
{
return tool->m_func;
}
}
StringBuilder sharedLibToolBuilder;
sharedLibToolBuilder.append(name);
sharedLibToolBuilder.append("-tool");
StringBuilder path;
SharedLibrary::appendPlatformFileName(sharedLibToolBuilder.getUnownedSlice(), path);
DefaultSharedLibraryLoader* loader = DefaultSharedLibraryLoader::getSingleton();
SharedLibraryTool tool = {};
if (SLANG_SUCCEEDED(
loader->loadPlatformSharedLibrary(path.begin(), tool.m_sharedLibrary.writeRef())))
{
tool.m_func = (InnerMainFunc)tool.m_sharedLibrary->findFuncByName("innerMain");
tool.m_cleanDeviceCacheFunc =
(CleanDeviceCacheFunc)tool.m_sharedLibrary->findFuncByName("cleanDeviceCache");
}
m_sharedLibTools.add(name, tool);
return tool.m_func;
}
void TestContext::setInnerMainFunc(const String& name, InnerMainFunc func)
{
SharedLibraryTool* tool = m_sharedLibTools.tryGetValue(name);
if (tool)
{
tool->m_sharedLibrary.setNull();
tool->m_func = func;
}
else
{
SharedLibraryTool tool = {};
tool.m_func = func;
m_sharedLibTools.add(name, tool);
}
}
TestContext::CleanDeviceCacheFunc TestContext::getCleanDeviceCacheFunc(const String& name)
{
SharedLibraryTool* tool = m_sharedLibTools.tryGetValue(name);
if (tool)
{
return tool->m_cleanDeviceCacheFunc;
}
return nullptr;
}
DownstreamCompilerSet* TestContext::getCompilerSet()
{
std::lock_guard<std::mutex> lock(mutex);
if (!compilerSet)
{
compilerSet = new DownstreamCompilerSet;
DownstreamCompilerLocatorFunc locators[int(SLANG_PASS_THROUGH_COUNT_OF)] = {nullptr};
DownstreamCompilerUtil::setDefaultLocators(locators);
for (Index i = 0; i < Index(SLANG_PASS_THROUGH_COUNT_OF); ++i)
{
auto locator = locators[i];
if (locator)
{
locator(String(), DefaultSharedLibraryLoader::getSingleton(), compilerSet);
}
}
DownstreamCompilerUtil::updateDefaults(compilerSet);
}
return compilerSet;
}
SlangResult TestContext::_createJSONRPCConnection(RefPtr<JSONRPCConnection>& out)
{
RefPtr<Process> process;
{
CommandLine cmdLine;
cmdLine.setExecutableLocation(ExecutableLocation(exeDirectoryPath, "test-server"));
if (options.ignoreAbortMsg)
{
cmdLine.addArg("-ignore-abort-msg");
}
SLANG_RETURN_ON_FAIL(Process::create(
cmdLine,
Process::Flag::AttachDebugger | Process::Flag::DisableStdErrRedirection,
process));
}
Stream* writeStream = process->getStream(StdStreamType::In);
RefPtr<BufferedReadStream> readStream(
new BufferedReadStream(process->getStream(StdStreamType::Out)));
RefPtr<BufferedReadStream> readErrStream(
new BufferedReadStream(process->getStream(StdStreamType::ErrorOut)));
RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, writeStream);
RefPtr<JSONRPCConnection> rpcConnection = new JSONRPCConnection;
SLANG_RETURN_ON_FAIL(
rpcConnection->init(connection, JSONRPCConnection::CallStyle::Default, process));
out = rpcConnection;
return SLANG_OK;
}
SlangResult TestContext::createLanguageServerJSONRPCConnection(RefPtr<JSONRPCConnection>& out)
{
RefPtr<Process> process;
{
CommandLine cmdLine;
cmdLine.setExecutableLocation(ExecutableLocation(exeDirectoryPath, "slangd"));
cmdLine.addArg("-periodic-diagnostic-update");
cmdLine.addArg("false");
SLANG_RETURN_ON_FAIL(Process::create(cmdLine, Process::Flag::AttachDebugger, process));
}
Stream* writeStream = process->getStream(StdStreamType::In);
RefPtr<BufferedReadStream> readStream(
new BufferedReadStream(process->getStream(StdStreamType::Out)));
RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, writeStream);
RefPtr<JSONRPCConnection> rpcConnection = new JSONRPCConnection;
SLANG_RETURN_ON_FAIL(
rpcConnection->init(connection, JSONRPCConnection::CallStyle::Object, process));
out = rpcConnection;
return SLANG_OK;
}
void TestContext::destroyRPCConnection()
{
if (m_jsonRpcConnections[slangTestThreadIndex])
{
m_jsonRpcConnections[slangTestThreadIndex]->disconnect();
m_jsonRpcConnections[slangTestThreadIndex].setNull();
}
}
Slang::JSONRPCConnection* TestContext::getOrCreateJSONRPCConnection()
{
if (!m_jsonRpcConnections[slangTestThreadIndex])
{
if (SLANG_FAILED(_createJSONRPCConnection(m_jsonRpcConnections[slangTestThreadIndex])))
{
return nullptr;
}
}
return m_jsonRpcConnections[slangTestThreadIndex];
}
Slang::IDownstreamCompiler* TestContext::getDefaultCompiler(SlangSourceLanguage sourceLanguage)
{
DownstreamCompilerSet* set = getCompilerSet();
return set ? set->getDefaultCompiler(sourceLanguage) : nullptr;
}
bool TestContext::canRunTestWithRenderApiFlags(Slang::RenderApiFlags requiredFlags)
{
// If only allow tests that use API - then the requiredFlags must be 0
if (options.apiOnly && requiredFlags == 0)
{
return false;
}
// Are the required rendering APIs enabled from the -api command line switch
return (requiredFlags & options.enabledApis) == requiredFlags;
}
SpawnType TestContext::getFinalSpawnType(SpawnType spawnType)
{
if (spawnType == SpawnType::Default)
{
if (options.outputMode == TestOutputMode::Default)
{
return SpawnType::UseSharedLibrary;
}
else
{
return SpawnType::UseTestServer;
}
}
// Just return whatever spawnType was passed in
return spawnType;
}
SpawnType TestContext::getFinalSpawnType()
{
return getFinalSpawnType(options.defaultSpawnType);
}
|