Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.

Conversation

@ACEmilG
Copy link
Contributor

@ACEmilG ACEmilG commented Mar 20, 2018

No description provided.


TEST(ResourceTest, Type) {
std::map<std::string, std::string> m;
google::MonitoredResource mr("type", m);
Copy link
Contributor

Choose a reason for hiding this comment

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

[Optional] Let's go with something other than "type", to make sure the field name does not accidentally leak into the value. I would propose something like "some_resource".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, changed.

}

TEST(ResourceTest, Labels) {
std::map<std::string, std::string> m;
Copy link
Contributor

Choose a reason for hiding this comment

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

std::map<std::string, std::string> m{{"foo", "bar"}, {"bar", "baz"}};
google::MonitoredResource mr("", m);

or even

google::MonitoredResource mr("", {{"foo", "bar"}, {"bar", "baz"}});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great, changed everywhere.

TEST(ResourceTest, BasicEquality) {
std::map<std::string, std::string> m1;
std::map<std::string, std::string> m2;
google::MonitoredResource mr1("", m1);
Copy link
Contributor

Choose a reason for hiding this comment

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

google::MonitoredResource mr1("", {});
google::MonitoredResource mr2("", {});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed everywhere.

google::MonitoredResource mr1("", m1);
google::MonitoredResource mr2("", m2);

EXPECT_TRUE(mr1 == mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

EXPECT_EQ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

google::MonitoredResource mr1("2", m1);
google::MonitoredResource mr2("1", m2);

EXPECT_TRUE(mr1 < mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

EXPECT_LT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

google::MonitoredResource mr1("", m1);
google::MonitoredResource mr2("", m2);

EXPECT_FALSE(mr1 == mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

EXPECT_NE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. I had to add the != operator to resource, but luckily the NE test will cover that as well.

ACEmilG added a commit that referenced this pull request Mar 21, 2018
@ACEmilG ACEmilG force-pushed the ACEmilG-resource-unit-tests branch from 7a96877 to eeeb6e6 Compare March 21, 2018 13:41
Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

One more.


TEST(ResourceTest, Type) {
std::map<std::string, std::string> m;
google::MonitoredResource mr("some_resource", m);
Copy link
Contributor

Choose a reason for hiding this comment

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

google::MonitoredResource mr("some_resource", {}).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, thanks

google::MonitoredResource mr1("2", {});
google::MonitoredResource mr2("1", {});

EXPECT_LT(mr1, mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

This one confuses me, mr1's key is 2, how is 2 less than 1?

Copy link
Contributor

@igorpeshansky igorpeshansky Mar 21, 2018

Choose a reason for hiding this comment

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

Simple -- it's a bug; the implementation is inverted. :-)
Yay for unit tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not a bug: "2" < "1" and "b" < "a". This is confusing, so I added simple examples above the expects to make it more clear.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's be careful here. The specific implementation of operator< for resources does have a bug, because x < y becomes x.operator<(y), which, in the current implementation would evaluate y.type_ < x.type_ || (y.type_ == x.type_ && y.labels_ < x.labels_), which is clearly wrong.
The test @ACEmilG ran to get "2" < "1" is flawed. If you compare std::strings, it would behave as expected: std::string("1") < std::string("2"). When you compare char* values, you're comparing pointers, so the order is essentially random, so that doesn't count.

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 see, thanks for clarification!

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. It's funny that ultimately, the behavior of the operator didn't matter as long as it's stable, because all it was needed for was using MonitoredResource as a key in std::map. But it's good to get it right.

google::MonitoredResource mr2("", {{"a", "a"}});

EXPECT_NE(mr1, mr2);
EXPECT_LT(mr1, mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Same confusion here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See above.

google::MonitoredResource mr1("2", {});
google::MonitoredResource mr2("1", {});

EXPECT_LT(mr1, mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's be careful here. The specific implementation of operator< for resources does have a bug, because x < y becomes x.operator<(y), which, in the current implementation would evaluate y.type_ < x.type_ || (y.type_ == x.type_ && y.labels_ < x.labels_), which is clearly wrong.
The test @ACEmilG ran to get "2" < "1" is flawed. If you compare std::strings, it would behave as expected: std::string("1") < std::string("2"). When you compare char* values, you're comparing pointers, so the order is essentially random, so that doesn't count.

google::MonitoredResource mr1("2", {});
google::MonitoredResource mr2("1", {});

EXPECT_LT("2", "1");
Copy link
Contributor

Choose a reason for hiding this comment

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

Leftover debug code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed. It was as an example (for my mistaken understanding of the comparison)

google::MonitoredResource mr1("", {{"b", "b"}});
google::MonitoredResource mr2("", {{"a", "a"}});

EXPECT_LT("b", "a");
Copy link
Contributor

Choose a reason for hiding this comment

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

Leftover debug code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed.

Copy link
Contributor

@igorpeshansky igorpeshansky left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

Don't forget to "squash and merge".

google::MonitoredResource mr1("2", {});
google::MonitoredResource mr2("1", {});

EXPECT_LT(mr1, mr2);
Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. It's funny that ultimately, the behavior of the operator didn't matter as long as it's stable, because all it was needed for was using MonitoredResource as a key in std::map. But it's good to get it right.

Copy link
Contributor

@bmoyles0117 bmoyles0117 left a comment

Choose a reason for hiding this comment

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

LGTM

@igorpeshansky igorpeshansky merged commit 83602d4 into master Mar 22, 2018
@igorpeshansky igorpeshansky deleted the ACEmilG-resource-unit-tests branch March 22, 2018 23:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants