From 5c8a66bcc9202fbbb256801d6b8099f56cb390f5 Mon Sep 17 00:00:00 2001 From: Joel Speed Date: Fri, 3 Jul 2020 15:24:57 +0100 Subject: [PATCH] Close client connections after each redis test --- CHANGELOG.md | 1 + pkg/sessions/redis/session_store_test.go | 36 +++++++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd35825..5a158aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ## Changes since v6.0.0 +- [#654](https://github.com/oauth2-proxy/oauth2-proxy/pull/654) Close client connections after each redis test (@JoelSpeed) - [#542](https://github.com/oauth2-proxy/oauth2-proxy/pull/542) Move SessionStore tests to independent package (@JoelSpeed) - [#577](https://github.com/oauth2-proxy/oauth2-proxy/pull/577) Move Cipher and Session Store initialisation out of Validation (@JoelSpeed) - [#635](https://github.com/oauth2-proxy/oauth2-proxy/pull/635) Support specifying alternative provider TLS trust source(s) (@k-wall) diff --git a/pkg/sessions/redis/session_store_test.go b/pkg/sessions/redis/session_store_test.go index b56e0d9..0fd1796 100644 --- a/pkg/sessions/redis/session_store_test.go +++ b/pkg/sessions/redis/session_store_test.go @@ -29,7 +29,14 @@ func TestSessionStore(t *testing.T) { } var _ = Describe("Redis SessionStore Tests", func() { + // helper interface to allow us to close client connections + // All non-nil redis clients should implement this + type closer interface { + Close() error + } + var mr *miniredis.Miniredis + var ss sessionsapi.SessionStore BeforeEach(func() { var err error @@ -41,12 +48,25 @@ var _ = Describe("Redis SessionStore Tests", func() { mr.Close() }) + JustAfterEach(func() { + // Release any connections immediately after the test ends + if redisStore, ok := ss.(*SessionStore); ok { + if redisStore.Client != nil { + Expect(redisStore.Client.(closer).Close()).To(Succeed()) + } + } + }) + tests.RunSessionStoreTests( func(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessionsapi.SessionStore, error) { // Set the connection URL opts.Type = options.RedisSessionStoreType opts.Redis.ConnectionURL = "redis://" + mr.Addr() - return NewRedisSessionStore(opts, cookieOpts) + + // Capture the session store so that we can close the client + var err error + ss, err = NewRedisSessionStore(opts, cookieOpts) + return ss, err }, func(d time.Duration) error { mr.FastForward(d) @@ -63,7 +83,7 @@ var _ = Describe("Redis SessionStore Tests", func() { }) AfterEach(func() { - go ms.Close() + ms.Close() }) tests.RunSessionStoreTests( @@ -74,7 +94,11 @@ var _ = Describe("Redis SessionStore Tests", func() { opts.Redis.SentinelConnectionURLs = []string{sentinelAddr} opts.Redis.UseSentinel = true opts.Redis.SentinelMasterName = ms.MasterInfo().Name - return NewRedisSessionStore(opts, cookieOpts) + + // Capture the session store so that we can close the client + var err error + ss, err = NewRedisSessionStore(opts, cookieOpts) + return ss, err }, func(d time.Duration) error { mr.FastForward(d) @@ -90,7 +114,11 @@ var _ = Describe("Redis SessionStore Tests", func() { opts.Type = options.RedisSessionStoreType opts.Redis.ClusterConnectionURLs = []string{clusterAddr} opts.Redis.UseCluster = true - return NewRedisSessionStore(opts, cookieOpts) + + // Capture the session store so that we can close the client + var err error + ss, err = NewRedisSessionStore(opts, cookieOpts) + return ss, err }, func(d time.Duration) error { mr.FastForward(d)