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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WaitAndRecover method to WaitGroup #53

Merged
merged 2 commits into from Jan 13, 2023
Merged

Add WaitAndRecover method to WaitGroup #53

merged 2 commits into from Jan 13, 2023

Conversation

craigpastro
Copy link
Contributor

Resolves #29.

馃憢 My attempt at resolving #29. I wasn't sure if was better to reimplement Wait in terms of WaitSafe. I think leaving the code as it looks a bit cleaner, so I just left it.

I also wondered if it is better to say in the docs that WaitSafe will return the first panic it encounters and add a test for that. That is true, right?

Thank you!

@camdencheek camdencheek self-requested a review January 11, 2023 18:11
Copy link
Member

@camdencheek camdencheek left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looking great. Just a couple of small comments inline.

waitgroup.go Outdated
Comment on lines 45 to 47
// WaitSafe will block until all goroutines spawned with Go exit and will
// return a *panics.RecoveredPanic if one of the child goroutines panics.
func (h *WaitGroup) WaitSafe() *panics.RecoveredPanic {
Copy link
Member

Choose a reason for hiding this comment

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

I'm starting to think that WaitAndRecover() might be a better name. The term "Safe" is pretty overloaded, and it's not super clear what it means without reading the doc string. "Recover" is a much more specific term regarding panics, and is a little more intuitive from my perspective.

The extra verbosity seems acceptable given that most users should probably be using Wait() still.

What do you think?

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 think that is a good call. Updated. Thanks!

panic("super bad thing")
})
p := wg.WaitSafe()
require.Contains(t, p.Error(), "super bad thing", p.Error())
Copy link
Member

Choose a reason for hiding this comment

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

nit: we can compare the panic value directly rather than checking the stringified error message.

Also, arguments 4+ for require.Contains() are to optionally add more information to the failure message. The error will already be included in the message if it fails, so there's no need for the optional args here.

Suggested change
require.Contains(t, p.Error(), "super bad thing", p.Error())
require.Equal(t, p.Value, "super bad thing")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops. Thank you! Fixed.

panic("another bad thing")
})
p := wg.WaitSafe()
require.Contains(t, p.Error(), "bad thing", p.Error())
Copy link
Member

Choose a reason for hiding this comment

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

This test seems slightly misleading. I think it might be more clear if the test more checked that the returned panic matches one of the panic values exactly. Something like:

require.True(t, p.Value == "one bad thing" || p.Value == "another bad thing")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. How about checking simply if p is non-nil?

I think at first I was thinking about adding a small sleep and checking that the first one to panic is the one that is returned, but that may possibly add flakiness, so I decided against it.

I'll update to a non-nil check for now, but let me know if you would like to be more explicit. Thanks!

@codecov-commenter
Copy link

Codecov Report

Merging #53 (83a01d6) into main (559008f) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##             main      #53   +/-   ##
=======================================
  Coverage   99.44%   99.45%           
=======================================
  Files          10       10           
  Lines         359      364    +5     
=======================================
+ Hits          357      362    +5     
  Misses          2        2           
Impacted Files Coverage 螖
waitgroup.go 100.00% <100.00%> (酶)

馃摚 We鈥檙e building smart automated test selection to slash your CI/CD build times. Learn more

@craigpastro craigpastro changed the title Add a WaitSafe method to WaitGroup Add WaitAndRecover method to WaitGroup Jan 12, 2023
Copy link
Member

@camdencheek camdencheek left a comment

Choose a reason for hiding this comment

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

Looks great, thanks for the contribution!

@camdencheek camdencheek merged commit ec1413e into sourcegraph:main Jan 13, 2023
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.

Add a Wait()-like method that returns a caught panic
3 participants