Skip to content

test: Add a mechanism for creating singletons in tests#5086

Merged
htuch merged 13 commits intoenvoyproxy:masterfrom
jmarantz:global-pattern
Nov 21, 2018
Merged

test: Add a mechanism for creating singletons in tests#5086
htuch merged 13 commits intoenvoyproxy:masterfrom
jmarantz:global-pattern

Conversation

@jmarantz
Copy link
Contributor

@jmarantz jmarantz commented Nov 19, 2018

Description: For the record, I am against singletons in general, and believe it's more maintainable to pass context objects through the call/instantiation stack, as is done in Envoy production code. However the test system employs a deep system of mocks that are constructed without context. As a result, any new context that may be needed in the future (e.g. SymbolTable in #4980, ThreadSystem in #5055 and #5072 and FileSystem in #5031) need a mechanism to plumb data into the system as instantiated during tests. I believe Api::OsSysCalls() is another example that might benefit from this.

This class provides a mechanism to have a bounded-scope singleton, such that anywhere an instance of a type T is desired, the same instance will be available. Once all references to a Global go out of scope, the shared instance is destroyed and the pointer to it nulled.

Thus, this singleton pattern has the property that between tests, the state will be completely reset, as long as there are no memory leaks.

The new class, Test::Global, creates a potential conflict with tests that had declared using testing::Test;, so this PR also cleans those up and adds a format check to avoid having that pattern re-occur.

Risk Level: the main risk here is that this class will be abused and used in lieu of proper object passing when that's most appropriate. However, the new library is declared as a bazel test library and is in the test/... tree, so hopefully -- with proper code reviews, the usage will be limited.
Testing: //test/...
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
… the test-runner that there are none remaining.

This also involved renaming a helper class that tracks the entire
system to 'Globals', which is where the new method was exposed.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, this seems a useful tool to have around for tests.

ASSERT(singleton->ref_count_ == 0);
singleton->ptr_ = make_object();
}
++singleton->ref_count_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come you're not using a shared_ptr/weak_ptr pattern here but doing it by hand?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this for a bit; couldn't figure out how to make the pieces fit given the heterogenous map and casting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about std::shared_ptr<Singleton> (and corresponding weak_ptr), with a void * still inside the object to provide the type cast abilities?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought of that, but thought it annoying that I'd have to hold a functor for calling the class-specific delete in the singleton.

Then I thought I'd give it a shot and it wasn't too bad to just do that, so switching to weak_ptr.

@htuch htuch self-assigned this Nov 19, 2018
This simplifies reasoning about the system, and potential for contention is not
important in tests. It does, however, make it harder to use thread-annotation due
to aliasing.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
…ngs in globals.cc/h

Signed-off-by: Joshua Marantz <jmarantz@google.com>
ASSERT(singleton->ref_count_ == 0);
singleton->ptr_ = make_object();
}
++singleton->ref_count_;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about std::shared_ptr<Singleton> (and corresponding weak_ptr), with a void * still inside the object to provide the type cast abilities?

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
htuch
htuch previously approved these changes Nov 20, 2018
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, do you have any thoughts on the one comment I left?

* start.
*/
class Globals {
using MakeObjectFn = std::function<void*()>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had another idea; I just added a templatized derived class which handles deletion in a type-safe way (though we do still need to use static_cast to access the pointer and ref from Singleton).

ptal.

…ing a functor.

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Joshua Marantz <jmarantz@google.com>
Copy link
Member

@htuch htuch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rad!

@htuch htuch merged commit 08310eb into envoyproxy:master Nov 21, 2018
@jmarantz jmarantz deleted the global-pattern branch November 21, 2018 16:35
fredlas pushed a commit to fredlas/envoy that referenced this pull request Mar 5, 2019
For the record, I am against singletons in general, and believe it's more maintainable to pass context objects through the call/instantiation stack, as is done in Envoy production code. However the test system employs a deep system of mocks that are constructed without context. As a result, any new context that may be needed in the future (e.g. SymbolTable in envoyproxy#4980, ThreadSystem in envoyproxy#5055 and envoyproxy#5072 and FileSystem in envoyproxy#5031) need a mechanism to plumb data into the system as instantiated during tests. I believe Api::OsSysCalls() is another example that might benefit from this.

This class provides a mechanism to have a bounded-scope singleton, such that anywhere an instance of a type T is desired, the same instance will be available. Once all references to a Global go out of scope, the shared instance is destroyed and the pointer to it nulled.

Thus, this singleton pattern has the property that between tests, the state will be completely reset, as long as there are no memory leaks.

The new class, Test::Global, creates a potential conflict with tests that had declared using testing::Test;, so this PR also cleans those up and adds a format check to avoid having that pattern re-occur.

Risk Level: the main risk here is that this class will be abused and used in lieu of proper object passing when that's most appropriate. However, the new library is declared as a bazel test library and is in the test/... tree, so hopefully -- with proper code reviews, the usage will be limited.
Testing: //test/...

Signed-off-by: Joshua Marantz <jmarantz@google.com>
Signed-off-by: Fred Douglas <fredlas@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants