diff options
author | Sami Hiltunen <shiltunen@gitlab.com> | 2022-01-24 18:45:53 +0300 |
---|---|---|
committer | Sami Hiltunen <shiltunen@gitlab.com> | 2022-01-27 15:51:08 +0300 |
commit | 6c91c8e8b0917567fdcd6b14b075c8bdf20d2c5f (patch) | |
tree | 38f87248a6ee7993e137aa74580c3860558511a0 /internal/streamcache | |
parent | b4a1fffaa707d5f0dc19af21f06947d0274db878 (diff) |
Automatically clean up testhelper.Context
testhelper.Context() currently return a cancellation function as a
second return value. Majority of the tests do not need to explicitly
cancel the context but they simply defer its cancellation to clean up
after the test. Given this, we can reduce the test verbosity and make
testhelper.Context easier to compose by removing the unnecessary second
return value. This adds a t.Cleanup function to automatically cancel the
context at the end of the tests and omits the returned cancellation
function.
Tests which simply `defer cancel()` have had the extra call removed. Some
tests explicitly call the cancellation, and these tests have been modified
to add context.WithCancel around the testhelper.Context call. There are a
few loctions where testing.TB was passed down to test helpers that create the
context.
Diffstat (limited to 'internal/streamcache')
-rw-r--r-- | internal/streamcache/cache_test.go | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/internal/streamcache/cache_test.go b/internal/streamcache/cache_test.go index 1bb9009ea..783c44467 100644 --- a/internal/streamcache/cache_test.go +++ b/internal/streamcache/cache_test.go @@ -29,8 +29,7 @@ func newCache(dir string) Cache { } func TestCache_writeOneReadMultiple(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -62,8 +61,7 @@ func TestCache_writeOneReadMultiple(t *testing.T) { } func TestCache_manyConcurrentWrites(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -181,8 +179,7 @@ func TestCache_deletedFile(t *testing.T) { } func TestCache_scope(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -231,8 +228,7 @@ func TestCache_scope(t *testing.T) { } func TestCache_diskCleanup(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -311,8 +307,7 @@ func TestCache_diskCleanup(t *testing.T) { } func TestCache_failedWrite(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -374,8 +369,7 @@ func TestCache_failCreateFile(t *testing.T) { } func TestCache_unWriteableFile(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -402,8 +396,7 @@ func TestCache_unWriteableFile(t *testing.T) { } func TestCache_unCloseableFile(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) tmp := testhelper.TempDir(t) @@ -451,8 +444,7 @@ func TestCache_cannotOpenFileForReading(t *testing.T) { } func TestWaiter(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) w := newWaiter() err := errors.New("test error") @@ -461,8 +453,7 @@ func TestWaiter(t *testing.T) { } func TestWaiter_cancel(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx, cancel := context.WithCancel(testhelper.Context(t)) w := newWaiter() errc := make(chan error, 1) @@ -473,8 +464,7 @@ func TestWaiter_cancel(t *testing.T) { } func TestNullCache(t *testing.T) { - ctx, cancel := testhelper.Context() - defer cancel() + ctx := testhelper.Context(t) const ( N = 1000 |