diff options
| author | Per Larsson <[email protected]> | 2021-11-02 10:50:18 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-11-02 10:50:18 +0100 |
| commit | 4b8d4c0e375c729e38bdaadfebf0eaf14f08f5f9 (patch) | |
| tree | d1e6fb1c71a0c8c5e12b2814f309d09d7093c9b3 /zenserver-test/zenserver-test.cpp | |
| parent | Merge branch 'main' into zcache-batch (diff) | |
| download | zen-4b8d4c0e375c729e38bdaadfebf0eaf14f08f5f9.tar.xz zen-4b8d4c0e375c729e38bdaadfebf0eaf14f08f5f9.zip | |
Added upstream batch API.
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 156 |
1 files changed, 138 insertions, 18 deletions
diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 876979914..70515770c 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -464,7 +464,9 @@ using namespace std::literals; class full_test_formatter final : public spdlog::formatter { public: - full_test_formatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch) : m_Epoch(Epoch), m_LogId(LogId) {} + full_test_formatter(std::string_view LogId, std::chrono::time_point<std::chrono::system_clock> Epoch) : m_Epoch(Epoch), m_LogId(LogId) + { + } virtual std::unique_ptr<formatter> clone() const override { return std::make_unique<full_test_formatter>(m_LogId, m_Epoch); } @@ -1703,8 +1705,8 @@ TEST_CASE("zcache.policy") LocalCfg.Spawn(LocalInst); - zen::IoHash Key; - zen::IoHash PayloadId; + zen::IoHash Key; + zen::IoHash PayloadId; // Store package locally { @@ -1761,8 +1763,8 @@ TEST_CASE("zcache.policy") UpstreamCfg.Spawn(UpstreamInst); LocalCfg.Spawn(LocalInst); - zen::IoHash Key; - zen::IoHash PayloadId; + zen::IoHash Key; + zen::IoHash PayloadId; // Store package upstream { @@ -1927,10 +1929,10 @@ TEST_CASE("zcache.batch") Inst.SpawnServer(PortNumber); Inst.WaitUntilReady(); - const std::string_view Bucket = "mybucket"sv; + const std::string_view Bucket = "mastodon"sv; const uint32_t BatchCount = 128; - // Create cachereords + // Create cach records { for (uint32_t Key = 1; Key <= BatchCount; ++Key) { @@ -1948,20 +1950,27 @@ TEST_CASE("zcache.batch") // Get all as a batch { - CbObjectWriter BatchQuery; + CbObjectWriter BatchRequest; + + BatchRequest << "method" + << "getcacherecords"; + + BatchRequest.BeginObject("params"); + BatchRequest.BeginArray("cachekeys"sv); - BatchQuery.BeginArray("records"sv); for (uint32_t Key = 1; Key <= BatchCount; ++Key) { const IoHash CacheKey = CreateCacheKey(Key); - BatchQuery.BeginObject(); - BatchQuery << "bucket"sv << Bucket << "key" << CacheKey; - BatchQuery.EndObject(); + BatchRequest.BeginObject(); + BatchRequest << "bucket"sv << Bucket << "key" << CacheKey; + BatchRequest.EndObject(); } - BatchQuery.EndArray(); + + BatchRequest.EndArray(); + BatchRequest.EndObject(); zen::BinaryWriter Payload; - BatchQuery.Save(Payload); + BatchRequest.Save(Payload); cpr::Response Result = cpr::Get(cpr::Url{"{}/$batch"_format(BaseUri)}, cpr::Header{{"Content-Type", "application/x-ue-cb"}, {"Accept", "application/x-ue-cbpkg"}}, @@ -1972,16 +1981,127 @@ TEST_CASE("zcache.batch") zen::IoBuffer Response(zen::IoBuffer::Wrap, Result.text.data(), Result.text.size()); zen::CbPackage Package; const bool Ok = Package.TryLoad(Response); + CHECK(Ok); + + size_t ReturnCount = 0; CbObjectView BatchResponse = Package.GetObject(); - for (uint32_t ExpectedKey = 1; CbFieldView ResponseView : BatchResponse["records"]) + for (uint32_t ExpectedKey = 1; CbFieldView ResponseView : BatchResponse["result"]) { - CbObjectView Response = ResponseView.AsObjectView(); - const uint32_t Key = Response["key"sv].AsUInt32(); - const IoHash Attachment = Response["data"sv].AsHash(); + CbObjectView Response = ResponseView.AsObjectView(); + const uint32_t Key = Response["key"sv].AsUInt32(); + const IoHash AttachmentHash = Response["data"sv].AsHash(); + const CbAttachment* Attachment = Package.FindAttachment(AttachmentHash); + CHECK(Key == ExpectedKey); + CHECK(Attachment != nullptr); + ExpectedKey++; + ReturnCount++; + } + + CHECK(ReturnCount == BatchCount); + } + } + + SUBCASE("get missing cache records") + { + std::filesystem::path TestDir = TestEnv.CreateNewTestDir(); + const uint16_t PortNumber = 13337; + const auto BaseUri = "http://localhost:{}/z$"_format(PortNumber); + + ZenServerInstance Inst(TestEnv); + Inst.SetTestDir(TestDir); + Inst.SpawnServer(PortNumber); + Inst.WaitUntilReady(); + + const std::string_view Bucket = "mastodon"sv; + + // Create some cache records + { + for (uint32_t Key = 1; Key <= 5; ++Key) + { + const IoHash CacheKey = CreateCacheKey(Key); + CbPackage CacheRecord = CreateCacheRecord(Key, 4096); + IoBuffer Payload = ToIoBuffer(CacheRecord); + + cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(BaseUri, Bucket, CacheKey)}, + cpr::Body{(const char*)Payload.Data(), Payload.Size()}, + cpr::Header{{"Content-Type", "application/x-ue-cbpkg"}}); + + CHECK(Result.status_code == 201); + } + } + + { + CbObjectWriter BatchRequest; + + BatchRequest << "method" + << "getcacherecords"; + + BatchRequest.BeginObject("params"); + BatchRequest.BeginArray("cachekeys"sv); + + BatchRequest.BeginObject(); + BatchRequest << "bucket"sv << Bucket << "key" << CreateCacheKey(1); + BatchRequest.EndObject(); + BatchRequest.BeginObject(); + BatchRequest << "bucket"sv << Bucket << "key" << CreateCacheKey(11); // Missing + BatchRequest.EndObject(); + BatchRequest.BeginObject(); + BatchRequest << "bucket"sv << Bucket << "key" << CreateCacheKey(2); + BatchRequest.EndObject(); + BatchRequest.BeginObject(); + BatchRequest << "bucket"sv << Bucket << "key" << CreateCacheKey(22); // Missing + BatchRequest.EndObject(); + + BatchRequest.EndArray(); + BatchRequest.EndObject(); + + zen::BinaryWriter Body; + BatchRequest.Save(Body); + + cpr::Response Result = cpr::Get(cpr::Url{"{}/$batch"_format(BaseUri)}, + cpr::Header{{"Content-Type", "application/x-ue-cb"}, {"Accept", "application/x-ue-cbpkg"}}, + cpr::Body{(const char*)Body.GetData(), Body.GetSize()}); + + CHECK(Result.status_code == 200); + + zen::IoBuffer Response(zen::IoBuffer::Wrap, Result.text.data(), Result.text.size()); + zen::CbPackage Package; + const bool Ok = Package.TryLoad(Response); + CHECK(Ok); + + CbObjectView BatchResponse = Package.GetObject(); + CbArrayView CacheRecords = BatchResponse["result"sv].AsArrayView(); + auto It = CacheRecords.CreateViewIterator(); + + { + CbObjectView CacheRecord = It.AsObjectView(); + const uint32_t Key = CacheRecord["key"sv].AsUInt32(); + const IoHash AttachmentHash = CacheRecord["data"sv].AsHash(); + const CbAttachment* Attachment = Package.FindAttachment(AttachmentHash); + + CHECK(Key == 1); + CHECK(Attachment != nullptr); } + + It++; + CHECK(It.IsNull()); + + It++; + { + CbObjectView CacheRecord = It.AsObjectView(); + const uint32_t Key = CacheRecord["key"sv].AsUInt32(); + const IoHash AttachmentHash = CacheRecord["data"sv].AsHash(); + const CbAttachment* Attachment = Package.FindAttachment(AttachmentHash); + + CHECK(Key == 2); + CHECK(Attachment != nullptr); + } + + It++; + CHECK(It.IsNull()); } } } |