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

Function TestMemoryManagement

class_test.go:1075–1127  ·  view source on GitHub ↗

TestMemoryManagement tests proper cleanup and memory management

(t *testing.T)

Source from the content-addressed store, hash-verified

1073
1074// TestMemoryManagement tests proper cleanup and memory management
1075func TestMemoryManagement(t *testing.T) {
1076 rt := NewRuntime()
1077 defer rt.Close()
1078
1079 context := rt.NewContext()
1080 defer context.Close()
1081
1082 // Reset finalize counter
1083 resetFinalizeCounter()
1084
1085 // Create multiple classes to test handle store management
1086 for i := 0; i < 5; i++ {
1087 className := fmt.Sprintf("TestClass%d", i)
1088 constructor, _ := NewClassBuilder(className).
1089 Constructor(func(ctx *Context, instance *Value, args []*Value) (interface{}, error) {
1090 point := &Point{X: float64(i), Y: float64(i * 2)}
1091 // SCHEME C: Return Go object for automatic association
1092 return point, nil
1093 }).
1094 Method("getValue", func(ctx *Context, this *Value, args []*Value) *Value {
1095 return ctx.NewFloat64(float64(i)) // Updated: Use NewFloat64()
1096 }).
1097 Build(context)
1098
1099 if constructor.IsException() {
1100 err := context.Exception()
1101 t.Fatalf("Failed to create class %s: %v", className, err)
1102 }
1103
1104 // Create instances and let them be garbage collected
1105 // Note: Globals will manage the constructor memory automatically
1106 context.Globals().Set(className, constructor)
1107 result := context.Eval(fmt.Sprintf(`
1108 for (let j = 0; j < 5; j++) {
1109 let obj = new %s();
1110 }
1111 `, className))
1112 defer result.Free()
1113 if result.IsException() {
1114 err := context.Exception()
1115 t.Fatalf("Failed to create instances for %s: %v", className, err)
1116 }
1117 }
1118
1119 // Force garbage collection multiple times
1120 for i := 0; i < 5; i++ {
1121 rt.RunGC()
1122 runtime.GC() // Go's runtime GC
1123 time.Sleep(10 * time.Millisecond)
1124 }
1125
1126 t.Logf("Created and cleaned up multiple classes, finalizers called: %d", getFinalizeCount())
1127}
1128
1129func TestClassBuilder_ValueSpecProperties(t *testing.T) {
1130 useStableOwnerHooksForLegacySubtests(t)

Callers

nothing calls this directly

Calls 15

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
NewClassBuilderFunction · 0.85
getFinalizeCountFunction · 0.85
MethodMethod · 0.80

Tested by

no test coverage detected