-
Notifications
You must be signed in to change notification settings - Fork 11
Conversation
test/api_server_unittest.cc
Outdated
| }; | ||
|
|
||
| TEST_F(ApiServerTest, BasicDispacher) { | ||
| BasicDispatcher(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm... Do you have to put the whole test into a static function in the fixture? Would it make more sense to add functions to create and invoke the dispatcher instead? Also below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that HttpServer, Handler, HandlerMap, and even Dispatcher, are all declared private. We could attempt to pull out small portions of the test, but that seems confusing and difficult without any gain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be enough to expose a Dispatcher test factory and an invoker, e.g.:
class ApiServerTest : public ::testing::Test {
protected:
using Dispatcher = MetadataApiServer::Dispatcher;
std::unique_ptr<Dispatcher> CreateDispatcher(
const std::map<std::pair<std::string, std::string>,
std::function<void()>>& handlers) {
Dispatcher::HandlerMap handler_map;
for (const auto& element : handlers) {
std::function<void()> handler = element.second;
handler_map.emplace(element.first, [handler](
const MetadataApiServer::HttpServer::request& request,
std::shared_ptr<MetadataApiServer::HttpServer::connection> conn) {
handler();
});
}
return std::unique_ptr<Dispatcher>(new Dispatcher(handler_map, false));
}
void InvokeDispatcher(
const std:: unique_ptr<Dispatcher>& dispatcher,
const std::string& method, const std::string& path) {
MetadataApiServer::HttpServer::request request;
request.method = method;
request.destination = path;
(*dispatcher)(request, nullptr);
}Then you can use the following in the test, e.g.:
bool handler_called;
std:: unique_ptr<Dispatcher> dispatcher = CreateDispatcher({
{{"GET", "/testPath/"}, [&handler_called]() {
handler_called = true;
}},
});
InvokeDispatcher(dispatcher, "GET", "/testPathFoo/");
EXPECT_FALSE(handler_called);
InvokeDispatcher(dispatcher, "GET", "/test/");
EXPECT_FALSE(handler_called);
InvokeDispatcher(dispatcher, "GET", "/testFooPath/");
EXPECT_FALSE(handler_called);There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool, changed, much better.
test/store_unittest.cc
Outdated
| protected: | ||
| MetadataStoreTest() : config(), store(config) {} | ||
|
|
||
| std::map<MonitoredResource, MetadataStore::Metadata> GetMetadataMap() const { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? Isn't GetMetadataMap public?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Backed out these changes - sorry about that, I should have remembered before pushing.
test/api_server_unittest.cc
Outdated
| }; | ||
|
|
||
| TEST_F(ApiServerTest, BasicDispacher) { | ||
| BasicDispatcher(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be enough to expose a Dispatcher test factory and an invoker, e.g.:
class ApiServerTest : public ::testing::Test {
protected:
using Dispatcher = MetadataApiServer::Dispatcher;
std::unique_ptr<Dispatcher> CreateDispatcher(
const std::map<std::pair<std::string, std::string>,
std::function<void()>>& handlers) {
Dispatcher::HandlerMap handler_map;
for (const auto& element : handlers) {
std::function<void()> handler = element.second;
handler_map.emplace(element.first, [handler](
const MetadataApiServer::HttpServer::request& request,
std::shared_ptr<MetadataApiServer::HttpServer::connection> conn) {
handler();
});
}
return std::unique_ptr<Dispatcher>(new Dispatcher(handler_map, false));
}
void InvokeDispatcher(
const std:: unique_ptr<Dispatcher>& dispatcher,
const std::string& method, const std::string& path) {
MetadataApiServer::HttpServer::request request;
request.method = method;
request.destination = path;
(*dispatcher)(request, nullptr);
}Then you can use the following in the test, e.g.:
bool handler_called;
std:: unique_ptr<Dispatcher> dispatcher = CreateDispatcher({
{{"GET", "/testPath/"}, [&handler_called]() {
handler_called = true;
}},
});
InvokeDispatcher(dispatcher, "GET", "/testPathFoo/");
EXPECT_FALSE(handler_called);
InvokeDispatcher(dispatcher, "GET", "/test/");
EXPECT_FALSE(handler_called);
InvokeDispatcher(dispatcher, "GET", "/testFooPath/");
EXPECT_FALSE(handler_called);
test/api_server_unittest.cc
Outdated
| class ApiServerTest : public ::testing::Test { | ||
| protected: | ||
| static void BasicDispatcher() { | ||
| bool handler_called; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you forget to initialize this? Also below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
|
|
||
| private: | ||
| friend class ApiServerTest; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is actually a bug in line 43 of api_server.cc — reverse iterators should still be incremented with ++. Can you please change --it to ++it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
|
||
| private: | ||
| friend class ApiServerTest; | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another thing we can clean up here is making both operator() and log() const in Dispatcher. Can you please do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking better. Some more comments.
test/api_server_unittest.cc
Outdated
| } | ||
|
|
||
| void InvokeDispatcher( | ||
| const std:: unique_ptr<Dispatcher>& dispatcher, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sigh, I thought I've caught them all... That's what I get for typing in code on my phone.
Let's remove the space after the ::.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry - I should have caught this myself
test/api_server_unittest.cc
Outdated
| } | ||
| }; | ||
|
|
||
| TEST_F(ApiServerTest, BasicDispacher) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about better test names? I'd call this one DispatcherMatchesFullPath.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, changed
test/api_server_unittest.cc
Outdated
| handler_called = true; | ||
| }}, | ||
| {{"GET", "/badPath/"}, [&handler_called]() { | ||
| handler_called = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not enough signal here (e.g., imagine if the implementation accidentally called every handler — if this one got called first, you'd never know). How about adding a bad_handler_called boolean for this one (and setting it to true in the handler)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, added.
test/api_server_unittest.cc
Outdated
| EXPECT_TRUE(handler_called); | ||
| } | ||
|
|
||
| TEST_F(ApiServerTest, DispatcherMethodCheck) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about DispatcherUnmatchedMethod?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, changed
| EXPECT_FALSE(handler_called); | ||
| } | ||
|
|
||
| TEST_F(ApiServerTest, DispatcherSubstringCheck) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also check that an actual substring does match (e.g., /testPath/subPath/)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, added
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ![]()
Does this need rebasing against master?
supriyagarg
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A rebase against master is required, but otherwise LGTM.
7e9b10a to
dab7c88
Compare
|
Rebased |
dab7c88 to
40965ca
Compare
test/api_server_unittest.cc
Outdated
| EXPECT_FALSE(handler_called); | ||
| InvokeDispatcher(dispatcher, "GET", "/testFooPath/"); | ||
| EXPECT_FALSE(handler_called); | ||
| InvokeDispatcher(dispatcher, "GET", "/testPath/subPath"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this have been "/testPath/subPath/" (note the trailing /)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
igorpeshansky
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM ![]()
This is a fresh attempt at testing the API server. See PR #123 for previous try.