TestFinalizerInterface tests automatic cleanup via ClassFinalizer interface
(t *testing.T)
| 871 | |
| 872 | // TestFinalizerInterface tests automatic cleanup via ClassFinalizer interface |
| 873 | func 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 | } |
nothing calls this directly
no test coverage detected