Skip to content

Commit 1f48432

Browse files
tests: opcache_preload (#2257)
Adds a test to reproduce #2254 and verify `opcache_preload` works as intended with the changes from #2252.
1 parent c1e30cd commit 1f48432

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

frankenphp_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"net/url"
2323
"os"
2424
"os/exec"
25+
"os/user"
2526
"path/filepath"
2627
"strconv"
2728
"strings"
@@ -1302,3 +1303,30 @@ func TestSessionNoLeakAfterExit_worker(t *testing.T) {
13021303
realServer: true,
13031304
})
13041305
}
1306+
1307+
func TestOpcachePreload_module(t *testing.T) {
1308+
testOpcachePreload(t, &testOptions{env: map[string]string{"TEST": "123"}})
1309+
}
1310+
1311+
func TestOpcachePreload_worker(t *testing.T) {
1312+
testOpcachePreload(t, &testOptions{workerScript: "preload-check.php", nbWorkers: 1, nbParallelRequests: 1, env: map[string]string{"TEST": "123"}})
1313+
}
1314+
1315+
func testOpcachePreload(t *testing.T, opts *testOptions) {
1316+
cwd, _ := os.Getwd()
1317+
preloadScript := cwd + "/testdata/preload.php"
1318+
1319+
u, err := user.Current()
1320+
require.NoError(t, err)
1321+
1322+
opts.phpIni = map[string]string{
1323+
"opcache.enable": "1",
1324+
"opcache.preload": preloadScript,
1325+
"opcache.preload_user": u.Username,
1326+
}
1327+
1328+
runTest(t, func(handler func(http.ResponseWriter, *http.Request), _ *httptest.Server, i int) {
1329+
body, _ := testGet("http://example.com/preload-check.php", handler, t)
1330+
assert.Equal(t, "I am preloaded", body)
1331+
}, opts)
1332+
}

testdata/preload-check.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
require_once __DIR__.'/_executor.php';
4+
5+
return function () {
6+
if (function_exists('preloaded_function')) {
7+
echo preloaded_function();
8+
} else {
9+
echo 'not preloaded';
10+
}
11+
};
12+

testdata/preload.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
// verify ENV can be accessed during preload
4+
$_ENV['TEST'] = '123';
5+
function preloaded_function(): string
6+
{
7+
return 'I am preloaded';
8+
}

0 commit comments

Comments
 (0)