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

Make the AlreadyRegisteredError useful for wrapped registries #607

Merged
merged 1 commit into from
Jun 14, 2019

Conversation

beorn7
Copy link
Member

@beorn7 beorn7 commented Jun 14, 2019

@lukedirtwalker please have a look. This should fix your use case.

@beorn7
Copy link
Member Author

beorn7 commented Jun 14, 2019

This fixes #605

Copy link

@lukedirtwalker lukedirtwalker left a comment

Choose a reason for hiding this comment

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

I didn't test it myself but if the tests you wrote pass, my use case should be covered.
Added some comments, but I'm also fine with the patch set as is.

Thanks for the quick response and fix, much appreciated :)

t.Error("expected original collector but got new one")
}
} else {
t.Error("unexpected error:", err)

Choose a reason for hiding this comment

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

Why are you using t.Error here. I think this is never expected so we could just use fatal?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's definitely fatal as in "the test is broken". But I only use t.Fatal in tests if the rest of the test wouldn't make sense to run anymore. However, if the rest of the test can still in principle work and tell me about other broken things, I use t.Error so that it continue to runs.

In this case, the Error is the last thing in the (sub-)test, so the difference doesn't really matter. It's more that I personally default to t.Error for the reasons above.

Choose a reason for hiding this comment

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

Makes sense yeah. 👍

if are.ExistingCollector != original {
t.Error("expected original collector but got something else")
}
if are.ExistingCollector == equalButNotSame {

Choose a reason for hiding this comment

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

I think I would just have that included in the error above

t.Errorf("expected original collector but got %v", are.ExiststingCollector)

(or even use Fatalf)

Copy link
Member Author

Choose a reason for hiding this comment

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

The %v representation of a collector can look a bit daunting. As this is an == test, you'll see from the line where the test fails what precise collector was returned. My take is that this is slightly clearer than dumping a possibly complex text representation to the test output (which might have the right hint for the qualified reader, but will probably require to look at the test code anyway).

} else {
t.Error("unexpected error:", err)
}
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {

Choose a reason for hiding this comment

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

This if/else block is almost the same for all test maybe add a function to check it, something like func checkAlreadyRegisteredError(t *testing.T, err error, original, equalButNotSame Collector) {...}

Copy link
Member Author

Choose a reason for hiding this comment

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

The whole test could (or should) be transformed into a table based one, filling in the registries used for registering, re-registering, and the CounterVecs to register and reregister.

In general, I don't mind code duplication in tests if it makes things very explicit. But perhaps at this point, the table approach is even easier to grok.

I'll work on it...

Copy link
Member Author

Choose a reason for hiding this comment

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

The test is now table-driven.

Signed-off-by: beorn7 <beorn@grafana.com>
@beorn7 beorn7 merged commit 6636dde into master Jun 14, 2019
@beorn7 beorn7 deleted the beorn7/wrap branch June 14, 2019 16:35
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.

None yet

2 participants