| |
| |
| |
|
|
| package weak_test |
|
|
| import ( |
| "context" |
| "internal/goarch" |
| "runtime" |
| "sync" |
| "testing" |
| "time" |
| "unsafe" |
| "weak" |
| ) |
|
|
| type T struct { |
| |
| |
| |
| |
| t *T |
| a int |
| b int |
| } |
|
|
| func TestPointer(t *testing.T) { |
| var zero weak.Pointer[T] |
| if zero.Value() != nil { |
| t.Error("Value of zero value of weak.Pointer is not nil") |
| } |
| zeroNil := weak.Make[T](nil) |
| if zeroNil.Value() != nil { |
| t.Error("Value of weak.Make[T](nil) is not nil") |
| } |
|
|
| bt := new(T) |
| wt := weak.Make(bt) |
| if st := wt.Value(); st != bt { |
| t.Fatalf("weak pointer is not the same as strong pointer: %p vs. %p", st, bt) |
| } |
| |
| runtime.GC() |
|
|
| if st := wt.Value(); st != bt { |
| t.Fatalf("weak pointer is not the same as strong pointer after GC: %p vs. %p", st, bt) |
| } |
| |
| runtime.GC() |
|
|
| if st := wt.Value(); st != nil { |
| t.Fatalf("expected weak pointer to be nil, got %p", st) |
| } |
| } |
|
|
| func TestPointerEquality(t *testing.T) { |
| var zero weak.Pointer[T] |
| zeroNil := weak.Make[T](nil) |
| if zero != zeroNil { |
| t.Error("weak.Make[T](nil) != zero value of weak.Pointer[T]") |
| } |
|
|
| bt := make([]*T, 10) |
| wt := make([]weak.Pointer[T], 10) |
| wo := make([]weak.Pointer[int], 10) |
| for i := range bt { |
| bt[i] = new(T) |
| wt[i] = weak.Make(bt[i]) |
| wo[i] = weak.Make(&bt[i].a) |
| } |
| for i := range bt { |
| st := wt[i].Value() |
| if st != bt[i] { |
| t.Fatalf("weak pointer is not the same as strong pointer: %p vs. %p", st, bt[i]) |
| } |
| if wp := weak.Make(st); wp != wt[i] { |
| t.Fatalf("new weak pointer not equal to existing weak pointer: %v vs. %v", wp, wt[i]) |
| } |
| if wp := weak.Make(&st.a); wp != wo[i] { |
| t.Fatalf("new weak pointer not equal to existing weak pointer: %v vs. %v", wp, wo[i]) |
| } |
| if i == 0 { |
| continue |
| } |
| if wt[i] == wt[i-1] { |
| t.Fatalf("expected weak pointers to not be equal to each other, but got %v", wt[i]) |
| } |
| } |
| |
| runtime.GC() |
| for i := range bt { |
| st := wt[i].Value() |
| if st != bt[i] { |
| t.Fatalf("weak pointer is not the same as strong pointer: %p vs. %p", st, bt[i]) |
| } |
| if wp := weak.Make(st); wp != wt[i] { |
| t.Fatalf("new weak pointer not equal to existing weak pointer: %v vs. %v", wp, wt[i]) |
| } |
| if wp := weak.Make(&st.a); wp != wo[i] { |
| t.Fatalf("new weak pointer not equal to existing weak pointer: %v vs. %v", wp, wo[i]) |
| } |
| if i == 0 { |
| continue |
| } |
| if wt[i] == wt[i-1] { |
| t.Fatalf("expected weak pointers to not be equal to each other, but got %v", wt[i]) |
| } |
| } |
| bt = nil |
| |
| runtime.GC() |
| for i := range wt { |
| st := wt[i].Value() |
| if st != nil { |
| t.Fatalf("expected weak pointer to be nil, got %p", st) |
| } |
| if i == 0 { |
| continue |
| } |
| if wt[i] == wt[i-1] { |
| t.Fatalf("expected weak pointers to not be equal to each other, but got %v", wt[i]) |
| } |
| } |
| } |
|
|
| func TestPointerFinalizer(t *testing.T) { |
| bt := new(T) |
| wt := weak.Make(bt) |
| done := make(chan struct{}, 1) |
| runtime.SetFinalizer(bt, func(bt *T) { |
| if wt.Value() != nil { |
| t.Errorf("weak pointer did not go nil before finalizer ran") |
| } |
| done <- struct{}{} |
| }) |
|
|
| |
| runtime.GC() |
| if wt.Value() == nil { |
| t.Errorf("weak pointer went nil too soon") |
| } |
| runtime.KeepAlive(bt) |
|
|
| |
| |
| |
| runtime.GC() |
| if wt.Value() != nil { |
| t.Errorf("weak pointer did not go nil when finalizer was enqueued") |
| } |
|
|
| |
| <-done |
|
|
| |
| runtime.GC() |
| if wt.Value() != nil { |
| t.Errorf("weak pointer is non-nil even after finalization: %v", wt) |
| } |
| } |
|
|
| func TestPointerCleanup(t *testing.T) { |
| bt := new(T) |
| wt := weak.Make(bt) |
| done := make(chan struct{}, 1) |
| runtime.AddCleanup(bt, func(_ bool) { |
| if wt.Value() != nil { |
| t.Errorf("weak pointer did not go nil before cleanup was executed") |
| } |
| done <- struct{}{} |
| }, true) |
|
|
| |
| runtime.GC() |
| if wt.Value() == nil { |
| t.Errorf("weak pointer went nil too soon") |
| } |
| runtime.KeepAlive(bt) |
|
|
| |
| |
| |
| runtime.GC() |
| if wt.Value() != nil { |
| t.Errorf("weak pointer did not go nil when cleanup was enqueued") |
| } |
|
|
| |
| <-done |
|
|
| |
| runtime.GC() |
| if wt.Value() != nil { |
| t.Errorf("weak pointer is non-nil even after cleanup: %v", wt) |
| } |
| } |
|
|
| func TestPointerSize(t *testing.T) { |
| var p weak.Pointer[T] |
| size := unsafe.Sizeof(p) |
| if size != goarch.PtrSize { |
| t.Errorf("weak.Pointer[T] size = %d, want %d", size, goarch.PtrSize) |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| func TestIssue69210(t *testing.T) { |
| if testing.Short() { |
| t.Skip("this is a stress test that takes seconds to run on its own") |
| } |
| ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| defer cancel() |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| var wg sync.WaitGroup |
| wg.Add(1) |
| go func() { |
| defer wg.Done() |
| for { |
| runtime.GC() |
|
|
| select { |
| case <-ctx.Done(): |
| return |
| default: |
| } |
| } |
| }() |
| for range max(runtime.GOMAXPROCS(-1)-1, 1) { |
| wg.Add(1) |
| go func() { |
| defer wg.Done() |
| for { |
| for range 5 { |
| bt := new(T) |
| wt := weak.Make(bt) |
| bt = nil |
| time.Sleep(1 * time.Millisecond) |
| bt = wt.Value() |
| if bt != nil { |
| time.Sleep(4 * time.Millisecond) |
| bt.t = bt |
| bt.a = 12 |
| } |
| runtime.KeepAlive(bt) |
| } |
| select { |
| case <-ctx.Done(): |
| return |
| default: |
| } |
| } |
| }() |
| } |
| wg.Wait() |
| } |
|
|
| func TestIssue70739(t *testing.T) { |
| x := make([]*int, 4<<16) |
| wx1 := weak.Make(&x[1<<16]) |
| wx2 := weak.Make(&x[1<<16]) |
| if wx1 != wx2 { |
| t.Fatal("failed to look up special and made duplicate weak handle; see issue #70739") |
| } |
| } |
|
|
| var immortal T |
|
|
| func TestImmortalPointer(t *testing.T) { |
| w0 := weak.Make(&immortal) |
| if weak.Make(&immortal) != w0 { |
| t.Error("immortal weak pointers to the same pointer not equal") |
| } |
| w0a := weak.Make(&immortal.a) |
| w0b := weak.Make(&immortal.b) |
| if w0a == w0b { |
| t.Error("separate immortal pointers (same object) have the same pointer") |
| } |
| if got, want := w0.Value(), &immortal; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
| if got, want := w0a.Value(), &immortal.a; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
| if got, want := w0b.Value(), &immortal.b; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
|
|
| |
| runtime.GC() |
| runtime.GC() |
|
|
| |
| if got, want := w0.Value(), &immortal; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
| if got, want := w0a.Value(), &immortal.a; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
| if got, want := w0b.Value(), &immortal.b; got != want { |
| t.Errorf("immortal weak pointer to %p has unexpected Value %p", want, got) |
| } |
| } |
|
|
| func TestPointerTiny(t *testing.T) { |
| runtime.GC() |
|
|
| const N = 1000 |
| wps := make([]weak.Pointer[int], N) |
| for i := range N { |
| |
| |
| |
| x := new(int) |
| *x = i |
| wps[i] = weak.Make(x) |
| } |
|
|
| |
| runtime.GC() |
|
|
| |
| |
| |
| |
| |
| n := 0 |
| for _, wp := range wps { |
| if wp.Value() == nil { |
| n++ |
| } |
| } |
| const want = 3 * N / 4 |
| if n < want { |
| t.Fatalf("not enough weak pointers are nil: expected at least %v, got %v", want, n) |
| } |
| } |
|
|