Skip to content

Commit

Permalink
Try to make reftypes test more resilient (bytecodealliance#77)
Browse files Browse the repository at this point in the history
macOS has been flaky for a long time now, and Windows has a weird
exception, so let's see if restructuring the test fixes both issues.
  • Loading branch information
alexcrichton committed May 3, 2021
1 parent 3d90915 commit 5b4f53d
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions reftypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,31 +287,39 @@ func newObjToDrop() (*ObjToDrop, *GcHit) {
}

func TestGlobalFinalizer(t *testing.T) {
store := refTypesStore()
global, err := NewGlobal(
store,
NewGlobalType(NewValType(KindExternref), true),
ValExternref(nil),
)
if err != nil {
panic(err)
}
obj, gc := newObjToDrop()
global.Set(ValExternref(obj))
runtime.GC()
if gc.hit {
panic("gc too early")
}
global.Set(ValExternref(nil))
// try real hard to get the Go GC to run the destructor
for i := 0; i < 10; i++ {
gc := func() *GcHit {
store := refTypesStore()
global, err := NewGlobal(
store,
NewGlobalType(NewValType(KindExternref), true),
ValExternref(nil),
)
if err != nil {
panic(err)
}
obj, gc := newObjToDrop()
global.Set(ValExternref(obj))
runtime.GC()
}
// There's some oddness on Windows right now where I guess the GC above
// doesn't work? Unsure why, but should be safe to skip there if it's
// working everywhere else.
if !gc.hit && runtime.GOOS != "windows" {
panic("dtor not run")
if gc.hit {
panic("gc too early")
}
global.Set(ValExternref(nil))
return gc
}()

// Try real hard to get the Go GC to run the destructor. This is
// somewhat nondeterministic depending on the platform, so this just
// hits `runtime.GC()` a lot and hopes that eventually it actually
// cleans up the `obj` from above. If this loop runs too many times,
// though, we assume it'll never work and we fail the test.
for i := 0; ; i++ {
runtime.GC()
if gc.hit {
break
}
if i >= 10000 {
panic("dtor not run")
}
}
}

Expand Down

0 comments on commit 5b4f53d

Please sign in to comment.