Skip to content

Commit 5911b50

Browse files
committed
added test and fixes
1 parent 9850335 commit 5911b50

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

internal/xruntime/cleanup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package xruntime
55
import "runtime"
66

77
func AddCleanup[T, S any](ptr *T, cleanup func(S), arg S) {
8-
runtime.SetFinalizer(ptr, func() {
8+
runtime.SetFinalizer(ptr, func(ptr *T) {
99
cleanup(arg)
1010
})
1111
}

internal/xruntime/cleanup_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package xruntime
2+
3+
import (
4+
"runtime"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestAddCleanup(t *testing.T) {
10+
cleanupCalled := make(chan struct{})
11+
func() {
12+
v := &struct {
13+
a int64
14+
b string
15+
c bool
16+
}{}
17+
AddCleanup(v, func(cleanupCalled chan struct{}) {
18+
close(cleanupCalled)
19+
}, cleanupCalled)
20+
}()
21+
runtime.GC()
22+
select {
23+
case <-cleanupCalled:
24+
case <-time.After(time.Second):
25+
t.Fail()
26+
}
27+
}

0 commit comments

Comments
 (0)