Last active
July 27, 2022 22:35
-
-
Save podhmo/9bd064e3e1827830b9824d02645f58bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"strconv" | |
"testing" | |
"time" | |
) | |
func TestDefer(t *testing.T) { | |
// t.Run()で囲ったgoroutineより早くdeferが呼ばれてしまう | |
// see: https://github.com/golang/go/issues/17791 | |
defer func() { t.Logf("end") }() | |
for i := 0; i < 2; i++ { | |
i := i | |
t.Run("case"+strconv.Itoa(i), func(t *testing.T) { | |
t.Parallel() | |
defer t.Logf("end: %d", i) | |
t.Logf("start: %d", i) | |
time.Sleep(1 * time.Second) | |
}) | |
} | |
} | |
func TestCleanup(t *testing.T) { | |
t.Cleanup(func() { t.Logf("end") }) | |
for i := 0; i < 2; i++ { | |
i := i | |
t.Run("case"+strconv.Itoa(i), func(t *testing.T) { | |
t.Parallel() | |
t.Cleanup(func() { t.Logf("end: %d", i) }) | |
t.Logf("start: %d", i) | |
time.Sleep(1 * time.Second) | |
}) | |
} | |
} | |
// === RUN TestDefer | |
// === RUN TestDefer/case0 | |
// === PAUSE TestDefer/case0 | |
// === RUN TestDefer/case1 | |
// === PAUSE TestDefer/case1 | |
// === CONT TestDefer | |
// main_test.go:10: end <- | |
// === CONT TestDefer/case0 | |
// main_test.go:16: start: 0 | |
// === CONT TestDefer/case1 | |
// main_test.go:16: start: 1 | |
// === CONT TestDefer/case0 | |
// main_test.go:18: end: 0 | |
// === CONT TestDefer/case1 | |
// main_test.go:18: end: 1 | |
// --- PASS: TestDefer (0.00s) | |
// --- PASS: TestDefer/case0 (1.00s) | |
// --- PASS: TestDefer/case1 (1.00s) | |
// | |
// | |
// === RUN TestCleanup | |
// === RUN TestCleanup/case0 | |
// === PAUSE TestCleanup/case0 | |
// === RUN TestCleanup/case1 | |
// === PAUSE TestCleanup/case1 | |
// === CONT TestCleanup/case0 | |
// main_test.go:29: start: 0 | |
// === CONT TestCleanup/case1 | |
// main_test.go:29: start: 1 | |
// === CONT TestCleanup/case0 | |
// main_test.go:28: end: 0 | |
// === CONT TestCleanup/case1 | |
// main_test.go:28: end: 1 | |
// === CONT TestCleanup | |
// main_test.go:23: end <- | |
// --- PASS: TestCleanup (0.00s) | |
// --- PASS: TestCleanup/case0 (1.00s) | |
// --- PASS: TestCleanup/case1 (1.00s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment