I have noticed that go tests do not execute defer
or t.Cleanup
statements when running past the default 10m timeout (or otherwise specified using the -timeout
flag).
Consider the following code:
<code>package timeout_test
import (
"fmt"
"testing"
"time"
)
func TestTimeoutDeferCleanup(t *testing.T) {
fmt.Println("This is printed.")
defer fmt.Println("This is not printed.")
t.Cleanup(func() {
fmt.Println("This is not printed either.")
})
time.Sleep(2 * time.Second)
}
</code>
<code>package timeout_test
import (
"fmt"
"testing"
"time"
)
func TestTimeoutDeferCleanup(t *testing.T) {
fmt.Println("This is printed.")
defer fmt.Println("This is not printed.")
t.Cleanup(func() {
fmt.Println("This is not printed either.")
})
time.Sleep(2 * time.Second)
}
</code>
package timeout_test
import (
"fmt"
"testing"
"time"
)
func TestTimeoutDeferCleanup(t *testing.T) {
fmt.Println("This is printed.")
defer fmt.Println("This is not printed.")
t.Cleanup(func() {
fmt.Println("This is not printed either.")
})
time.Sleep(2 * time.Second)
}
Run this with go test -v -timeout 1s ./timeout_test.go
.
Does anyone know a way around this other than disabling the timer (-timeout 0
)?
1