2021-06-09 14:42:47 -07:00
|
|
|
#include <c10/util/irange.h>
|
2021-06-24 12:37:29 -07:00
|
|
|
#include "StoreTestCommon.hpp"
|
2019-10-31 13:52:41 -07:00
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <thread>
|
|
|
|
|
|
2022-09-30 02:04:29 +00:00
|
|
|
#include <c10d/HashStore.hpp>
|
|
|
|
|
#include <c10d/PrefixStore.hpp>
|
2019-10-31 13:52:41 -07:00
|
|
|
|
2021-03-15 13:21:12 -07:00
|
|
|
constexpr int64_t kShortStoreTimeoutMillis = 100;
|
|
|
|
|
|
2020-09-09 17:13:36 -07:00
|
|
|
void testGetSet(std::string prefix = "") {
|
2019-10-31 13:52:41 -07:00
|
|
|
// Basic set/get
|
|
|
|
|
{
|
2020-11-11 22:49:06 -08:00
|
|
|
auto hashStore = c10::make_intrusive<c10d::HashStore>();
|
2019-10-31 13:52:41 -07:00
|
|
|
c10d::PrefixStore store(prefix, hashStore);
|
|
|
|
|
c10d::test::set(store, "key0", "value0");
|
|
|
|
|
c10d::test::set(store, "key1", "value1");
|
|
|
|
|
c10d::test::set(store, "key2", "value2");
|
|
|
|
|
c10d::test::check(store, "key0", "value0");
|
|
|
|
|
c10d::test::check(store, "key1", "value1");
|
|
|
|
|
c10d::test::check(store, "key2", "value2");
|
2021-03-12 12:36:11 -08:00
|
|
|
|
|
|
|
|
// Check compareSet, does not check return value
|
2021-04-29 13:55:42 -07:00
|
|
|
c10d::test::compareSet(store, "key0", "wrongExpectedValue", "newValue");
|
2021-03-12 12:36:11 -08:00
|
|
|
c10d::test::check(store, "key0", "value0");
|
|
|
|
|
c10d::test::compareSet(store, "key0", "value0", "newValue");
|
|
|
|
|
c10d::test::check(store, "key0", "newValue");
|
|
|
|
|
|
2020-10-14 11:56:05 -07:00
|
|
|
auto numKeys = store.getNumKeys();
|
|
|
|
|
EXPECT_EQ(numKeys, 3);
|
2020-10-14 11:56:05 -07:00
|
|
|
auto delSuccess = store.deleteKey("key0");
|
|
|
|
|
EXPECT_TRUE(delSuccess);
|
|
|
|
|
numKeys = store.getNumKeys();
|
|
|
|
|
EXPECT_EQ(numKeys, 2);
|
|
|
|
|
auto delFailure = store.deleteKey("badKeyName");
|
|
|
|
|
EXPECT_FALSE(delFailure);
|
2021-03-15 13:21:12 -07:00
|
|
|
auto timeout = std::chrono::milliseconds(kShortStoreTimeoutMillis);
|
|
|
|
|
store.setTimeout(timeout);
|
2020-10-14 11:56:05 -07:00
|
|
|
EXPECT_THROW(store.get("key0"), std::runtime_error);
|
2019-10-31 13:52:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get() waits up to timeout_.
|
|
|
|
|
{
|
2020-11-11 22:49:06 -08:00
|
|
|
auto hashStore = c10::make_intrusive<c10d::HashStore>();
|
2019-10-31 13:52:41 -07:00
|
|
|
c10d::PrefixStore store(prefix, hashStore);
|
|
|
|
|
std::thread th([&]() { c10d::test::set(store, "key0", "value0"); });
|
|
|
|
|
c10d::test::check(store, "key0", "value0");
|
|
|
|
|
th.join();
|
|
|
|
|
}
|
2020-09-09 17:13:36 -07:00
|
|
|
}
|
2019-10-31 13:52:41 -07:00
|
|
|
|
2020-09-09 17:13:36 -07:00
|
|
|
void stressTestStore(std::string prefix = "") {
|
|
|
|
|
// Hammer on HashStore::add
|
2019-10-31 13:52:41 -07:00
|
|
|
const auto numThreads = 4;
|
|
|
|
|
const auto numIterations = 100;
|
2020-09-09 17:13:36 -07:00
|
|
|
|
|
|
|
|
std::vector<std::thread> threads;
|
2019-10-31 13:52:41 -07:00
|
|
|
c10d::test::Semaphore sem1, sem2;
|
2020-11-11 22:49:06 -08:00
|
|
|
auto hashStore = c10::make_intrusive<c10d::HashStore>();
|
2019-10-31 13:52:41 -07:00
|
|
|
c10d::PrefixStore store(prefix, hashStore);
|
2020-09-09 17:13:36 -07:00
|
|
|
|
2021-12-09 21:59:50 -08:00
|
|
|
for (C10_UNUSED const auto i : c10::irange(numThreads)) {
|
|
|
|
|
threads.emplace_back(std::thread([&] {
|
2019-10-31 13:52:41 -07:00
|
|
|
sem1.post();
|
|
|
|
|
sem2.wait();
|
2021-12-09 21:59:50 -08:00
|
|
|
for (C10_UNUSED const auto j : c10::irange(numIterations)) {
|
2019-10-31 13:52:41 -07:00
|
|
|
store.add("counter", 1);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
2020-09-09 17:13:36 -07:00
|
|
|
|
2019-10-31 13:52:41 -07:00
|
|
|
sem1.wait(numThreads);
|
|
|
|
|
sem2.post(numThreads);
|
2020-09-09 17:13:36 -07:00
|
|
|
|
2019-10-31 13:52:41 -07:00
|
|
|
for (auto& thread : threads) {
|
|
|
|
|
thread.join();
|
|
|
|
|
}
|
|
|
|
|
std::string expected = std::to_string(numThreads * numIterations);
|
|
|
|
|
c10d::test::check(store, "counter", expected);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-09 17:13:36 -07:00
|
|
|
TEST(HashStoreTest, testGetAndSet) {
|
|
|
|
|
testGetSet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(HashStoreTest, testGetAndSetWithPrefix) {
|
|
|
|
|
testGetSet("testPrefix");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(HashStoreTest, testStressStore) {
|
|
|
|
|
stressTestStore();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(HashStoreTest, testStressStoreWithPrefix) {
|
|
|
|
|
stressTestStore("testPrefix");
|
2019-10-31 13:52:41 -07:00
|
|
|
}
|