Skip to content

Commit 958537e

Browse files
committed
add more tests
1 parent 53795c7 commit 958537e

3 files changed

Lines changed: 161 additions & 137 deletions

File tree

caddy/caddy.go

Lines changed: 87 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func init() {
5151
}
5252

5353
type workerConfig struct {
54-
// Name for the worker. Default: the filename for FrankenPHPApp workers, filename + environment variables for FrankenPHPModule workers.
54+
// Name for the worker. Default: the filename for FrankenPHPApp workers, always prefixed with "m#" for FrankenPHPModule workers.
5555
Name string `json:"name,omitempty"`
5656
// FileName sets the path to the worker script.
5757
FileName string `json:"file_name,omitempty"`
@@ -148,6 +148,86 @@ func (f *FrankenPHPApp) Stop() error {
148148
return nil
149149
}
150150

151+
func parseWorkerConfig(d *caddyfile.Dispenser) (workerConfig, error) {
152+
wc := workerConfig{}
153+
if d.NextArg() {
154+
wc.FileName = d.Val()
155+
}
156+
157+
if d.NextArg() {
158+
if d.Val() == "watch" {
159+
wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
160+
} else {
161+
v, err := strconv.Atoi(d.Val())
162+
if err != nil {
163+
return wc, err
164+
}
165+
166+
wc.Num = v
167+
}
168+
}
169+
170+
if d.NextArg() {
171+
return wc, errors.New("FrankenPHP: too many 'worker' arguments: " + d.Val())
172+
}
173+
174+
for d.NextBlock(1) {
175+
v := d.Val()
176+
switch v {
177+
case "name":
178+
if !d.NextArg() {
179+
return wc, d.ArgErr()
180+
}
181+
wc.Name = d.Val()
182+
case "file":
183+
if !d.NextArg() {
184+
return wc, d.ArgErr()
185+
}
186+
wc.FileName = d.Val()
187+
case "num":
188+
if !d.NextArg() {
189+
return wc, d.ArgErr()
190+
}
191+
192+
v, err := strconv.Atoi(d.Val())
193+
if err != nil {
194+
return wc, err
195+
}
196+
197+
wc.Num = v
198+
case "env":
199+
args := d.RemainingArgs()
200+
if len(args) != 2 {
201+
return wc, d.ArgErr()
202+
}
203+
if wc.Env == nil {
204+
wc.Env = make(map[string]string)
205+
}
206+
wc.Env[args[0]] = args[1]
207+
case "watch":
208+
if !d.NextArg() {
209+
// the default if the watch directory is left empty:
210+
wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
211+
} else {
212+
wc.Watch = append(wc.Watch, d.Val())
213+
}
214+
default:
215+
allowedDirectives := "name, file, num, env, watch"
216+
return wc, wrongSubDirectiveError("worker", allowedDirectives, v)
217+
}
218+
}
219+
220+
if wc.FileName == "" {
221+
return wc, errors.New(`the "file" argument must be specified`)
222+
}
223+
224+
if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(wc.FileName) {
225+
wc.FileName = filepath.Join(frankenphp.EmbeddedAppPath, wc.FileName)
226+
}
227+
228+
return wc, nil
229+
}
230+
151231
// UnmarshalCaddyfile implements caddyfile.Unmarshaler.
152232
func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
153233
for d.Next() {
@@ -229,82 +309,10 @@ func (f *FrankenPHPApp) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
229309
}
230310

231311
case "worker":
232-
wc := workerConfig{}
233-
if d.NextArg() {
234-
wc.FileName = d.Val()
235-
}
236-
237-
if d.NextArg() {
238-
if d.Val() == "watch" {
239-
wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
240-
} else {
241-
v, err := strconv.Atoi(d.Val())
242-
if err != nil {
243-
return err
244-
}
245-
246-
wc.Num = v
247-
}
248-
}
249-
250-
if d.NextArg() {
251-
return errors.New("FrankenPHP: too many 'worker' arguments: " + d.Val())
252-
}
253-
254-
for d.NextBlock(1) {
255-
v := d.Val()
256-
switch v {
257-
case "name":
258-
if !d.NextArg() {
259-
return d.ArgErr()
260-
}
261-
wc.Name = d.Val()
262-
case "file":
263-
if !d.NextArg() {
264-
return d.ArgErr()
265-
}
266-
wc.FileName = d.Val()
267-
case "num":
268-
if !d.NextArg() {
269-
return d.ArgErr()
270-
}
271-
272-
v, err := strconv.Atoi(d.Val())
273-
if err != nil {
274-
return err
275-
}
276-
277-
wc.Num = v
278-
case "env":
279-
args := d.RemainingArgs()
280-
if len(args) != 2 {
281-
return d.ArgErr()
282-
}
283-
if wc.Env == nil {
284-
wc.Env = make(map[string]string)
285-
}
286-
wc.Env[args[0]] = args[1]
287-
case "watch":
288-
if !d.NextArg() {
289-
// the default if the watch directory is left empty:
290-
wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
291-
} else {
292-
wc.Watch = append(wc.Watch, d.Val())
293-
}
294-
default:
295-
allowedDirectives := "name, file, num, env, watch"
296-
return wrongSubDirectiveError("worker", allowedDirectives, v)
297-
}
298-
}
299-
300-
if wc.FileName == "" {
301-
return errors.New(`the "file" argument must be specified`)
302-
}
303-
304-
if frankenphp.EmbeddedAppPath != "" && filepath.IsLocal(wc.FileName) {
305-
wc.FileName = filepath.Join(frankenphp.EmbeddedAppPath, wc.FileName)
312+
wc, err := parseWorkerConfig(d)
313+
if err != nil {
314+
return err
306315
}
307-
308316
if wc.Name == "" {
309317
// let worker initialization validate if the FileName is valid or not
310318
name, _ := fastabs.FastAbs(wc.FileName)
@@ -545,62 +553,9 @@ func (f *FrankenPHPModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
545553
for d.Next() {
546554
for d.NextBlock(0) {
547555
if d.Val() == "worker" {
548-
wc := workerConfig{}
549-
if d.NextArg() {
550-
wc.FileName = d.Val()
551-
}
552-
553-
if d.NextArg() {
554-
v, err := strconv.Atoi(d.Val())
555-
if err != nil {
556-
return err
557-
}
558-
wc.Num = v
559-
}
560-
561-
for d.NextBlock(1) {
562-
switch d.Val() {
563-
case "name":
564-
if !d.NextArg() {
565-
return d.ArgErr()
566-
}
567-
wc.Name = d.Val()
568-
case "file":
569-
if !d.NextArg() {
570-
return d.ArgErr()
571-
}
572-
wc.FileName = d.Val()
573-
case "num":
574-
if !d.NextArg() {
575-
return d.ArgErr()
576-
}
577-
v, err := strconv.Atoi(d.Val())
578-
if err != nil {
579-
return err
580-
}
581-
wc.Num = v
582-
case "env":
583-
args := d.RemainingArgs()
584-
if len(args) != 2 {
585-
return d.ArgErr()
586-
}
587-
if wc.Env == nil {
588-
wc.Env = make(map[string]string)
589-
}
590-
wc.Env[args[0]] = args[1]
591-
case "watch":
592-
if !d.NextArg() {
593-
wc.Watch = append(wc.Watch, "./**/*.{php,yaml,yml,twig,env}")
594-
} else {
595-
wc.Watch = append(wc.Watch, d.Val())
596-
}
597-
default:
598-
return fmt.Errorf("unknown worker subdirective: %s", d.Val())
599-
}
600-
}
601-
602-
if wc.FileName == "" {
603-
return errors.New(`the "file" argument must be specified`)
556+
wc, err := parseWorkerConfig(d)
557+
if err != nil {
558+
return err
604559
}
605560

606561
// Inherit environment variables from the parent php_server directive

caddy/caddy_test.go

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func TestWorker(t *testing.T) {
120120
wg.Wait()
121121
}
122122

123-
func TestGlobalAndLocalWorker(t *testing.T) {
123+
func TestGlobalAndModuleWorker(t *testing.T) {
124124
var wg sync.WaitGroup
125125
testPortNum, _ := strconv.Atoi(testPort)
126126
testPortTwo := strconv.Itoa(testPortNum + 1)
@@ -146,7 +146,7 @@ func TestGlobalAndLocalWorker(t *testing.T) {
146146
worker {
147147
file worker-with-env.php
148148
num 2
149-
env APP_ENV local
149+
env APP_ENV module
150150
}
151151
}
152152
}
@@ -161,18 +161,88 @@ func TestGlobalAndLocalWorker(t *testing.T) {
161161
}
162162
`, "caddyfile")
163163

164-
for i := 0; i < 2; i++ {
164+
for i := 0; i < 10; i++ {
165165
wg.Add(1)
166166

167167
go func(i int) {
168-
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=local")
168+
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=module")
169169
tester.AssertGetResponse("http://localhost:"+testPortTwo+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=global")
170170
wg.Done()
171171
}(i)
172172
}
173173
wg.Wait()
174174
}
175175

176+
func TestNamedModuleWorkers(t *testing.T) {
177+
var wg sync.WaitGroup
178+
testPortNum, _ := strconv.Atoi(testPort)
179+
testPortTwo := strconv.Itoa(testPortNum + 1)
180+
testPortThree := strconv.Itoa(testPortNum + 2)
181+
tester := caddytest.NewTester(t)
182+
tester.InitServer(`
183+
{
184+
skip_install_trust
185+
admin localhost:2999
186+
187+
frankenphp {
188+
worker {
189+
file ../testdata/worker-with-env.php
190+
num 1
191+
env APP_ENV global
192+
}
193+
}
194+
}
195+
196+
http://localhost:`+testPort+` {
197+
route {
198+
php {
199+
root ../testdata
200+
worker {
201+
file worker-with-env.php
202+
num 2
203+
env APP_ENV one
204+
name module1
205+
}
206+
}
207+
}
208+
}
209+
210+
http://localhost:`+testPortTwo+` {
211+
route {
212+
php {
213+
root ../testdata
214+
worker {
215+
file worker-with-env.php
216+
num 1
217+
env APP_ENV two
218+
name module2
219+
}
220+
}
221+
}
222+
}
223+
224+
http://localhost:`+testPortThree+` {
225+
route {
226+
php {
227+
root ../testdata
228+
}
229+
}
230+
}
231+
`, "caddyfile")
232+
233+
for i := 0; i < 10; i++ {
234+
wg.Add(1)
235+
236+
go func(i int) {
237+
tester.AssertGetResponse("http://localhost:"+testPort+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=one")
238+
tester.AssertGetResponse("http://localhost:"+testPortTwo+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=two")
239+
tester.AssertGetResponse("http://localhost:"+testPortThree+"/worker-with-env.php", http.StatusOK, "Worker has APP_ENV=global")
240+
wg.Done()
241+
}(i)
242+
}
243+
wg.Wait()
244+
}
245+
176246
func TestEnv(t *testing.T) {
177247
tester := caddytest.NewTester(t)
178248
tester.InitServer(`

worker.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ func newWorker(o workerOpt) (*worker, error) {
103103
env: o.env,
104104
requestChan: make(chan *frankenPHPContext),
105105
}
106-
107106
workers[key] = w
108107

109108
return w, nil

0 commit comments

Comments
 (0)