MCPcopy Create free account
hub / github.com/buke/quickjs-go / TestFinalizerInterface

Function TestFinalizerInterface

class_test.go:873–930  ·  view source on GitHub ↗

TestFinalizerInterface tests automatic cleanup via ClassFinalizer interface

(t *testing.T)

Source from the content-addressed store, hash-verified

871
872// TestFinalizerInterface tests automatic cleanup via ClassFinalizer interface
873func TestFinalizerInterface(t *testing.T) {
874 rt := NewRuntime()
875 defer rt.Close()
876
877 context := rt.NewContext()
878 defer context.Close()
879
880 // Reset finalize counter
881 resetFinalizeCounter()
882
883 // Create and register Point class
884 pointConstructor, _ := createPointClass(context)
885 if pointConstructor.IsException() {
886 defer pointConstructor.Free()
887 err := context.Exception()
888 t.Fatalf("Failed to create Point class: %v", err)
889 }
890
891 // Register Point class globally
892 // Note: Globals will manage the memory automatically
893 context.Globals().Set("Point", pointConstructor)
894
895 // Create instances that will be garbage collected
896 result := context.Eval(`
897 for (let i = 0; i < 10; i++) {
898 let p = new Point(i, i * 2);
899 // Don't keep references, let them be GC'd
900 }
901 `)
902 defer result.Free()
903 if result.IsException() {
904 err := context.Exception()
905 t.Fatalf("Failed to create test instances: %v", err)
906 }
907
908 // Force garbage collection multiple times
909 for i := 0; i < 5; i++ {
910 rt.RunGC()
911 runtime.GC() // Go's runtime GC
912 time.Sleep(10 * time.Millisecond)
913 }
914
915 // Check that some finalizers were called
916 finalizeCount := getFinalizeCount()
917 if finalizeCount == 0 {
918 t.Logf("Warning: No finalizers called yet (this might be timing-dependent)")
919 // Try more aggressive GC
920 for i := 0; i < 10; i++ {
921 rt.RunGC()
922 runtime.GC() // Go's runtime GC
923 time.Sleep(50 * time.Millisecond)
924 }
925 finalizeCount = getFinalizeCount()
926 }
927
928 t.Logf("Finalizer called %d times", finalizeCount)
929 // Note: Due to GC timing, we can't guarantee exact count, but some should be called
930}

Callers

nothing calls this directly

Calls 14

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
ExceptionMethod · 0.95
GlobalsMethod · 0.95
EvalMethod · 0.95
RunGCMethod · 0.95
NewRuntimeFunction · 0.85
resetFinalizeCounterFunction · 0.85
createPointClassFunction · 0.85
getFinalizeCountFunction · 0.85
IsExceptionMethod · 0.80

Tested by

no test coverage detected