2016-06-03 15:15:16 +00:00
|
|
|
// Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2016-06-03 15:15:16 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2017-01-25 23:54:09 +00:00
|
|
|
#include "rocksdb/utilities/object_registry.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2016-06-03 15:15:16 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2016-06-03 15:15:16 +00:00
|
|
|
|
|
|
|
class EnvRegistryTest : public testing::Test {
|
|
|
|
public:
|
|
|
|
static int num_a, num_b;
|
|
|
|
};
|
|
|
|
|
|
|
|
int EnvRegistryTest::num_a = 0;
|
|
|
|
int EnvRegistryTest::num_b = 0;
|
2019-07-24 00:08:26 +00:00
|
|
|
static FactoryFunc<Env> test_reg_a = ObjectLibrary::Default()->Register<Env>(
|
|
|
|
"a://.*",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*env_guard*/,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
++EnvRegistryTest::num_a;
|
|
|
|
return Env::Default();
|
|
|
|
});
|
2016-06-03 15:15:16 +00:00
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
static FactoryFunc<Env> test_reg_b = ObjectLibrary::Default()->Register<Env>(
|
|
|
|
"b://.*", [](const std::string& /*uri*/, std::unique_ptr<Env>* env_guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
++EnvRegistryTest::num_b;
|
|
|
|
// Env::Default() is a singleton so we can't grant ownership directly to
|
|
|
|
// the caller - we must wrap it first.
|
|
|
|
env_guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return env_guard->get();
|
|
|
|
});
|
2016-06-03 15:15:16 +00:00
|
|
|
|
|
|
|
TEST_F(EnvRegistryTest, Basics) {
|
2019-07-24 00:08:26 +00:00
|
|
|
std::string msg;
|
2016-06-03 15:15:16 +00:00
|
|
|
std::unique_ptr<Env> env_guard;
|
2019-07-24 00:08:26 +00:00
|
|
|
auto registry = ObjectRegistry::NewInstance();
|
|
|
|
auto res = registry->NewObject<Env>("a://test", &env_guard, &msg);
|
2016-06-03 15:15:16 +00:00
|
|
|
ASSERT_NE(res, nullptr);
|
|
|
|
ASSERT_EQ(env_guard, nullptr);
|
|
|
|
ASSERT_EQ(1, num_a);
|
|
|
|
ASSERT_EQ(0, num_b);
|
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
res = registry->NewObject<Env>("b://test", &env_guard, &msg);
|
2016-06-03 15:15:16 +00:00
|
|
|
ASSERT_NE(res, nullptr);
|
|
|
|
ASSERT_NE(env_guard, nullptr);
|
|
|
|
ASSERT_EQ(1, num_a);
|
|
|
|
ASSERT_EQ(1, num_b);
|
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
res = registry->NewObject<Env>("c://test", &env_guard, &msg);
|
2016-06-03 15:15:16 +00:00
|
|
|
ASSERT_EQ(res, nullptr);
|
|
|
|
ASSERT_EQ(env_guard, nullptr);
|
|
|
|
ASSERT_EQ(1, num_a);
|
|
|
|
ASSERT_EQ(1, num_b);
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:08:26 +00:00
|
|
|
TEST_F(EnvRegistryTest, LocalRegistry) {
|
|
|
|
std::string msg;
|
|
|
|
std::unique_ptr<Env> guard;
|
|
|
|
auto registry = ObjectRegistry::NewInstance();
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectLibrary> library =
|
|
|
|
std::make_shared<ObjectLibrary>("local");
|
2019-07-24 00:08:26 +00:00
|
|
|
registry->AddLibrary(library);
|
|
|
|
library->Register<Env>(
|
|
|
|
"test-local",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*guard */,
|
|
|
|
std::string* /* errmsg */) { return Env::Default(); });
|
|
|
|
|
|
|
|
ObjectLibrary::Default()->Register<Env>(
|
|
|
|
"test-global",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*guard */,
|
|
|
|
std::string* /* errmsg */) { return Env::Default(); });
|
|
|
|
|
|
|
|
ASSERT_EQ(
|
|
|
|
ObjectRegistry::NewInstance()->NewObject<Env>("test-local", &guard, &msg),
|
|
|
|
nullptr);
|
|
|
|
ASSERT_NE(
|
|
|
|
ObjectRegistry::NewInstance()->NewObject("test-global", &guard, &msg),
|
|
|
|
nullptr);
|
|
|
|
ASSERT_NE(registry->NewObject<Env>("test-local", &guard, &msg), nullptr);
|
|
|
|
ASSERT_NE(registry->NewObject<Env>("test-global", &guard, &msg), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvRegistryTest, CheckShared) {
|
|
|
|
std::shared_ptr<Env> shared;
|
|
|
|
std::shared_ptr<ObjectRegistry> registry = ObjectRegistry::NewInstance();
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectLibrary> library =
|
|
|
|
std::make_shared<ObjectLibrary>("shared");
|
2019-07-24 00:08:26 +00:00
|
|
|
registry->AddLibrary(library);
|
|
|
|
library->Register<Env>(
|
|
|
|
"unguarded",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*guard */,
|
|
|
|
std::string* /* errmsg */) { return Env::Default(); });
|
|
|
|
|
|
|
|
library->Register<Env>(
|
|
|
|
"guarded", [](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT_OK(registry->NewSharedObject<Env>("guarded", &shared));
|
|
|
|
ASSERT_NE(shared, nullptr);
|
|
|
|
shared.reset();
|
|
|
|
ASSERT_NOK(registry->NewSharedObject<Env>("unguarded", &shared));
|
|
|
|
ASSERT_EQ(shared, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvRegistryTest, CheckStatic) {
|
|
|
|
Env* env = nullptr;
|
|
|
|
std::shared_ptr<ObjectRegistry> registry = ObjectRegistry::NewInstance();
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectLibrary> library =
|
|
|
|
std::make_shared<ObjectLibrary>("static");
|
2019-07-24 00:08:26 +00:00
|
|
|
registry->AddLibrary(library);
|
|
|
|
library->Register<Env>(
|
|
|
|
"unguarded",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*guard */,
|
|
|
|
std::string* /* errmsg */) { return Env::Default(); });
|
|
|
|
|
|
|
|
library->Register<Env>(
|
|
|
|
"guarded", [](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT_NOK(registry->NewStaticObject<Env>("guarded", &env));
|
|
|
|
ASSERT_EQ(env, nullptr);
|
|
|
|
env = nullptr;
|
|
|
|
ASSERT_OK(registry->NewStaticObject<Env>("unguarded", &env));
|
|
|
|
ASSERT_NE(env, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(EnvRegistryTest, CheckUnique) {
|
|
|
|
std::unique_ptr<Env> unique;
|
|
|
|
std::shared_ptr<ObjectRegistry> registry = ObjectRegistry::NewInstance();
|
2021-05-11 13:45:49 +00:00
|
|
|
std::shared_ptr<ObjectLibrary> library =
|
|
|
|
std::make_shared<ObjectLibrary>("unique");
|
2019-07-24 00:08:26 +00:00
|
|
|
registry->AddLibrary(library);
|
|
|
|
library->Register<Env>(
|
|
|
|
"unguarded",
|
|
|
|
[](const std::string& /*uri*/, std::unique_ptr<Env>* /*guard */,
|
|
|
|
std::string* /* errmsg */) { return Env::Default(); });
|
|
|
|
|
|
|
|
library->Register<Env>(
|
|
|
|
"guarded", [](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
|
|
|
|
ASSERT_OK(registry->NewUniqueObject<Env>("guarded", &unique));
|
|
|
|
ASSERT_NE(unique, nullptr);
|
|
|
|
unique.reset();
|
|
|
|
ASSERT_NOK(registry->NewUniqueObject<Env>("unguarded", &unique));
|
|
|
|
ASSERT_EQ(unique, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-05-11 13:45:49 +00:00
|
|
|
TEST_F(EnvRegistryTest, TestRegistryParents) {
|
|
|
|
auto grand = ObjectRegistry::Default();
|
|
|
|
auto parent = ObjectRegistry::NewInstance(); // parent with a grandparent
|
|
|
|
auto uncle = ObjectRegistry::NewInstance(grand);
|
|
|
|
auto child = ObjectRegistry::NewInstance(parent);
|
|
|
|
auto cousin = ObjectRegistry::NewInstance(uncle);
|
|
|
|
|
|
|
|
auto library = parent->AddLibrary("parent");
|
|
|
|
library->Register<Env>(
|
|
|
|
"parent", [](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
library = cousin->AddLibrary("cousin");
|
|
|
|
library->Register<Env>(
|
|
|
|
"cousin", [](const std::string& /*uri*/, std::unique_ptr<Env>* guard,
|
|
|
|
std::string* /* errmsg */) {
|
|
|
|
guard->reset(new EnvWrapper(Env::Default()));
|
|
|
|
return guard->get();
|
|
|
|
});
|
|
|
|
|
|
|
|
std::unique_ptr<Env> guard;
|
|
|
|
std::string msg;
|
|
|
|
|
|
|
|
// a:://* is registered in Default, so they should all workd
|
|
|
|
ASSERT_NE(parent->NewObject<Env>("a://test", &guard, &msg), nullptr);
|
|
|
|
ASSERT_NE(child->NewObject<Env>("a://test", &guard, &msg), nullptr);
|
|
|
|
ASSERT_NE(uncle->NewObject<Env>("a://test", &guard, &msg), nullptr);
|
|
|
|
ASSERT_NE(cousin->NewObject<Env>("a://test", &guard, &msg), nullptr);
|
|
|
|
|
|
|
|
// The parent env is only registered for parent, not uncle,
|
|
|
|
// So parent and child should return success and uncle and cousin should fail
|
|
|
|
ASSERT_OK(parent->NewUniqueObject<Env>("parent", &guard));
|
|
|
|
ASSERT_OK(child->NewUniqueObject<Env>("parent", &guard));
|
|
|
|
ASSERT_NOK(uncle->NewUniqueObject<Env>("parent", &guard));
|
|
|
|
ASSERT_NOK(cousin->NewUniqueObject<Env>("parent", &guard));
|
|
|
|
|
|
|
|
// The cousin is only registered in the cousin, so all of the others should
|
|
|
|
// fail
|
|
|
|
ASSERT_OK(cousin->NewUniqueObject<Env>("cousin", &guard));
|
|
|
|
ASSERT_NOK(parent->NewUniqueObject<Env>("cousin", &guard));
|
|
|
|
ASSERT_NOK(child->NewUniqueObject<Env>("cousin", &guard));
|
|
|
|
ASSERT_NOK(uncle->NewUniqueObject<Env>("cousin", &guard));
|
|
|
|
}
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2016-06-03 15:15:16 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // ROCKSDB_LITE
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 00:19:57 +00:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2016-06-03 15:15:16 +00:00
|
|
|
fprintf(stderr, "SKIPPED as EnvRegistry is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ROCKSDB_LITE
|