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

Close client connections after each redis test #654

Merged
merged 1 commit into from
Jul 3, 2020

Conversation

JoelSpeed
Copy link
Member

@JoelSpeed JoelSpeed commented Jul 3, 2020

Description

Make sure we close connections in the redis client after each test

Motivation and Context

Fixes #653

How Has This Been Tested?

docker run --rm -it -v $PWD:/go/src/github.com/oauth2-proxy/oauth2-proxy --workdir /go/src/github.com/oauth2-proxy/oauth2-proxy/pkg/sessions/redis golang:1.14 bash -c "ulimit -n 256 && ulimit -a && go test -v ./..."

Checklist:

  • My change requires a change to the documentation or CHANGELOG.
  • I have updated the documentation/CHANGELOG accordingly.
  • I have created a feature (non-master) branch for my PR.

@JoelSpeed JoelSpeed added the bug label Jul 3, 2020
@JoelSpeed JoelSpeed requested review from NickMeves and a team July 3, 2020 14:38

// close is only used in tests to ensure that client connections are terminated
// after each test is finished
close() error
Copy link
Member

Choose a reason for hiding this comment

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

Any advantage to just doing these in the tests themselves directly rather than adding to the interface and implementation just for tests sake?

redisStore.Client.(*client).Close()

And for cluster:

redisStore.Client.(*clusterClient).Close()

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm happy either way to be honest, it's not uncommon for things like this to be implemented for the tests sake within Go I don't think, but I could just do as you've suggested.

I deliberately set the method to private because it's only for testing so it's not on the public interface

Copy link
Member Author

Choose a reason for hiding this comment

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

I just had another look and decided I will change it, added a small closer interface that can be used in the test, WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

Nice -- this looks super clean. I like it.

NickMeves
NickMeves previously approved these changes Jul 3, 2020
Copy link
Member

@NickMeves NickMeves left a comment

Choose a reason for hiding this comment

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

Looks good to me. Left one design question -- curious your thoughts on the best design (IE hackier test implementation for cleaner production interface VS methods added to the interface just for use in unit tests)

@JoelSpeed JoelSpeed force-pushed the redis-test-client-close branch 2 times, most recently from 32e99d1 to 3d80d5b Compare July 3, 2020 15:20
// Release any connections immediately after the test ends
if redisStore, ok := ss.(*SessionStore); ok {
if redisStore.Client != nil {
redisStore.Client.(closer).Close()
Copy link
Member

@NickMeves NickMeves Jul 3, 2020

Choose a reason for hiding this comment

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

Since Close() can error - I think we should assert that it didn't error.

Otherwise someday some developer will run into the same unclear socket timeout/file descriptor errors that are less clear when we could expose more clearly that the error was here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, gimme a second

Copy link
Member

@NickMeves NickMeves left a comment

Choose a reason for hiding this comment

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

Looks good! Thanks for helping to figure this out.

@JoelSpeed JoelSpeed merged commit 39c01d5 into master Jul 3, 2020
@JoelSpeed JoelSpeed deleted the redis-test-client-close branch July 3, 2020 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Miniredis session tests appear to leak file descriptors
2 participants