Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing: make factory registries support injection #1976

Merged
merged 7 commits into from
Nov 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions include/envoy/registry/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
namespace Envoy {
namespace Registry {

// Forward declaration of test class for friend declaration below.
template <typename T> class InjectFactory;

/**
* General registry for implementation factories. The registry is templated by the Base class that a
* set of factories conforms to.
Expand Down Expand Up @@ -46,17 +49,32 @@ template <class Base> class FactoryRegistry {
return it->second;
}

private:
// Allow factory injection only in tests.
friend class InjectFactory<Base>;

/**
* Replaces a factory by name. This method should only be used for testing purposes.
* @param factory is the factory to inject.
* @return Base* a pointer to the previously registered value.
*/
static Base* replaceFactoryForTest(Base& factory) {
auto displaced = getFactory(factory.name());
factories().emplace(std::make_pair(factory.name(), &factory));
return displaced;
}

/**
* Remove a factory by name.
* Remove a factory by name. This method should only be used for testing purposes.
* @param name is the name of the factory to remove.
*/
static void unregisterFactory(Base& factory) {
auto result = factories().erase(factory.name());
static void removeFactoryForTest(const std::string& name) {
auto result = factories().erase(name);
if (result == 0) {
throw EnvoyException(fmt::format("No registration for name: '{}'", factory.name()));
throw EnvoyException(fmt::format("No registration for name: '{}'", name));
}
}

private:
/**
* Gets the current map of factory implementations.
*/
Expand Down Expand Up @@ -85,15 +103,8 @@ template <class T, class Base> class RegisterFactory {
*/
RegisterFactory() { FactoryRegistry<Base>::registerFactory(instance_); }

/**
* Destructor that removes an instance of the factory from the FactoryRegistry.
*/
~RegisterFactory() { FactoryRegistry<Base>::unregisterFactory(instance_); }

T& testGetFactory() { return instance_; }

private:
T instance_;
T instance_{};
};

} // namespace Registry
Expand Down
1 change: 1 addition & 0 deletions test/common/network/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ envoy_cc_test(
"//source/common/protobuf",
"//test/mocks/network:network_mocks",
"//test/test_common:environment_lib",
"//test/test_common:registry_lib",
],
)

Expand Down
7 changes: 3 additions & 4 deletions test/common/network/resolver_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "test/mocks/network/mocks.h"
#include "test/test_common/environment.h"
#include "test/test_common/registry.h"
#include "test/test_common/utility.h"

#include "api/address.pb.h"
Expand Down Expand Up @@ -103,12 +104,10 @@ class TestResolver : public Resolver {
};

TEST(ResolverTest, NonStandardResolver) {
// TODO(akonradi) Use singleton override instead of adding and removing
// resolvers for this test once issue #1808 is resolved.
Registry::RegisterFactory<TestResolver, Resolver> register_resolver;
auto& test_resolver = register_resolver.testGetFactory();
TestResolver test_resolver;
test_resolver.addMapping("foo", "1.2.3.4");
test_resolver.addMapping("bar", "4.3.2.1");
Registry::InjectFactory<Resolver> register_resolver(test_resolver);

{
envoy::api::v2::Address address;
Expand Down
8 changes: 8 additions & 0 deletions test/test_common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ envoy_cc_library(
],
)

envoy_cc_test_library(
name = "registry_lib",
hdrs = ["registry.h"],
deps = [
"//include/envoy/registry",
],
)

envoy_cc_test_library(
name = "utility_lib",
srcs = ["utility.cc"],
Expand Down
34 changes: 34 additions & 0 deletions test/test_common/registry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "envoy/registry/registry.h"

#include "gtest/gtest.h"

namespace Envoy {
namespace Registry {

/**
* Factory registration template for tests. This can be used to inject a mock or dummy version
* of a factory for testing purposes. It will restore the original value, if any, when it goes
* out of scope.
*/
template <class Base> class InjectFactory {
public:
InjectFactory(Base& instance) : instance_(instance) {
displaced_ = Registry::FactoryRegistry<Base>::replaceFactoryForTest(instance_);
}

~InjectFactory() {
if (displaced_) {
auto injected = Registry::FactoryRegistry<Base>::replaceFactoryForTest(*displaced_);
EXPECT_EQ(injected, &instance_);
} else {
Registry::FactoryRegistry<Base>::removeFactoryForTest(instance_.name());
}
}

private:
Base& instance_;
Base* displaced_{};
};

} // namespace Registry
} // namespace Envoy