diff options
| author | Per Larsson <[email protected]> | 2021-11-12 15:03:22 +0100 |
|---|---|---|
| committer | Per Larsson <[email protected]> | 2021-11-12 15:03:22 +0100 |
| commit | d088f93b163b26aa059118275d4f36c0b53af08b (patch) | |
| tree | 1385985b48a9721a2304d887bdd61414fd99a317 /zenserver-test/zenserver-test.cpp | |
| parent | Added test for cache policy SkipAttachments. (diff) | |
| download | zen-d088f93b163b26aa059118275d4f36c0b53af08b.tar.xz zen-d088f93b163b26aa059118275d4f36c0b53af08b.zip | |
Cleanup test.
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
| -rw-r--r-- | zenserver-test/zenserver-test.cpp | 118 |
1 files changed, 31 insertions, 87 deletions
diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp index 7dea80a36..8f38cc1be 100644 --- a/zenserver-test/zenserver-test.cpp +++ b/zenserver-test/zenserver-test.cpp @@ -1900,7 +1900,7 @@ TEST_CASE("zcache.policy") } } -TEST_CASE("zcache.batch") +TEST_CASE("zcache.rpc") { using namespace std::literals; @@ -2072,97 +2072,41 @@ TEST_CASE("zcache.batch") Inst.SpawnServer(PortNumber); Inst.WaitUntilReady(); - std::vector<zen::CacheKey> CacheKeys; + CacheRecordPolicy Policy; + std::vector<zen::CacheKey> ExistingKeys = PutCacheRecords(BaseUri, ""sv, "mastodon"sv, 128); + std::vector<zen::CacheKey> Keys; - // Create some cache records + for (const zen::CacheKey& Key : ExistingKeys) { - for (uint32_t Key = 1; Key <= 4; ++Key) - { - const CacheKey CacheKey = zen::CacheKey::Create("mastodon"sv, zen::IoHash::HashBuffer(&Key, sizeof uint32_t)); - CbPackage CacheRecord = CreateCacheRecord(CacheKey, 1024); - - CacheKeys.push_back(CacheKey); - - IoBuffer Payload = ToIoBuffer(CacheRecord); - - cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(BaseUri, CacheKey.Bucket, CacheKey.Hash)}, - cpr::Body{(const char*)Payload.Data(), Payload.Size()}, - cpr::Header{{"Content-Type", "application/x-ue-cbpkg"}}); - - CHECK(Result.status_code == 201); - } + Keys.push_back(Key); + Keys.push_back(CacheKey::Create("missing"sv, IoHash::Zero)); } - // Get all records with every other missing - { - CbObjectWriter BatchRequest; + GetCacheRecordResult Result = GetCacheRecords(BaseUri, Keys, Policy); - BatchRequest << "Method"sv - << "GetCacheRecords"sv; + CHECK(Result.Records.size() == Keys.size()); - BatchRequest.BeginObject("Params"sv); + size_t KeyIndex = 0; + for (size_t Index = 0; CbFieldView RecordView : Result.Records) + { + const bool Missing = Index++ % 2 != 0; - BatchRequest.BeginArray("CacheKeys"sv); - for (const CacheKey& Key : CacheKeys) + if (Missing) { - BatchRequest.BeginObject(); - BatchRequest << "Bucket"sv << Key.Bucket << "Hash"sv << Key.Hash; - BatchRequest.EndObject(); - BatchRequest.BeginObject(); - BatchRequest << "Bucket"sv - << "missing"sv - << "Hash"sv << Key.Hash; - BatchRequest.EndObject(); + CHECK(RecordView.IsNull()); } - BatchRequest.EndArray(); - - BatchRequest.EndObject(); - - zen::BinaryWriter Payload; - BatchRequest.Save(Payload); - - cpr::Response Result = cpr::Post(cpr::Url{"{}/$rpc"_format(BaseUri)}, - cpr::Header{{"Content-Type", "application/x-ue-cb"}, {"Accept", "application/x-ue-cbpkg"}}, - cpr::Body{(const char*)Payload.GetData(), Payload.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(); - - size_t KeyIndex = 0; - size_t ReturnCount = 0; - - for (size_t Index = 0; CbFieldView RecordView : BatchResponse["Result"]) + else { - const bool Missing = Index++ % 2 != 0; - - if (Missing) - { - CHECK(RecordView.IsNull()); - } - else - { - const CacheKey& ExpectedKey = CacheKeys[KeyIndex++]; - - 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(); - const CbAttachment* Attachment = Package.FindAttachment(AttachmentHash); - - CHECK(Key == ExpectedKey); - CHECK(Attachment != nullptr); - } - - ReturnCount++; + 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); + + CHECK(Key == ExpectedKey); + CHECK(Attachment != nullptr); } - - CHECK(ReturnCount == (CacheKeys.size() * 2)); } } @@ -2182,7 +2126,7 @@ TEST_CASE("zcache.batch") GetCacheRecordResult Result = GetCacheRecords(BaseUri, Keys, Policy); CHECK(Result.Records.size() == Keys.size()); - + std::span<const zen::CbAttachment> Attachments = Result.Response.GetAttachments(); CHECK(Attachments.empty()); @@ -2190,10 +2134,10 @@ TEST_CASE("zcache.batch") { const CacheKey& ExpectedKey = Keys[Index++]; - 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(); + 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); } @@ -2245,8 +2189,8 @@ TEST_CASE("zcache.batch") for (size_t Index = 0; CbFieldView RecordView : Result.Response.GetObject()["Result"sv]) { - CbObjectView RecordObj = RecordView.AsObjectView(); const zen::CacheKey& ExpectedKey = Keys[Index++]; + CbObjectView RecordObj = RecordView.AsObjectView(); zen::CacheKey Key = LoadKey(RecordObj["CacheKey"sv]); CHECK(Key == ExpectedKey); } |