mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-27 12:18:55 +01:00
internal/atlas: reduce test flakiness
This commit is contained in:
parent
732eedf2e0
commit
64cb6cf8a9
@ -20,6 +20,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"unsafe"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||||
@ -811,26 +812,32 @@ func TestIteratingImagesToPutOnSourceBackend(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureGC() {
|
||||||
|
// Use a pointer to avoid tinyalloc. A tinyalloc-ed object's finalizer might not called immediately.
|
||||||
|
// See runtime/mfinal_test.go.
|
||||||
|
x := new(unsafe.Pointer)
|
||||||
|
ch := make(chan struct{})
|
||||||
|
runtime.SetFinalizer(x, func(*unsafe.Pointer) { close(ch) })
|
||||||
|
runtime.KeepAlive(x)
|
||||||
|
runtime.GC()
|
||||||
|
<-ch
|
||||||
|
|
||||||
|
// Add a little sleep to wait for other finalizers.
|
||||||
|
// TODO: Is there a better way?
|
||||||
|
time.Sleep(time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
func TestGC(t *testing.T) {
|
func TestGC(t *testing.T) {
|
||||||
img := atlas.NewImage(16, 16, atlas.ImageTypeRegular)
|
img := atlas.NewImage(16, 16, atlas.ImageTypeRegular)
|
||||||
img.WritePixels(make([]byte, 4*16*16), image.Rect(0, 0, 16, 16))
|
img.WritePixels(make([]byte, 4*16*16), image.Rect(0, 0, 16, 16))
|
||||||
|
|
||||||
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
||||||
runtime.GC()
|
ensureGC()
|
||||||
|
|
||||||
// Get the difference of the number of deferred functions before and after img is GCed.
|
// Get the difference of the number of deferred functions before and after img is GCed.
|
||||||
c := atlas.DeferredFuncCountForTesting()
|
c := atlas.DeferredFuncCountForTesting()
|
||||||
runtime.KeepAlive(img)
|
runtime.KeepAlive(img)
|
||||||
runtime.GC()
|
ensureGC()
|
||||||
|
|
||||||
// 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
|
diff := atlas.DeferredFuncCountForTesting() - c
|
||||||
if got, want := diff, 1; got != want {
|
if got, want := diff, 1; got != want {
|
||||||
|
@ -19,7 +19,6 @@ import (
|
|||||||
"image/color"
|
"image/color"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||||
@ -93,21 +92,12 @@ func TestGCShader(t *testing.T) {
|
|||||||
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll)
|
dst.DrawTriangles([graphics.ShaderImageCount]*atlas.Image{}, vs, is, graphicsdriver.BlendCopy, dr, [graphics.ShaderImageCount]image.Rectangle{}, s, nil, graphicsdriver.FillAll)
|
||||||
|
|
||||||
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
// Ensure other objects are GCed, as GC appends deferred functions for collected objects.
|
||||||
runtime.GC()
|
ensureGC()
|
||||||
|
|
||||||
// Get the difference of the number of deferred functions before and after s is GCed.
|
// Get the difference of the number of deferred functions before and after s is GCed.
|
||||||
c := atlas.DeferredFuncCountForTesting()
|
c := atlas.DeferredFuncCountForTesting()
|
||||||
runtime.KeepAlive(s)
|
runtime.KeepAlive(s)
|
||||||
runtime.GC()
|
ensureGC()
|
||||||
|
|
||||||
// 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
|
diff := atlas.DeferredFuncCountForTesting() - c
|
||||||
if got, want := diff, 1; got != want {
|
if got, want := diff, 1; got != want {
|
||||||
|
Loading…
Reference in New Issue
Block a user