debian-forge-composer/cmd
Ondřej Budai 02b70be9c6 test/dnf-json: fix cleaning-up the cache after the test is finished
t.Parallel() makes defer weird. This commit moves the temporary dir setup
and teardown to the parallel subtest, so the teardown gets called after
the tests are finished.

More detailed explanation:

Consider this code:

```go
t.Run("group", func(t *testing.T) {
    setUp()
    t.Run("parallel test1", parallelTest1)
    t.Run("parallel test2", parallelTest1)
    t.Run("sequential test", sequentialTest)
    cleanUp()
})
```

When the group subtest is started, firstly parallelTest1 is run. When
t.Parallel() in the parallelTest1 is hit, its execution is suspended and
parallelTest2 is started. Once again, t.Parallel() is hit and the
parallelTest2 is suspended. Now, sequentialTest is started. It doesn't
contain t.Parallel() so it's run without any interruptions. The group subtest
continues its execution and calls the cleanUp method. After the cleanup is
done, the group subtest returns and it's time for the important part:
The parallelTest1 and 2 are resumed and run in parallel. In other words,
the code after t.Parallel is run when the PARENT subtest returns.

The important (and weird) part is that the parent subtest of the parallel
subtests returns before its parallel children finish. However, just the inner
anonymous function returns. The containing t.Run("group", func(){...})
function does not return until all the parallel subtests are finished.

How to solve this? One solution is to move the cleanup out of the group
subtest:

```go
t.Run("group", func(t *testing.T) {
    setUp()
    t.Run("parallel test1", parallelTest1)
    t.Run("parallel test2", parallelTest1)
    t.Run("sequential test", sequentialTest)

})
cleanUp()
```

How does this work? The group subtest is not marked as parallel, so it just
runs from its beginning to its end (including the parallel subtests). After
it finishes, then the cleanUp is called.

In this commit I chose a different method: I just moved the clean up into each
parallel subtest. I think this is clearer than all that parallel magic I
tried explaining in previous paragraphs.

See the section "Run a group of tests in parallel":
https://blog.golang.org/subtests

and this paragraph:
https://golang.org/pkg/testing/#hdr-Subtests_and_Sub_benchmarks
2020-05-14 09:49:04 +02:00
..
osbuild-composer fsjobqueue: pass accepted job types to New() 2020-05-13 16:45:09 +02:00
osbuild-dnf-json-tests test/dnf-json: fix cleaning-up the cache after the test is finished 2020-05-14 09:49:04 +02:00
osbuild-image-tests osbuild-image-test: use --output-directory of osbuild 2020-05-13 13:31:23 +02:00
osbuild-pipeline 🐣 Add initial RHEL 8.3 support 2020-04-22 15:25:59 +02:00
osbuild-rcm-tests tests: Distro is fedora-31 update the repo URL to match 2020-04-29 18:48:51 +02:00
osbuild-tests tests: Make TestSources distro independent. Refs #315 2020-03-25 16:30:52 +01:00
osbuild-upload-aws Refactor Azure upload code to a separate package 2019-11-27 12:14:07 +01:00
osbuild-upload-azure cmd/osbuild-upload-azure: improve error message on wrong arguments 2020-04-27 20:34:20 +02:00
osbuild-worker worker: switch to --output-directory=DIR 2020-05-13 13:31:23 +02:00