diff options
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 128 |
1 files changed, 76 insertions, 52 deletions
diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 0878678d8..0dc0c13dd 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -41,16 +41,19 @@ ZEN_THIRD_PARTY_INCLUDES_START #undef GetObject ZEN_THIRD_PARTY_INCLUDES_END -#include <ppl.h> #include <atomic> #include <filesystem> #include <map> #include <random> #include <span> +#include <thread> #include <unordered_map> -#include <atlbase.h> -#include <process.h> +#if ZEN_PLATFORM_WINDOWS +# include <ppl.h> +# include <atlbase.h> +# include <process.h> +#endif #include <asio.hpp> @@ -69,6 +72,25 @@ ZEN_THIRD_PARTY_INCLUDES_END using namespace fmt::literals; using namespace std::literals; +#if ZEN_PLATFORM_LINUX || ZEN_PLATFORM_MAC +struct Concurrency +{ + template <typename... T> + static void parallel_invoke(T&&... t) + { + constexpr size_t NumTs = sizeof...(t); + std::thread Threads[NumTs] = { + std::thread(std::forward<T>(t))..., + }; + + for (std::thread& Thread : Threads) + { + Thread.join(); + } + } +}; +#endif + /* ___ ___ _________ _________ ________ ________ ___ ___ _______ ________ _________ @@ -141,7 +163,17 @@ public: private: void Reset() {} - void OnError(const std::error_code& Error) { ZEN_ERROR("HTTP client error! '{}'", Error.message()); } + void OnError(const asio::error_code& Error) + { + // Let EOF errors proceed. They're raised when sockets close. + if (Error == asio::error::eof) + { + return; + } + + using namespace fmt::literals; + zen::ThrowLastError("HTTP client error! '{}'"_format(Error.message())); + } int OnHeader(const char* Data, size_t Bytes) { @@ -183,7 +215,7 @@ private: // Send initial request payload asio::async_write(m_Socket, asio::const_buffer(m_RequestBody.data(), m_RequestBody.size()), - [this](const std::error_code& Error, size_t Bytes) { + [this](const asio::error_code& Error, size_t Bytes) { ZEN_UNUSED(Bytes); if (Error) { @@ -196,7 +228,7 @@ private: void OnRequestWritten() { - asio::async_read(m_Socket, m_ResponseBuffer, asio::transfer_at_least(1), [this](const std::error_code& Error, size_t Bytes) { + asio::async_read(m_Socket, m_ResponseBuffer, asio::transfer_at_least(1), [this](const asio::error_code& Error, size_t Bytes) { if (Error) { return OnError(Error); @@ -227,7 +259,7 @@ private: asio::async_read(m_Socket, m_ResponseBuffer, asio::transfer_at_least(1), - [this](const std::error_code& Error, size_t Bytes) { + [this](const asio::error_code& Error, size_t Bytes) { if (Error) { return OnError(Error); @@ -310,6 +342,8 @@ HttpConnectionPool::~HttpConnectionPool() std::unique_ptr<HttpClientConnection> HttpConnectionPool::GetConnection() { + using namespace fmt::literals; + zen::RwLock::ExclusiveLockScope ScopedLock(m_Lock); if (m_AvailableConnections.empty()) @@ -319,13 +353,13 @@ HttpConnectionPool::GetConnection() asio::ip::tcp::resolver Resolver{m_Context}; - std::error_code ErrCode; - auto it = Resolver.resolve(m_HostName, Service, ErrCode); - auto itEnd = asio::ip::tcp::resolver::iterator(); + asio::error_code ErrCode; + auto it = Resolver.resolve(m_HostName, Service, ErrCode); + auto itEnd = asio::ip::tcp::resolver::iterator(); if (ErrCode) { - return nullptr; + zen::ThrowLastError("Unabled to resolve '{}'"_format(m_HostName)); } asio::ip::tcp::socket Socket{m_Context}; @@ -333,7 +367,7 @@ HttpConnectionPool::GetConnection() if (ErrCode) { - return nullptr; + zen::ThrowLastError("Failed connecting '{}:{}'"_format(m_HostName, m_Port)); } return std::make_unique<HttpClientConnection>(m_Context, this, std::move(Socket)); @@ -341,7 +375,7 @@ HttpConnectionPool::GetConnection() std::unique_ptr<HttpClientConnection> Connection{m_AvailableConnections.back()}; m_AvailableConnections.pop_back(); - return std::move(Connection); + return Connection; } void @@ -740,7 +774,7 @@ TEST_CASE("default.single") auto IssueTestRequests = [&] { const uint64_t BatchNo = BatchCounter.fetch_add(1); - const DWORD ThreadId = GetCurrentThreadId(); + const int ThreadId = zen::GetCurrentThreadId(); ZEN_INFO("query batch {} started (thread {})", BatchNo, ThreadId); cpr::Session cli; @@ -754,22 +788,8 @@ TEST_CASE("default.single") ZEN_INFO("query batch {} ended (thread {})", BatchNo, ThreadId); }; - auto fun10 = [&] { - Concurrency::parallel_invoke(IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests, - IssueTestRequests); - }; - zen::Stopwatch timer; - // Concurrency::parallel_invoke(fun10, fun10, fun, fun, fun, fun, fun, fun, fun, fun); Concurrency::parallel_invoke(IssueTestRequests, IssueTestRequests, IssueTestRequests, @@ -808,7 +828,7 @@ TEST_CASE("multi.basic") auto IssueTestRequests = [&](int PortNumber) { const uint64_t BatchNo = BatchCounter.fetch_add(1); - const DWORD ThreadId = GetCurrentThreadId(); + const int ThreadId = zen::GetCurrentThreadId(); ZEN_INFO("query batch {} started (thread {}) for port {}", BatchNo, ThreadId, PortNumber); @@ -960,6 +980,10 @@ TEST_CASE("project.basic") zen::StringBuilder<64> BaseUri; BaseUri << "http://localhost:{}/prj/test"_format(PortNumber); + std::filesystem::path BinPath = zen::GetRunningExecutablePath(); + std::filesystem::path RootPath = BinPath.parent_path().parent_path(); + BinPath = BinPath.lexically_relative(RootPath); + SUBCASE("build store init") { { @@ -968,7 +992,7 @@ TEST_CASE("project.basic") Body << "id" << "test"; Body << "root" - << "/zooom"; + << RootPath.c_str(); Body << "project" << "/zooom"; Body << "engine" @@ -988,7 +1012,7 @@ TEST_CASE("project.basic") zen::CbObjectView ResponseObject = zen::CbFieldView(Response.text.data()).AsObjectView(); CHECK(ResponseObject["id"].AsString() == "test"sv); - CHECK(ResponseObject["root"].AsString() == "/zooom"sv); + CHECK(ResponseObject["root"].AsString() == PathToUtf8(RootPath.c_str())); } } @@ -1030,13 +1054,11 @@ TEST_CASE("project.basic") "00010000"}; auto FileOid = zen::Oid::FromHexString(ChunkId); - std::filesystem::path ReliablePath = zen::GetRunningExecutablePath(); - OpWriter.BeginArray("files"); OpWriter.BeginObject(); OpWriter << "id" << FileOid; - OpWriter << "clientpath" << ReliablePath.c_str(); - OpWriter << "serverpath" << ReliablePath.c_str(); + OpWriter << "clientpath" << "/{engine}/client/side/path"; + OpWriter << "serverpath" << BinPath.c_str(); OpWriter.EndObject(); OpWriter.EndArray(); @@ -1351,14 +1373,13 @@ TEST_CASE("zcache.cbpackage") ZenServerInstance RemoteInstance(TestEnv); RemoteInstance.SetTestDir(RemoteDataDir); RemoteInstance.SpawnServer(RemotePortNumber); + RemoteInstance.WaitUntilReady(); ZenServerInstance LocalInstance(TestEnv); LocalInstance.SetTestDir(LocalDataDir); LocalInstance.SpawnServer(LocalPortNumber, "--upstream-thread-count=0 --upstream-zen-url=http://localhost:{}"_format(RemotePortNumber)); - LocalInstance.WaitUntilReady(); - RemoteInstance.WaitUntilReady(); const std::string_view Bucket = "mosdef"sv; zen::IoHash Key; @@ -1415,14 +1436,13 @@ TEST_CASE("zcache.cbpackage") ZenServerInstance RemoteInstance(TestEnv); RemoteInstance.SetTestDir(RemoteDataDir); RemoteInstance.SpawnServer(RemotePortNumber); + RemoteInstance.WaitUntilReady(); ZenServerInstance LocalInstance(TestEnv); LocalInstance.SetTestDir(LocalDataDir); LocalInstance.SpawnServer(LocalPortNumber, "--upstream-thread-count=0 --upstream-zen-url=http://localhost:{}"_format(RemotePortNumber)); - LocalInstance.WaitUntilReady(); - RemoteInstance.WaitUntilReady(); const std::string_view Bucket = "mosdef"sv; zen::IoHash Key; @@ -1745,6 +1765,8 @@ TEST_CASE("zcache.policy") const bool Ok = Package.TryLoad(Body); CHECK(Ok); + CHECK(Ok); + CbObject CacheRecord = Package.GetObject(); std::vector<IoHash> AttachmentKeys; @@ -1765,6 +1787,7 @@ TEST_CASE("zcache.policy") const bool Ok = Package.TryLoad(Body); CHECK(Ok); + CHECK(Ok); CHECK(Package.GetAttachments().size() != 0); } } @@ -1805,6 +1828,8 @@ TEST_CASE("zcache.policy") const bool Ok = Package.TryLoad(Body); CHECK(Ok); + CHECK(Ok); + CbObject CacheRecord = Package.GetObject(); std::vector<IoHash> AttachmentKeys; @@ -1825,6 +1850,7 @@ TEST_CASE("zcache.policy") const bool Ok = Package.TryLoad(Body); CHECK(Ok); + CHECK(Ok); CHECK(Package.GetAttachments().size() != 0); } } @@ -1946,7 +1972,7 @@ TEST_CASE("zcache.rpc") for (uint32_t Key = 1; Key <= Num; ++Key) { - const zen::CacheKey CacheKey = zen::CacheKey::Create(Bucket, zen::IoHash::HashBuffer(&Key, sizeof uint32_t)); + const zen::CacheKey CacheKey = zen::CacheKey::Create(Bucket, zen::IoHash::HashBuffer(&Key, sizeof(uint32_t))); CbPackage CacheRecord = CreateCacheRecord(CacheKey, PayloadSize); OutKeys.push_back(CacheKey); @@ -2013,8 +2039,6 @@ TEST_CASE("zcache.rpc") for (CbFieldView RecordView : ResponseObject["Result"]) { - ExtendableStringBuilder<256> Tmp; - auto JSON = RecordView.AsObjectView().ToJson(Tmp).ToView(); OutResult.Records.push_back(RecordView); } @@ -2103,7 +2127,6 @@ TEST_CASE("zcache.rpc") { const CacheKey& ExpectedKey = ExistingKeys[KeyIndex++]; CbObjectView RecordObj = RecordView.AsObjectView(); - CbObjectView KeyObj = RecordObj["CacheKey"sv].AsObjectView(); zen::CacheKey Key = LoadKey(RecordObj["CacheKey"sv]); const IoHash AttachmentHash = RecordObj["Data"sv].AsHash(); const CbAttachment* Attachment = Result.Response.FindAttachment(AttachmentHash); @@ -2141,7 +2164,6 @@ TEST_CASE("zcache.rpc") CbObjectView RecordObj = RecordView.AsObjectView(); CbObjectView KeyObj = RecordObj["CacheKey"sv].AsObjectView(); const CacheKey Key = CacheKey::Create(KeyObj["Bucket"sv].AsString(), KeyObj["Hash"].AsHash()); - const IoHash AttachmentHash = RecordObj["Data"sv].AsHash(); CHECK(Key == ExpectedKey); } @@ -2225,7 +2247,7 @@ struct RemoteExecutionRequest for (const auto& Kv : m_Visit.m_Files) { PrepReq.BeginObject(); - PrepReq << "file" << zen::WideToUtf8(Kv.first) << "size" << Kv.second.Size << "hash" << Kv.second.Hash; + PrepReq << "file" << zen::PathToUtf8(Kv.first) << "size" << Kv.second.Size << "hash" << Kv.second.Hash; PrepReq.EndObject(); } PrepReq.EndArray(); @@ -2248,7 +2270,7 @@ struct RemoteExecutionRequest if (auto It = m_Visit.m_HashToFile.find(NeedHash); It != m_Visit.m_HashToFile.end()) { - zen::IoBuffer FileData = zen::IoBufferBuilder::MakeFromFile(It->second.c_str()); + zen::IoBuffer FileData = zen::IoBufferBuilder::MakeFromFile(It->second); cpr::Response CasResponse = cpr::Post(cpr::Url(m_CasUri), cpr::Body((const char*)FileData.Data(), FileData.Size())); @@ -2286,7 +2308,7 @@ private: Visitor(const std::filesystem::path& RootPath) : m_RootPath(RootPath) {} - virtual void VisitFile(const std::filesystem::path& Parent, const std::wstring_view& FileName, uint64_t FileSize) override + virtual void VisitFile(const std::filesystem::path& Parent, const path_view& FileName, uint64_t FileSize) override { std::filesystem::path FullPath = Parent / FileName; @@ -2294,8 +2316,8 @@ private: zen::ScanFile(FullPath, 64 * 1024, [&](const void* Data, size_t Size) { Ios.Append(Data, Size); }); zen::IoHash Hash = Ios.GetHash(); - std::wstring RelativePath = FullPath.lexically_relative(m_RootPath).native(); - // ZEN_INFO("File: {:32} => {} ({})", zen::WideToUtf8(RelativePath), Hash, FileSize); + auto RelativePath = FullPath.lexically_relative(m_RootPath).native(); + // ZEN_INFO("File: {:32} => {} ({})", zen::PathToUtf8(RelativePath), Hash, FileSize); FileEntry& Entry = m_Files[RelativePath]; Entry.Hash = Hash; @@ -2304,11 +2326,11 @@ private: m_HashToFile[Hash] = FullPath; } - virtual bool VisitDirectory(const std::filesystem::path& Parent, const std::wstring_view& DirectoryName) override + virtual bool VisitDirectory(const std::filesystem::path& Parent, const path_view& DirectoryName) override { std::filesystem::path FullPath = Parent / DirectoryName; - if (DirectoryName.starts_with(L".")) + if (DirectoryName.starts_with('.')) { return false; } @@ -2322,7 +2344,7 @@ private: zen::IoHash Hash; }; - std::map<std::wstring, FileEntry> m_Files; + std::map<std::filesystem::path::string_type, FileEntry> m_Files; std::unordered_map<zen::IoHash, std::filesystem::path, zen::IoHash::Hasher> m_HashToFile; }; @@ -2337,6 +2359,7 @@ private: TEST_CASE("exec.basic") { +#if ZEN_WITH_COMPUTE_SERVICES using namespace std::literals; std::filesystem::path TestDir = TestEnv.CreateNewTestDir(); @@ -2367,6 +2390,7 @@ TEST_CASE("exec.basic") CHECK(Result["exitcode"].AsInt32(-1) == 1); } +#endif // ZEN_WITH_COMPUTE_SERVICES } TEST_CASE("mesh.basic") |