internal/atlas: bug fix: test flakiness due to GC

This commit is contained in:
Hajime Hoshi 2024-02-15 03:00:49 +09:00
parent 820c996329
commit 24256af624
2 changed files with 22 additions and 2 deletions

View File

@ -19,6 +19,7 @@ import (
"image/color"
"runtime"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -821,8 +822,17 @@ func TestGC(t *testing.T) {
c := atlas.DeferredFuncCountForTesting()
runtime.KeepAlive(img)
runtime.GC()
diff := atlas.DeferredFuncCountForTesting() - c
// In theory, a finalizer might be called a little later. Try a few times.
for i := 0; i < 3; i++ {
diff := atlas.DeferredFuncCountForTesting() - c
if diff == 1 {
return
}
time.Sleep(time.Millisecond)
}
diff := atlas.DeferredFuncCountForTesting() - c
if got, want := diff, 1; got != want {
t.Errorf("got: %d, want: %d", got, want)
}

View File

@ -19,6 +19,7 @@ import (
"image/color"
"runtime"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -98,8 +99,17 @@ func TestGCShader(t *testing.T) {
c := atlas.DeferredFuncCountForTesting()
runtime.KeepAlive(s)
runtime.GC()
diff := atlas.DeferredFuncCountForTesting() - c
// In theory, a finalizer might be called a little later. Try a few times.
for i := 0; i < 3; i++ {
diff := atlas.DeferredFuncCountForTesting() - c
if diff == 1 {
return
}
time.Sleep(time.Millisecond)
}
diff := atlas.DeferredFuncCountForTesting() - c
if got, want := diff, 1; got != want {
t.Errorf("got: %d, want: %d", got, want)
}