uber-go / goleak

Goroutine leak detector
MIT License
4.5k stars 148 forks source link

Add option to pass teardown function #47

Closed vdimir closed 4 years ago

vdimir commented 4 years ago

Hi!

In some cases performing additional work after test execution are required. But when VerifyTestMain is used it is not possible to do this work before leaked goroutine caught. This may be related to goroutine shutdown, so it should be done inside VerifyTestMain after test run but before check.

This PR adds option to pass custom teardown function.

CLAassistant commented 4 years ago

CLA assistant check
All committers have signed the CLA.

codecov[bot] commented 4 years ago

Codecov Report

Merging #47 into master will decrease coverage by 3.75%. The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #47      +/-   ##
==========================================
- Coverage   95.20%   91.44%   -3.76%     
==========================================
  Files           4        4              
  Lines         146      152       +6     
==========================================
  Hits          139      139              
- Misses          4        9       +5     
- Partials        3        4       +1     
Impacted Files Coverage Δ
leaks.go 92.00% <0.00%> (-8.00%) :arrow_down:
options.go 93.10% <0.00%> (-6.90%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 4eaa857...430a96a. Read the comment docs.

prashantv commented 4 years ago

Thanks for the contribution @vdimir

If I understand correctly, you want to run some cleanup logic after tests are run, but before goleak looks for leaks.

I'm not sure an Option is the best solution for this, as it doesn't make sense for Find or VerifyNone to have a cleanup function that runs at the beginning.

Since the VerifyTestMain takes an interface TestingM, you could pass an implementation that wraps the underying test-runner, and then runs some cleanup function. E.g.,

func WithCleanup(m goleak.TestingM, cleanup func()) goleak.TestingM {
  return &cleanupM{m, cleanup}
}

type cleanupM struct {
  underying goleak.testingM
  cleanup func()
}

func (m *cleanupM) Run() int {
  code := m.underying.Run()
  m.cleanup()
  return code
}

This is something that's agnostic of goleak and can be done in your own code-base.

vdimir commented 4 years ago

Got it! Thanks @prashantv . I'll try this approach, I reckon it solves the problem. So, I'm closing this PR.