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

Function TestContextMemoryPressure

context_test.go:1915–1957  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1913}
1914
1915func TestContextMemoryPressure(t *testing.T) {
1916 // Test extreme memory pressure to trigger compilation failures
1917 rt := NewRuntime(WithMemoryLimit(256 * 1024)) // 256KB limit
1918 defer rt.Close()
1919 ctx := rt.NewContext()
1920 defer ctx.Close()
1921
1922 // Fill memory first
1923 memoryResult := ctx.Eval(`
1924 var memoryFiller = [];
1925 try {
1926 for(let i = 0; i < 1000; i++) {
1927 memoryFiller.push(new Array(100).fill('x'.repeat(50)));
1928 }
1929 } catch(e) {
1930 // Expected to fail due to memory limit
1931 }
1932 `)
1933 defer memoryResult.Free()
1934
1935 // Try to compile - this should fail at JS_WriteObject due to no available memory
1936 _, err := ctx.Compile(`
1937 var obj = {};
1938 for(let i = 0; i < 100; i++) {
1939 obj['prop_' + i] = function() { return 'value_' + i; };
1940 }
1941 obj;
1942 `)
1943
1944 if err != nil {
1945 t.Logf("Memory pressure compilation error (expected): %v", err)
1946 }
1947
1948 // Try multiple rapid compilations to exhaust memory
1949 for i := 0; i < 20; i++ {
1950 code := fmt.Sprintf(`var obj%d = { data: new Array(500).fill(%d) }; obj%d;`, i, i, i)
1951 _, err := ctx.Compile(code)
1952 if err != nil {
1953 t.Logf("Rapid compilation %d failed (expected): %v", i, err)
1954 break
1955 }
1956 }
1957}
1958
1959func TestContextAsyncFunction(t *testing.T) {
1960 useStableOwnerHooksForLegacySubtests(t)

Callers

nothing calls this directly

Calls 8

CloseMethod · 0.95
NewContextMethod · 0.95
CloseMethod · 0.95
EvalMethod · 0.95
CompileMethod · 0.95
NewRuntimeFunction · 0.85
WithMemoryLimitFunction · 0.85
FreeMethod · 0.45

Tested by

no test coverage detected