Skip to content

Commit 80bc973

Browse files
committed
fix tests
1 parent b8257ee commit 80bc973

3 files changed

Lines changed: 77 additions & 73 deletions

File tree

caddy/caddy_test.go

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ package caddy_test
22

33
import (
44
"bytes"
5-
"context"
65
"fmt"
76
"net/http"
8-
"net/url"
97
"os"
108
"path/filepath"
119
"strconv"
@@ -18,7 +16,6 @@ import (
1816
"github.com/caddyserver/caddy/v2/caddytest"
1917
"github.com/dunglas/frankenphp/internal/fastabs"
2018
"github.com/prometheus/client_golang/prometheus/testutil"
21-
"github.com/stretchr/testify/assert"
2219
"github.com/stretchr/testify/require"
2320
)
2421

@@ -1475,72 +1472,3 @@ func TestDd(t *testing.T) {
14751472
"dump123",
14761473
)
14771474
}
1478-
1479-
func TestHotReload(t *testing.T) {
1480-
const topic = "https://frankenphp.dev/hot-reload/test"
1481-
1482-
url := "/.well-known/mercure?topic=" + url.QueryEscape(topic)
1483-
1484-
tmpDir := t.TempDir()
1485-
indexFile := filepath.Join(tmpDir, "index.php")
1486-
1487-
tester := caddytest.NewTester(t)
1488-
tester.InitServer(`
1489-
{
1490-
debug
1491-
skip_install_trust
1492-
admin localhost:2999
1493-
}
1494-
1495-
http://localhost:`+testPort+` {
1496-
mercure {
1497-
transport local
1498-
subscriber_jwt TestKey
1499-
anonymous
1500-
}
1501-
1502-
php_server {
1503-
name test
1504-
root `+tmpDir+`
1505-
hot_reload `+tmpDir+`/*.php
1506-
}
1507-
`, "caddyfile")
1508-
1509-
var connected, received sync.WaitGroup
1510-
1511-
connected.Add(1)
1512-
received.Go(func() {
1513-
cx, cancel := context.WithCancel(t.Context())
1514-
req, _ := http.NewRequest(http.MethodGet, "http://localhost:"+testPort+url, nil)
1515-
req = req.WithContext(cx)
1516-
resp := tester.AssertResponseCode(req, http.StatusOK)
1517-
1518-
connected.Done()
1519-
1520-
var receivedBody strings.Builder
1521-
1522-
buf := make([]byte, 1024)
1523-
for {
1524-
_, err := resp.Body.Read(buf)
1525-
require.NoError(t, err)
1526-
1527-
receivedBody.Write(buf)
1528-
1529-
if strings.Contains(receivedBody.String(), "index.php") {
1530-
cancel()
1531-
1532-
break
1533-
}
1534-
}
1535-
1536-
assert.NoError(t, resp.Body.Close())
1537-
})
1538-
1539-
connected.Wait()
1540-
1541-
require.NoError(t, os.WriteFile(indexFile, []byte(`<?=$_SERVER['FRANKENPHP_HOT_RELOAD'];?>`), 0644))
1542-
1543-
received.Wait()
1544-
1545-
tester.AssertGetResponse("http://localhost:"+testPort+"/index.php", http.StatusOK, url)
1546-
}

caddy/watcher_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
package caddy_test
44

55
import (
6+
"context"
67
"net/http"
8+
"net/url"
9+
"os"
10+
"path/filepath"
11+
"strings"
12+
"sync"
713
"testing"
814

915
"github.com/caddyserver/caddy/v2/caddytest"
16+
"github.com/stretchr/testify/require"
1017
)
1118

1219
func TestWorkerWithInactiveWatcher(t *testing.T) {
@@ -36,3 +43,72 @@ func TestWorkerWithInactiveWatcher(t *testing.T) {
3643
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "requests:1")
3744
tester.AssertGetResponse("http://localhost:"+testPort, http.StatusOK, "requests:2")
3845
}
46+
47+
func TestHotReload(t *testing.T) {
48+
const topic = "https://frankenphp.dev/hot-reload/test"
49+
50+
u := "/.well-known/mercure?topic=" + url.QueryEscape(topic)
51+
52+
tmpDir := t.TempDir()
53+
indexFile := filepath.Join(tmpDir, "index.php")
54+
55+
tester := caddytest.NewTester(t)
56+
tester.InitServer(`
57+
{
58+
debug
59+
skip_install_trust
60+
admin localhost:2999
61+
}
62+
63+
http://localhost:`+testPort+` {
64+
mercure {
65+
transport local
66+
subscriber_jwt TestKey
67+
anonymous
68+
}
69+
70+
php_server {
71+
name test
72+
root `+tmpDir+`
73+
hot_reload `+tmpDir+`/*.php
74+
}
75+
`, "caddyfile")
76+
77+
var connected, received sync.WaitGroup
78+
79+
connected.Add(1)
80+
received.Go(func() {
81+
cx, cancel := context.WithCancel(t.Context())
82+
req, _ := http.NewRequest(http.MethodGet, "http://localhost:"+testPort+u, nil)
83+
req = req.WithContext(cx)
84+
resp := tester.AssertResponseCode(req, http.StatusOK)
85+
86+
connected.Done()
87+
88+
var receivedBody strings.Builder
89+
90+
buf := make([]byte, 1024)
91+
for {
92+
_, err := resp.Body.Read(buf)
93+
require.NoError(t, err)
94+
95+
receivedBody.Write(buf)
96+
97+
if strings.Contains(receivedBody.String(), "index.php") {
98+
cancel()
99+
100+
break
101+
}
102+
}
103+
104+
require.NoError(t, resp.Body.Close())
105+
})
106+
107+
connected.Wait()
108+
109+
require.NoError(t, os.WriteFile(indexFile, []byte("<?=$_SERVER['FRANKENPHP_HOT_RELOAD'];"), 0644))
110+
111+
received.Wait()
112+
113+
tester.AssertGetResponse("http://localhost:"+testPort+"/index.php", http.StatusOK, u)
114+
}

internal/watcher/watcher.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// clang-format off
22
//go:build !nowatcher
33
// clang-format on
4-
#include <wtr/watcher-c.h>
54
#include "_cgo_export.h"
5+
#include <wtr/watcher-c.h>
66

77
void handle_event(struct wtr_watcher_event event, void *_ctx) {
88
go_handle_file_watcher_event(event, (uintptr_t)_ctx);

0 commit comments

Comments
 (0)