Skip to content

Commit 95b043d

Browse files
committed
Improve memory limit test
1 parent a697216 commit 95b043d

File tree

3 files changed

+12
-22
lines changed

3 files changed

+12
-22
lines changed

tests/set_memory_limit_001.phpt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,22 @@ if (getenv("SKIP_SLOW_TESTS")) {
1313

1414
$JS = <<< EOT
1515
var jsfunc = function() {
16-
var text = "abcdefghijklmnopqrstuvwyxz0123456789";
16+
var text = "abcdefghijklmnopqrstuvwyxz0123456789"; // 36 bytes fits 277777 times in 10 MB
1717
var memory = "";
18-
for (var i = 0; i < 100; ++i) {
19-
for (var j = 0; j < 10000; ++j) {
18+
19+
// this should exceed the memory limit as it tries to generate 500_000 * 36 bytes = 18 MB
20+
for (var i = 0; i < 50; ++i) {
21+
for (var j = 0; j < 10_000; ++j) {
2022
memory += text;
21-
}
22-
sleep(0);
23+
}
24+
sleep(0);
2325
}
2426
};
2527
jsfunc;
2628
EOT;
2729

2830
$v8 = new V8Js();
29-
$v8->setMemoryLimit(10000000);
31+
$v8->setMemoryLimit(10_000_000);
3032

3133
$func = $v8->executeString($JS);
3234
var_dump($func);

tests/set_memory_limit_003.phpt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,8 @@ if (getenv("SKIP_SLOW_TESTS")) {
1414
$JS = <<< EOT
1515
var jsfunc = function() {
1616
PHP.imposeMemoryLimit();
17-
var text = "abcdefghijklmnopqrstuvwyxz0123456789";
18-
var memory = "";
19-
for (var i = 0; i < 100; ++i) {
20-
for (var j = 0; j < 10000; ++j) {
21-
memory += text;
22-
}
23-
sleep(0);
24-
}
17+
// Allocate a single large string to deterministically exceed the limit
18+
var memory = new Array(12 * 1024 * 1024).join("x"); // ~12MB
2519
};
2620
jsfunc;
2721
EOT;

tests/set_memory_limit_basic.phpt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ if (getenv("SKIP_SLOW_TESTS")) {
1111
--FILE--
1212
<?php
1313
$JS = <<< EOT
14-
var text = "abcdefghijklmnopqrstuvwyxz0123456789";
15-
var memory = "";
16-
for (var i = 0; i < 100; ++i) {
17-
for (var j = 0; j < 10000; ++j) {
18-
memory += text;
19-
}
20-
sleep(0);
21-
}
14+
// Allocate a single large string to deterministically exceed the limit
15+
var memory = new Array(12 * 1024 * 1024).join("x"); // ~12 MB
2216
EOT;
2317

2418
$v8 = new V8Js();

0 commit comments

Comments
 (0)