|
1 | 1 | package caddy |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "path/filepath" |
5 | 4 | "testing" |
6 | 5 | "time" |
7 | 6 |
|
@@ -249,38 +248,73 @@ func TestModuleWorkerWithCustomName(t *testing.T) { |
249 | 248 | require.Equal(t, "../testdata/worker-with-env.php", module.Workers[0].FileName, "Worker should have the correct filename") |
250 | 249 | } |
251 | 250 |
|
252 | | -func TestCreateUniqueWorkerNames(t *testing.T) { |
253 | | - app := &FrankenPHPApp{} |
254 | | - filename := "../testdata/worker-with-env.php" |
255 | | - absFileName, _ := filepath.Abs(filename) |
256 | | - names := make([]string, 6) |
257 | | - for i := range 3 { |
258 | | - names[i] = app.createUniqueWorkerName(workerConfig{ |
259 | | - FileName: filename, |
260 | | - Name: "custom-worker-name", |
261 | | - }, "") |
262 | | - names[i+3] = app.createUniqueWorkerName(workerConfig{ |
263 | | - FileName: filename, |
264 | | - }, "") |
265 | | - } |
266 | | - |
267 | | - require.Equal(t, "custom-worker-name", names[0]) |
268 | | - require.Equal(t, "custom-worker-name_1", names[1]) |
269 | | - require.Equal(t, "custom-worker-name_2", names[2]) |
270 | | - require.Equal(t, absFileName, names[3]) |
271 | | - require.Equal(t, absFileName+"_1", names[4]) |
272 | | - require.Equal(t, absFileName+"_2", names[5]) |
| 251 | +func TestWorkerBackgroundConfig(t *testing.T) { |
| 252 | + d := caddyfile.NewTestDispenser(` |
| 253 | + { |
| 254 | + php_server { |
| 255 | + worker { |
| 256 | + name jobs |
| 257 | + file ../testdata/worker-with-env.php |
| 258 | + num 2 |
| 259 | + background |
| 260 | + } |
| 261 | + } |
| 262 | + }`) |
| 263 | + module := &FrankenPHPModule{} |
| 264 | + |
| 265 | + require.NoError(t, module.UnmarshalCaddyfile(d)) |
| 266 | + require.Len(t, module.Workers, 1) |
| 267 | + require.True(t, module.Workers[0].Background) |
| 268 | + require.Equal(t, "jobs", module.Workers[0].Name) |
| 269 | +} |
| 270 | + |
| 271 | +func TestWorkerBackgroundRequiresName(t *testing.T) { |
| 272 | + d := caddyfile.NewTestDispenser(` |
| 273 | + { |
| 274 | + php_server { |
| 275 | + worker { |
| 276 | + file ../testdata/worker-with-env.php |
| 277 | + background |
| 278 | + } |
| 279 | + } |
| 280 | + }`) |
| 281 | + module := &FrankenPHPModule{} |
| 282 | + |
| 283 | + err := module.UnmarshalCaddyfile(d) |
| 284 | + require.ErrorContains(t, err, `background workers must have an explicit "name"`) |
273 | 285 | } |
274 | 286 |
|
275 | | -func TestCreateUniqueWorkerNamesQualifiedByServer(t *testing.T) { |
276 | | - app := &FrankenPHPApp{} |
277 | | - wc := workerConfig{FileName: "../testdata/worker-with-env.php", Name: "queue"} |
278 | | - |
279 | | - require.Equal(t, "queue", app.createUniqueWorkerName(wc, "one.example.com")) |
280 | | - // on collision, the name is qualified with the server name |
281 | | - require.Equal(t, "two.example.com:queue", app.createUniqueWorkerName(wc, "two.example.com")) |
282 | | - // when the qualified name is also taken, fall back to the numeric postfix |
283 | | - require.Equal(t, "queue_1", app.createUniqueWorkerName(wc, "two.example.com")) |
284 | | - // workers without a server keep the numeric postfix behavior |
285 | | - require.Equal(t, "queue_2", app.createUniqueWorkerName(wc, "")) |
| 287 | +func TestWorkerBackgroundRequiresNum(t *testing.T) { |
| 288 | + d := caddyfile.NewTestDispenser(` |
| 289 | + { |
| 290 | + php_server { |
| 291 | + worker { |
| 292 | + name jobs |
| 293 | + file ../testdata/worker-with-env.php |
| 294 | + background |
| 295 | + } |
| 296 | + } |
| 297 | + }`) |
| 298 | + module := &FrankenPHPModule{} |
| 299 | + |
| 300 | + err := module.UnmarshalCaddyfile(d) |
| 301 | + require.ErrorContains(t, err, `background workers must declare "num" >= 1`) |
| 302 | +} |
| 303 | + |
| 304 | +func TestWorkerBackgroundRejectsMatch(t *testing.T) { |
| 305 | + d := caddyfile.NewTestDispenser(` |
| 306 | + { |
| 307 | + php_server { |
| 308 | + worker { |
| 309 | + name jobs |
| 310 | + file ../testdata/worker-with-env.php |
| 311 | + match /jobs/* |
| 312 | + background |
| 313 | + } |
| 314 | + } |
| 315 | + }`) |
| 316 | + module := &FrankenPHPModule{} |
| 317 | + |
| 318 | + err := module.UnmarshalCaddyfile(d) |
| 319 | + require.ErrorContains(t, err, `"match" is not supported for background workers`) |
286 | 320 | } |
0 commit comments