aboutsummaryrefslogtreecommitdiff
path: root/zenserver-test/zenserver-test.cpp
diff options
context:
space:
mode:
authorPer Larsson <[email protected]>2021-11-12 11:08:53 +0100
committerPer Larsson <[email protected]>2021-11-12 11:08:53 +0100
commit3faf0b57c625152a8facfca1c4995bd9edc95707 (patch)
tree0ae1ff078147091bafb7f5f4799943cebd0a7f50 /zenserver-test/zenserver-test.cpp
parentChanged from batch to RPC. (diff)
downloadzen-3faf0b57c625152a8facfca1c4995bd9edc95707.tar.xz
zen-3faf0b57c625152a8facfca1c4995bd9edc95707.zip
Movec cache utility types to zenutil and fixed unit tests.
Diffstat (limited to 'zenserver-test/zenserver-test.cpp')
-rw-r--r--zenserver-test/zenserver-test.cpp166
1 files changed, 87 insertions, 79 deletions
diff --git a/zenserver-test/zenserver-test.cpp b/zenserver-test/zenserver-test.cpp
index fd1c010be..a5cfabe08 100644
--- a/zenserver-test/zenserver-test.cpp
+++ b/zenserver-test/zenserver-test.cpp
@@ -20,6 +20,7 @@
#include <zenhttp/httpclient.h>
#include <zenhttp/httpshared.h>
#include <zenhttp/zenhttp.h>
+#include <zenutil/cache/cache.h>
#include <zenutil/zenserverprocess.h>
#if ZEN_USE_MIMALLOC
@@ -1891,9 +1892,7 @@ TEST_CASE("zcache.batch")
{
using namespace std::literals;
- auto CreateCacheKey = [](uint32_t Id) -> zen::IoHash { return zen::IoHash::HashBuffer(&Id, sizeof(uint32_t)); };
-
- auto CreateCacheRecord = [](uint32_t Key, size_t PayloadSize) -> zen::CbPackage {
+ auto CreateCacheRecord = [](const zen::CacheKey& CacheKey, size_t PayloadSize) -> zen::CbPackage {
std::vector<uint8_t> Data;
Data.resize(PayloadSize);
for (size_t Idx = 0; Idx < PayloadSize; ++Idx)
@@ -1901,11 +1900,13 @@ TEST_CASE("zcache.batch")
Data[Idx] = Idx % 255;
}
- auto Compressed = zen::CompressedBuffer::Compress(SharedBuffer::MakeView(Data.data(), Data.size()));
- zen::CbAttachment Attachment(Compressed);
+ zen::CbAttachment Attachment(zen::CompressedBuffer::Compress(SharedBuffer::MakeView(Data.data(), Data.size())));
zen::CbObjectWriter CacheRecord;
- CacheRecord << "key" << Key << "data" << Attachment;
+ CacheRecord.BeginObject("CacheKey"sv);
+ CacheRecord << "Bucket"sv << CacheKey.Bucket << "Hash"sv << CacheKey.Hash;
+ CacheRecord.EndObject();
+ CacheRecord << "Data"sv << Attachment;
zen::CbPackage Package;
Package.SetObject(CacheRecord.Save());
@@ -1931,18 +1932,20 @@ TEST_CASE("zcache.batch")
Inst.SpawnServer(PortNumber);
Inst.WaitUntilReady();
- const std::string_view Bucket = "mastodon"sv;
- const uint32_t BatchCount = 128;
+ std::vector<zen::CacheKey> CacheKeys;
- // Create cach records
+ // Create some cache records
{
- for (uint32_t Key = 1; Key <= BatchCount; ++Key)
+ for (uint32_t Key = 1; Key <= 128; ++Key)
{
- const IoHash CacheKey = CreateCacheKey(Key);
- CbPackage CacheRecord = CreateCacheRecord(Key, 4096);
- IoBuffer Payload = ToIoBuffer(CacheRecord);
+ const CacheKey CacheKey = zen::CacheKey::Create("mastodon"sv, zen::IoHash::HashBuffer(&Key, sizeof uint32_t));
+ CbPackage CacheRecord = CreateCacheRecord(CacheKey, 1024);
+
+ CacheKeys.push_back(CacheKey);
- cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(BaseUri, Bucket, 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"}});
@@ -1954,27 +1957,26 @@ TEST_CASE("zcache.batch")
{
CbObjectWriter BatchRequest;
- BatchRequest << "method"
- << "get-cache-records";
+ BatchRequest << "Method"sv
+ << "GetCacheRecords"sv;
- BatchRequest.BeginObject("params");
- BatchRequest.BeginArray("cachekeys"sv);
+ BatchRequest.BeginObject("Params"sv);
- for (uint32_t Key = 1; Key <= BatchCount; ++Key)
+ BatchRequest.BeginArray("CacheKeys"sv);
+ for (const CacheKey& Key : CacheKeys)
{
- const IoHash CacheKey = CreateCacheKey(Key);
BatchRequest.BeginObject();
- BatchRequest << "bucket"sv << Bucket << "hash" << CacheKey;
+ BatchRequest << "Bucket"sv << Key.Bucket << "Hash"sv << Key.Hash;
BatchRequest.EndObject();
}
-
BatchRequest.EndArray();
+
BatchRequest.EndObject();
zen::BinaryWriter Payload;
BatchRequest.Save(Payload);
- cpr::Response Result = cpr::Post(cpr::Url{"{}/$batch"_format(BaseUri)},
+ 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()});
@@ -1985,24 +1987,26 @@ TEST_CASE("zcache.batch")
const bool Ok = Package.TryLoad(Response);
CHECK(Ok);
- size_t ReturnCount = 0;
-
+ size_t ReturnCount = 0;
CbObjectView BatchResponse = Package.GetObject();
- for (uint32_t ExpectedKey = 1; CbFieldView ResponseView : BatchResponse["result"])
+
+ for (size_t Index = 0; CbFieldView RecordView : BatchResponse["Result"])
{
- CbObjectView Response = ResponseView.AsObjectView();
- const uint32_t Key = Response["key"sv].AsUInt32();
- const IoHash AttachmentHash = Response["data"sv].AsHash();
+ const CacheKey& ExpectedKey = CacheKeys[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();
const CbAttachment* Attachment = Package.FindAttachment(AttachmentHash);
CHECK(Key == ExpectedKey);
CHECK(Attachment != nullptr);
- ExpectedKey++;
ReturnCount++;
}
- CHECK(ReturnCount == BatchCount);
+ CHECK(ReturnCount == CacheKeys.size());
}
}
@@ -2017,17 +2021,20 @@ TEST_CASE("zcache.batch")
Inst.SpawnServer(PortNumber);
Inst.WaitUntilReady();
- const std::string_view Bucket = "mastodon"sv;
+ std::vector<zen::CacheKey> CacheKeys;
// Create some cache records
{
- for (uint32_t Key = 1; Key <= 5; ++Key)
+ for (uint32_t Key = 1; Key <= 4; ++Key)
{
- const IoHash CacheKey = CreateCacheKey(Key);
- CbPackage CacheRecord = CreateCacheRecord(Key, 4096);
- IoBuffer Payload = ToIoBuffer(CacheRecord);
+ const CacheKey CacheKey = zen::CacheKey::Create("mastodon"sv, zen::IoHash::HashBuffer(&Key, sizeof uint32_t));
+ CbPackage CacheRecord = CreateCacheRecord(CacheKey, 1024);
+
+ CacheKeys.push_back(CacheKey);
- cpr::Response Result = cpr::Put(cpr::Url{"{}/{}/{}"_format(BaseUri, Bucket, 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"}});
@@ -2035,37 +2042,37 @@ TEST_CASE("zcache.batch")
}
}
+ // Get all records with every other missing
{
CbObjectWriter BatchRequest;
- BatchRequest << "method"
- << "get-cache-records";
-
- BatchRequest.BeginObject("params");
- BatchRequest.BeginArray("cachekeys"sv);
+ BatchRequest << "Method"sv
+ << "GetCacheRecords"sv;
- BatchRequest.BeginObject();
- BatchRequest << "bucket"sv << Bucket << "hash" << CreateCacheKey(1);
- BatchRequest.EndObject();
- BatchRequest.BeginObject();
- BatchRequest << "bucket"sv << Bucket << "hash" << CreateCacheKey(11); // Missing
- BatchRequest.EndObject();
- BatchRequest.BeginObject();
- BatchRequest << "bucket"sv << Bucket << "hash" << CreateCacheKey(2);
- BatchRequest.EndObject();
- BatchRequest.BeginObject();
- BatchRequest << "bucket"sv << Bucket << "hash" << CreateCacheKey(22); // Missing
- BatchRequest.EndObject();
+ BatchRequest.BeginObject("Params"sv);
+ BatchRequest.BeginArray("CacheKeys"sv);
+ for (const CacheKey& Key : CacheKeys)
+ {
+ 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();
+ }
BatchRequest.EndArray();
+
BatchRequest.EndObject();
- zen::BinaryWriter Body;
- BatchRequest.Save(Body);
+ zen::BinaryWriter Payload;
+ BatchRequest.Save(Payload);
- cpr::Response Result = cpr::Post(cpr::Url{"{}/$batch"_format(BaseUri)},
+ 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*)Body.GetData(), Body.GetSize()});
+ cpr::Body{(const char*)Payload.GetData(), Payload.GetSize()});
CHECK(Result.status_code == 200);
@@ -2075,35 +2082,36 @@ TEST_CASE("zcache.batch")
CHECK(Ok);
CbObjectView BatchResponse = Package.GetObject();
- CbArrayView CacheRecords = BatchResponse["result"sv].AsArrayView();
- auto It = CacheRecords.CreateViewIterator();
+ size_t KeyIndex = 0;
+ size_t ReturnCount = 0;
+
+ for (size_t Index = 0; CbFieldView RecordView : BatchResponse["Result"])
{
- 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);
+ const bool Missing = Index++ % 2 != 0;
- CHECK(Key == 1);
- CHECK(Attachment != nullptr);
- }
+ if (Missing)
+ {
+ CHECK(RecordView.IsNull());
+ }
+ else
+ {
+ const CacheKey& ExpectedKey = CacheKeys[KeyIndex++];
- It++;
- CHECK(It.IsNull());
+ 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);
- 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 == ExpectedKey);
+ CHECK(Attachment != nullptr);
+ }
- CHECK(Key == 2);
- CHECK(Attachment != nullptr);
+ ReturnCount++;
}
- It++;
- CHECK(It.IsNull());
+ CHECK(ReturnCount == (CacheKeys.size() * 2));
}
}
}