summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-09-23 15:55:57 +0300
committerGitHub <[email protected]>2018-09-23 15:55:57 +0300
commit6628249e68a85e6c2fed6d5802fa247388b053dc (patch)
treedef759b509dbd32569afc8229c8888fa6599e1bf
parentbdff4b25d2a879357bc0ed53e96c0b551de07f88 (diff)
parenteb64ebee3c77522202c5163513e7318bd630f8be (diff)
Merge pull request #37 from spiral/feature/1.3.0
Feature/1.3.0
-rw-r--r--.gitignore3
-rw-r--r--.travis.yml35
-rw-r--r--CHANGELOG.md8
-rwxr-xr-xbuild.sh2
-rw-r--r--composer.json2
-rw-r--r--config.go8
-rw-r--r--php-src/Exceptions/RoadRunnerException.php13
-rw-r--r--pipe_factory_test.go20
-rw-r--r--server_config.go21
-rw-r--r--server_config_test.go10
-rw-r--r--server_test.go24
-rw-r--r--service/container.go17
-rw-r--r--service/container_test.go69
-rw-r--r--service/env/config.go6
-rw-r--r--service/env/config_test.go6
-rw-r--r--service/http/config.go21
-rw-r--r--service/http/config_test.go16
-rw-r--r--service/http/handler.go2
-rw-r--r--service/http/handler_test.go36
-rw-r--r--service/http/request.go2
-rw-r--r--service/http/rpc_test.go6
-rw-r--r--service/http/service.go25
-rw-r--r--service/http/service_test.go43
-rw-r--r--service/http/uploads_config.go6
-rw-r--r--service/http/uploads_test.go8
-rw-r--r--service/rpc/config.go8
-rw-r--r--service/rpc/config_test.go14
-rw-r--r--service/rpc/service_test.go13
-rw-r--r--service/static/service_test.go30
-rw-r--r--socket_factory_test.go36
-rw-r--r--src/Exception/RoadRunnerException.php13
-rw-r--r--src/Exceptions/RoadRunnerException.php17
-rw-r--r--src/PSR7Client.php (renamed from php-src/PSR7Client.php)0
-rw-r--r--src/Worker.php (renamed from php-src/Worker.php)2
-rw-r--r--static_pool.go13
-rw-r--r--static_pool_test.go116
-rw-r--r--tests/broken.php (renamed from php-src/tests/broken.php)0
-rw-r--r--tests/client.php (renamed from php-src/tests/client.php)2
-rw-r--r--tests/delay.php (renamed from php-src/tests/delay.php)0
-rw-r--r--tests/echo.php (renamed from php-src/tests/echo.php)0
-rw-r--r--tests/error.php (renamed from php-src/tests/error.php)0
-rw-r--r--tests/failboot.php (renamed from php-src/tests/failboot.php)0
-rw-r--r--tests/head.php (renamed from php-src/tests/head.php)0
-rw-r--r--tests/http/client.php (renamed from php-src/tests/http/client.php)2
-rw-r--r--tests/http/cookie.php (renamed from php-src/tests/http/cookie.php)0
-rw-r--r--tests/http/data.php (renamed from php-src/tests/http/data.php)0
-rw-r--r--tests/http/echo.php (renamed from php-src/tests/http/echo.php)0
-rw-r--r--tests/http/echoerr.php (renamed from php-src/tests/http/echoerr.php)0
-rw-r--r--tests/http/env.php (renamed from php-src/tests/http/env.php)0
-rw-r--r--tests/http/error.php (renamed from php-src/tests/http/error.php)0
-rw-r--r--tests/http/error2.php (renamed from php-src/tests/http/error2.php)0
-rw-r--r--tests/http/header.php (renamed from php-src/tests/http/header.php)0
-rw-r--r--tests/http/payload.php (renamed from php-src/tests/http/payload.php)0
-rw-r--r--tests/http/pid.php (renamed from php-src/tests/http/pid.php)0
-rw-r--r--tests/http/upload.php (renamed from php-src/tests/http/upload.php)0
-rw-r--r--tests/pid.php (renamed from php-src/tests/pid.php)0
-rw-r--r--tests/sample.txt (renamed from php-src/tests/sample.txt)0
-rw-r--r--tests/slow-client.php (renamed from php-src/tests/slow-client.php)2
-rw-r--r--tests/slow-destroy.php37
-rw-r--r--tests/stop.php (renamed from php-src/tests/stop.php)0
-rw-r--r--worker_test.go28
61 files changed, 522 insertions, 220 deletions
diff --git a/.gitignore b/.gitignore
index 32f835d7..4d5c82e9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
.idea/*
composer.lock
vendor/
-builds/ \ No newline at end of file
+builds/
+tests/vendor/ \ No newline at end of file
diff --git a/.travis.yml b/.travis.yml
index 8fd4f66d..d2edddbf 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -36,4 +36,37 @@ after_success:
- bash <(curl -s https://codecov.io/bash) -f env.txt
- bash <(curl -s https://codecov.io/bash) -f rpc.txt
- bash <(curl -s https://codecov.io/bash) -f http.txt
- - bash <(curl -s https://codecov.io/bash) -f static.txt \ No newline at end of file
+ - bash <(curl -s https://codecov.io/bash) -f static.txt
+
+jobs:
+ include:
+ - stage: Test
+ env: "PHP=7.0"
+ before_install:
+ - go version
+ - sudo add-apt-repository -y ppa:ondrej/php
+ - sudo apt-get update
+ - sudo apt-get install -y php7.0-cli
+ - sudo cp `which php7.0` `which php`
+ - php -v
+ - composer self-update
+ - stage: Test
+ env: "PHP=7.1"
+ before_install:
+ - go version
+ - sudo add-apt-repository -y ppa:ondrej/php
+ - sudo apt-get update
+ - sudo apt-get install -y php7.1-cli
+ - sudo cp `which php7.1` `which php`
+ - php -v
+ - composer self-update
+ - stage: Test
+ env: "PHP=7.2"
+ before_install:
+ - go version
+ - sudo add-apt-repository -y ppa:ondrej/php
+ - sudo apt-get update
+ - sudo apt-get install -y php7.2-cli
+ - sudo cp `which php7.2` `which php`
+ - php -v
+ - composer self-update \ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 57bd5cde..52eccc47 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,6 +1,14 @@
CHANGELOG
=========
+v1.2.2 (23.09.2018)
+- new project directory structure
+- introduces DefaultsConfig, allows to keep config files smaller
+- better worker pool destruction while working with long running processes
+- added more php versions to travis config
+- `Spiral\RoadRunner\Exceptions\RoadRunnerException` is marked as deprecated in favor of `Spiral\RoadRunner\Exception\RoadRunnerException`
+- improved test coverage
+
v1.2.1 (21.09.2018)
------
- added RR_HTTP env variable to php processes run under http service
diff --git a/build.sh b/build.sh
index dbcd4da4..1c2d8128 100755
--- a/build.sh
+++ b/build.sh
@@ -2,7 +2,7 @@
cd $(dirname "${BASH_SOURCE[0]}")
OD="$(pwd)"
# Pushes application version into the build information.
-RR_VERSION=1.2.1
+RR_VERSION=1.2.2
# Hardcode some values to the core package
LDFLAGS="$LDFLAGS -X github.com/spiral/roadrunner/cmd/rr/cmd.Version=${RR_VERSION}"
diff --git a/composer.json b/composer.json
index 5d946f49..7be3ecfe 100644
--- a/composer.json
+++ b/composer.json
@@ -17,7 +17,7 @@
},
"autoload": {
"psr-4": {
- "Spiral\\RoadRunner\\": "php-src/"
+ "Spiral\\RoadRunner\\": "src/"
}
}
}
diff --git a/config.go b/config.go
index 929ca806..23f1afc8 100644
--- a/config.go
+++ b/config.go
@@ -25,6 +25,14 @@ type Config struct {
DestroyTimeout time.Duration
}
+// InitDefaults allows to init blank config with pre-defined set of default values.
+func (cfg *Config) InitDefaults() error {
+ cfg.AllocateTimeout = time.Minute
+ cfg.DestroyTimeout = time.Minute
+
+ return nil
+}
+
// Valid returns error if config not valid.
func (cfg *Config) Valid() error {
if cfg.NumWorkers == 0 {
diff --git a/php-src/Exceptions/RoadRunnerException.php b/php-src/Exceptions/RoadRunnerException.php
deleted file mode 100644
index fa7b8da3..00000000
--- a/php-src/Exceptions/RoadRunnerException.php
+++ /dev/null
@@ -1,13 +0,0 @@
-<?php
-/**
- * High-performance PHP process supervisor and load balancer written in Go
- *
- * @author Wolfy-J
- */
-
-namespace Spiral\RoadRunner\Exceptions;
-
-class RoadRunnerException extends \RuntimeException
-{
-
-} \ No newline at end of file
diff --git a/pipe_factory_test.go b/pipe_factory_test.go
index ae276ab6..9d50e47f 100644
--- a/pipe_factory_test.go
+++ b/pipe_factory_test.go
@@ -7,7 +7,7 @@ import (
)
func Test_Pipe_Start(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, err := NewPipeFactory().SpawnWorker(cmd)
assert.NoError(t, err)
@@ -21,7 +21,7 @@ func Test_Pipe_Start(t *testing.T) {
}
func Test_Pipe_StartError(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
cmd.Start()
w, err := NewPipeFactory().SpawnWorker(cmd)
@@ -30,7 +30,7 @@ func Test_Pipe_StartError(t *testing.T) {
}
func Test_Pipe_PipeError(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
cmd.StdinPipe()
w, err := NewPipeFactory().SpawnWorker(cmd)
@@ -39,7 +39,7 @@ func Test_Pipe_PipeError(t *testing.T) {
}
func Test_Pipe_PipeError2(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
cmd.StdoutPipe()
w, err := NewPipeFactory().SpawnWorker(cmd)
@@ -48,7 +48,7 @@ func Test_Pipe_PipeError2(t *testing.T) {
}
func Test_Pipe_Failboot(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/failboot.php")
+ cmd := exec.Command("php", "tests/failboot.php")
w, err := NewPipeFactory().SpawnWorker(cmd)
assert.Nil(t, w)
@@ -57,7 +57,7 @@ func Test_Pipe_Failboot(t *testing.T) {
}
func Test_Pipe_Invalid(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/invalid.php")
+ cmd := exec.Command("php", "tests/invalid.php")
w, err := NewPipeFactory().SpawnWorker(cmd)
assert.Error(t, err)
@@ -65,7 +65,7 @@ func Test_Pipe_Invalid(t *testing.T) {
}
func Test_Pipe_Echo(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -84,7 +84,7 @@ func Test_Pipe_Echo(t *testing.T) {
}
func Test_Pipe_Broken(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "broken", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "broken", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -104,7 +104,7 @@ func Test_Pipe_Broken(t *testing.T) {
func Benchmark_Pipe_SpawnWorker_Stop(b *testing.B) {
f := NewPipeFactory()
for n := 0; n < b.N; n++ {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := f.SpawnWorker(cmd)
go func() {
if w.Wait() != nil {
@@ -117,7 +117,7 @@ func Benchmark_Pipe_SpawnWorker_Stop(b *testing.B) {
}
func Benchmark_Pipe_Worker_ExecEcho(b *testing.B) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
diff --git a/server_config.go b/server_config.go
index e2f9266b..936744a1 100644
--- a/server_config.go
+++ b/server_config.go
@@ -33,27 +33,16 @@ type ServerConfig struct {
env []string
}
-// SetDefaults sets missing values to their default values.
-func (cfg *ServerConfig) SetDefaults() {
- if cfg.Relay == "" {
- cfg.Relay = "pipes"
- }
-
- if cfg.RelayTimeout == 0 {
- cfg.RelayTimeout = time.Minute
- }
+// InitDefaults sets missing values to their default values.
+func (cfg *ServerConfig) InitDefaults() error {
+ cfg.Relay = "pipes"
+ cfg.RelayTimeout = time.Minute
if cfg.Pool == nil {
cfg.Pool = &Config{}
}
- if cfg.Pool.AllocateTimeout == 0 {
- cfg.Pool.AllocateTimeout = time.Minute
- }
-
- if cfg.Pool.DestroyTimeout == 0 {
- cfg.Pool.DestroyTimeout = time.Minute
- }
+ return cfg.Pool.InitDefaults()
}
// UpscaleDurations converts duration values from nanoseconds to seconds.
diff --git a/server_config_test.go b/server_config_test.go
index e116323d..ec29412e 100644
--- a/server_config_test.go
+++ b/server_config_test.go
@@ -85,7 +85,7 @@ func Test_ServerConfig_ErrorMethod(t *testing.T) {
func Test_ServerConfig_Cmd(t *testing.T) {
cfg := &ServerConfig{
- Command: "php php-src/tests/client.php pipes",
+ Command: "php tests/client.php pipes",
}
cmd := cfg.makeCommand()
@@ -94,7 +94,7 @@ func Test_ServerConfig_Cmd(t *testing.T) {
func Test_ServerConfig_SetEnv(t *testing.T) {
cfg := &ServerConfig{
- Command: "php php-src/tests/client.php pipes",
+ Command: "php tests/client.php pipes",
}
cfg.SetEnv("key", "value")
@@ -109,10 +109,10 @@ func Test_ServerConfig_SetEnv(t *testing.T) {
func Test_ServerConfigDefaults(t *testing.T) {
cfg := &ServerConfig{
- Command: "php php-src/tests/client.php pipes",
+ Command: "php tests/client.php pipes",
}
- cfg.SetDefaults()
+ cfg.InitDefaults()
assert.Equal(t, "pipes", cfg.Relay)
assert.Equal(t, time.Minute, cfg.Pool.AllocateTimeout)
@@ -121,7 +121,7 @@ func Test_ServerConfigDefaults(t *testing.T) {
func Test_Config_Upscale(t *testing.T) {
cfg := &ServerConfig{
- Command: "php php-src/tests/client.php pipes",
+ Command: "php tests/client.php pipes",
RelayTimeout: 1,
Pool: &Config{
AllocateTimeout: 1,
diff --git a/server_test.go b/server_test.go
index 846b1040..4646ebc9 100644
--- a/server_test.go
+++ b/server_test.go
@@ -11,7 +11,7 @@ import (
func TestServer_PipesEcho(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: int64(runtime.NumCPU()),
@@ -36,7 +36,7 @@ func TestServer_PipesEcho(t *testing.T) {
func TestServer_SocketEcho(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo tcp",
+ Command: "php tests/client.php echo tcp",
Relay: "tcp://:9007",
RelayTimeout: 10 * time.Second,
Pool: &Config{
@@ -62,7 +62,7 @@ func TestServer_SocketEcho(t *testing.T) {
func TestServer_Configure_BeforeStart(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: int64(runtime.NumCPU()),
@@ -73,7 +73,7 @@ func TestServer_Configure_BeforeStart(t *testing.T) {
defer srv.Stop()
err := srv.Reconfigure(&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 2,
@@ -99,7 +99,7 @@ func TestServer_Configure_BeforeStart(t *testing.T) {
func TestServer_Stop_NotStarted(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: int64(runtime.NumCPU()),
@@ -115,7 +115,7 @@ func TestServer_Stop_NotStarted(t *testing.T) {
func TestServer_Reconfigure(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 1,
@@ -129,7 +129,7 @@ func TestServer_Reconfigure(t *testing.T) {
assert.Len(t, srv.Workers(), 1)
err := srv.Reconfigure(&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 2,
@@ -145,7 +145,7 @@ func TestServer_Reconfigure(t *testing.T) {
func TestServer_Reset(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 1,
@@ -167,7 +167,7 @@ func TestServer_Reset(t *testing.T) {
func TestServer_ReplacePool(t *testing.T) {
srv := NewServer(
&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 1,
@@ -196,7 +196,7 @@ func TestServer_ReplacePool(t *testing.T) {
func TestServer_ServerFailure(t *testing.T) {
srv := NewServer(&ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &Config{
NumWorkers: 1,
@@ -216,9 +216,9 @@ func TestServer_ServerFailure(t *testing.T) {
})
// emulating potential server failure
- srv.cfg.Command = "php php-src/tests/client.php echo broken-connection"
+ srv.cfg.Command = "php tests/client.php echo broken-connection"
srv.pool.(*StaticPool).cmd = func() *exec.Cmd {
- return exec.Command("php", "php-src/tests/client.php", "echo", "broken-connection")
+ return exec.Command("php", "tests/client.php", "echo", "broken-connection")
}
// killing random worker and expecting pool to replace it
diff --git a/service/container.go b/service/container.go
index 861e1aac..fc1012c8 100644
--- a/service/container.go
+++ b/service/container.go
@@ -66,6 +66,11 @@ type HydrateConfig interface {
Hydrate(cfg Config) error
}
+type DefaultsConfig interface {
+ // InitDefaults allows to init blank config with pre-defined set of default values.
+ InitDefaults() error
+}
+
type container struct {
log logrus.FieldLogger
mu sync.Mutex
@@ -257,11 +262,19 @@ func (c *container) resolveValues(s interface{}, m reflect.Method, cfg Config) (
values = append(values, reflect.ValueOf(c.log))
case v.Implements(reflect.TypeOf((*HydrateConfig)(nil)).Elem()): // injectable config
- if cfg == nil {
+ sc := reflect.New(v.Elem())
+
+ if dsc, ok := sc.Interface().(DefaultsConfig); ok {
+ dsc.InitDefaults()
+ if cfg == nil {
+ values = append(values, sc)
+ continue
+ }
+
+ } else if cfg == nil {
return nil, errNoConfig
}
- sc := reflect.New(v.Elem())
if err := sc.Interface().(HydrateConfig).Hydrate(cfg); err != nil {
return nil, err
}
diff --git a/service/container_test.go b/service/container_test.go
index efccc182..8eeb647a 100644
--- a/service/container_test.go
+++ b/service/container_test.go
@@ -79,6 +79,37 @@ func (cfg *testCfg) Get(name string) Config {
}
func (cfg *testCfg) Unmarshal(out interface{}) error { return json.Unmarshal([]byte(cfg.cfg), out) }
+// Config defines RPC service config.
+type dConfig struct {
+ // Indicates if RPC connection is enabled.
+ Value string
+}
+
+// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
+func (c *dConfig) Hydrate(cfg Config) error {
+ if err := cfg.Unmarshal(c); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// InitDefaults allows to init blank config with pre-defined set of default values.
+func (c *dConfig) InitDefaults() error {
+ c.Value = "default"
+
+ return nil
+}
+
+type dService struct {
+ Cfg *dConfig
+}
+
+func (s *dService) Init(cfg *dConfig) (bool, error) {
+ s.Cfg = cfg
+ return true, nil
+}
+
func TestContainer_Register(t *testing.T) {
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
@@ -148,6 +179,44 @@ func TestContainer_Configure(t *testing.T) {
assert.Equal(t, StatusOK, st)
}
+func TestContainer_Init_Default(t *testing.T) {
+ logger, hook := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ svc := &dService{}
+
+ c := NewContainer(logger)
+ c.Register("test", svc)
+ assert.Equal(t, 1, len(hook.Entries))
+
+ assert.NoError(t, c.Init(&testCfg{`{}`}))
+
+ s, st := c.Get("test")
+ assert.IsType(t, &dService{}, s)
+ assert.Equal(t, StatusOK, st)
+
+ assert.Equal(t, "default", svc.Cfg.Value)
+}
+
+func TestContainer_Init_Default_Overwrite(t *testing.T) {
+ logger, hook := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ svc := &dService{}
+
+ c := NewContainer(logger)
+ c.Register("test", svc)
+ assert.Equal(t, 1, len(hook.Entries))
+
+ assert.NoError(t, c.Init(&testCfg{`{"test":{"value": "something"}}`}))
+
+ s, st := c.Get("test")
+ assert.IsType(t, &dService{}, s)
+ assert.Equal(t, StatusOK, st)
+
+ assert.Equal(t, "something", svc.Cfg.Value)
+}
+
func TestContainer_ConfigureNull(t *testing.T) {
logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.DebugLevel)
diff --git a/service/env/config.go b/service/env/config.go
index d0ba686b..a7da695e 100644
--- a/service/env/config.go
+++ b/service/env/config.go
@@ -14,3 +14,9 @@ type Config struct {
func (c *Config) Hydrate(cfg service.Config) error {
return cfg.Unmarshal(&c.Values)
}
+
+// InitDefaults allows to init blank config with pre-defined set of default values.
+func (c *Config) InitDefaults() error {
+ c.Values = make(map[string]string)
+ return nil
+}
diff --git a/service/env/config_test.go b/service/env/config_test.go
index 3ae2afbc..50fbdaa5 100644
--- a/service/env/config_test.go
+++ b/service/env/config_test.go
@@ -27,3 +27,9 @@ func Test_Config_Hydrate_Empty(t *testing.T) {
assert.NoError(t, c.Hydrate(cfg))
assert.Len(t, c.Values, 0)
}
+
+func Test_Config_Defaults(t *testing.T) {
+ c := &Config{}
+ c.InitDefaults()
+ assert.Len(t, c.Values, 0)
+}
diff --git a/service/http/config.go b/service/http/config.go
index 5be42ae6..b11d807c 100644
--- a/service/http/config.go
+++ b/service/http/config.go
@@ -9,9 +9,6 @@ import (
// Config configures RoadRunner HTTP server.
type Config struct {
- // Enable enables http service.
- Enable bool
-
// Address and port to handle as http server.
Address string
@@ -27,21 +24,27 @@ type Config struct {
// Hydrate must populate Config values using given Config source. Must return error if Config is not valid.
func (c *Config) Hydrate(cfg service.Config) error {
- if err := cfg.Unmarshal(c); err != nil {
- return err
+ if c.Workers == nil {
+ c.Workers = &roadrunner.ServerConfig{}
}
- if !c.Enable {
- return nil
+ if c.Uploads == nil {
+ c.Uploads = &UploadsConfig{}
}
- if err := c.Valid(); err != nil {
+ c.Uploads.InitDefaults()
+ c.Workers.InitDefaults()
+
+ if err := cfg.Unmarshal(c); err != nil {
return err
}
- c.Workers.SetDefaults()
c.Workers.UpscaleDurations()
+ if err := c.Valid(); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/service/http/config_test.go b/service/http/config_test.go
index 2e3fe731..823efb32 100644
--- a/service/http/config_test.go
+++ b/service/http/config_test.go
@@ -31,7 +31,6 @@ func Test_Config_Hydrate_Error2(t *testing.T) {
func Test_Config_Valid(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: ":8080",
MaxRequest: 1024,
Uploads: &UploadsConfig{
@@ -39,7 +38,7 @@ func Test_Config_Valid(t *testing.T) {
Forbid: []string{".go"},
},
Workers: &roadrunner.ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -54,11 +53,10 @@ func Test_Config_Valid(t *testing.T) {
func Test_Config_NoUploads(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: ":8080",
MaxRequest: 1024,
Workers: &roadrunner.ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -73,7 +71,6 @@ func Test_Config_NoUploads(t *testing.T) {
func Test_Config_NoWorkers(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: ":8080",
MaxRequest: 1024,
Uploads: &UploadsConfig{
@@ -87,7 +84,6 @@ func Test_Config_NoWorkers(t *testing.T) {
func Test_Config_NoPool(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: ":8080",
MaxRequest: 1024,
Uploads: &UploadsConfig{
@@ -95,7 +91,7 @@ func Test_Config_NoPool(t *testing.T) {
Forbid: []string{".go"},
},
Workers: &roadrunner.ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 0,
@@ -110,7 +106,6 @@ func Test_Config_NoPool(t *testing.T) {
func Test_Config_DeadPool(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: ":8080",
MaxRequest: 1024,
Uploads: &UploadsConfig{
@@ -118,7 +113,7 @@ func Test_Config_DeadPool(t *testing.T) {
Forbid: []string{".go"},
},
Workers: &roadrunner.ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
},
}
@@ -128,7 +123,6 @@ func Test_Config_DeadPool(t *testing.T) {
func Test_Config_InvalidAddress(t *testing.T) {
cfg := &Config{
- Enable: true,
Address: "",
MaxRequest: 1024,
Uploads: &UploadsConfig{
@@ -136,7 +130,7 @@ func Test_Config_InvalidAddress(t *testing.T) {
Forbid: []string{".go"},
},
Workers: &roadrunner.ServerConfig{
- Command: "php php-src/tests/client.php echo pipes",
+ Command: "php tests/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
diff --git a/service/http/handler.go b/service/http/handler.go
index 9e67e5b4..945cd51e 100644
--- a/service/http/handler.go
+++ b/service/http/handler.go
@@ -51,7 +51,7 @@ func (h *Handler) Listen(l func(event int, ctx interface{})) {
h.lsn = l
}
-// middleware serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled.
+// mdwr serve using PSR-7 requests passed to underlying application. Attempts to serve static files first if enabled.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// validating request size
if h.cfg.MaxRequest != 0 {
diff --git a/service/http/handler_test.go b/service/http/handler_test.go
index 3b51c873..e864b86e 100644
--- a/service/http/handler_test.go
+++ b/service/http/handler_test.go
@@ -39,7 +39,7 @@ func TestServer_Echo(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Command: "php ../../tests/http/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -74,7 +74,7 @@ func Test_HandlerErrors(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Command: "php ../../tests/http/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -101,7 +101,7 @@ func Test_Handler_JSON_error(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Command: "php ../../tests/http/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -130,7 +130,7 @@ func TestServer_Headers(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php header pipes",
+ Command: "php ../../tests/http/client.php header pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -177,7 +177,7 @@ func TestServer_Cookies(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php cookie pipes",
+ Command: "php ../../tests/http/client.php cookie pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -228,7 +228,7 @@ func TestServer_JsonPayload_POST(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php payload pipes",
+ Command: "php ../../tests/http/client.php payload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -278,7 +278,7 @@ func TestServer_JsonPayload_PUT(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php payload pipes",
+ Command: "php ../../tests/http/client.php payload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -324,7 +324,7 @@ func TestServer_JsonPayload_PATCH(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php payload pipes",
+ Command: "php ../../tests/http/client.php payload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -370,7 +370,7 @@ func TestServer_FormData_POST(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -428,7 +428,7 @@ func TestServer_FormData_PUT(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -486,7 +486,7 @@ func TestServer_FormData_PATCH(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -544,7 +544,7 @@ func TestServer_Multipart_POST(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -606,7 +606,7 @@ func TestServer_Multipart_PUT(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -668,7 +668,7 @@ func TestServer_Multipart_PATCH(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php data pipes",
+ Command: "php ../../tests/http/client.php data pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -730,7 +730,7 @@ func TestServer_Error(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php error pipes",
+ Command: "php ../../tests/http/client.php error pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -764,7 +764,7 @@ func TestServer_Error2(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php error2 pipes",
+ Command: "php ../../tests/http/client.php error2 pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -798,7 +798,7 @@ func TestServer_Error3(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php pid pipes",
+ Command: "php ../../tests/http/client.php pid pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -843,7 +843,7 @@ func BenchmarkHandler_Listen_Echo(b *testing.B) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php echo pipes",
+ Command: "php ../../tests/http/client.php echo pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: int64(runtime.NumCPU()),
diff --git a/service/http/request.go b/service/http/request.go
index 531a1efd..d733b20c 100644
--- a/service/http/request.go
+++ b/service/http/request.go
@@ -49,7 +49,7 @@ type Request struct {
// Uploads contains list of uploaded files, their names, sized and associations with temporary files.
Uploads *Uploads `json:"uploads"`
- // Attributes can be set by chained middleware to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions.
+ // Attributes can be set by chained mdwr to safely pass value from Golang to PHP. See: GetAttribute, SetAttribute functions.
Attributes map[string]interface{} `json:"attributes"`
// request body can be parsedData or []byte
diff --git a/service/http/rpc_test.go b/service/http/rpc_test.go
index 32bb776c..ba3efd2e 100644
--- a/service/http/rpc_test.go
+++ b/service/http/rpc_test.go
@@ -33,7 +33,7 @@ func Test_RPC(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "command": "php ../../tests/http/client.php pid pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -94,7 +94,7 @@ func Test_RPC_Unix(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "command": "php ../../tests/http/client.php pid pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -148,7 +148,7 @@ func Test_Workers(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "command": "php ../../tests/http/client.php pid pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
diff --git a/service/http/service.go b/service/http/service.go
index ecce1c15..bb75a2c0 100644
--- a/service/http/service.go
+++ b/service/http/service.go
@@ -27,7 +27,7 @@ type Service struct {
cfg *Config
env env.Environment
lsns []func(event int, ctx interface{})
- mdws []middleware
+ mdwr []middleware
mu sync.Mutex
rr *roadrunner.Server
stopping int32
@@ -35,9 +35,9 @@ type Service struct {
http *http.Server
}
-// AddMiddleware adds new net/http middleware.
+// AddMiddleware adds new net/http mdwr.
func (s *Service) AddMiddleware(m middleware) {
- s.mdws = append(s.mdws, m)
+ s.mdwr = append(s.mdwr, m)
}
// AddListener attaches server event watcher.
@@ -48,10 +48,6 @@ func (s *Service) AddListener(l func(event int, ctx interface{})) {
// Init must return configure svc and return true if svc hasStatus enabled. Must return error in case of
// misconfiguration. Services must not be used without proper configuration pushed first.
func (s *Service) Init(cfg *Config, r *rpc.Service, e env.Environment) (bool, error) {
- if !cfg.Enable {
- return false, nil
- }
-
s.cfg = cfg
s.env = e
if r != nil {
@@ -87,17 +83,14 @@ func (s *Service) Serve() error {
s.rr.Listen(s.listener)
s.srv.Listen(s.listener)
- if len(s.mdws) == 0 {
- s.http.Handler = s.srv
- } else {
- s.http.Handler = s
- }
+ s.http.Handler = s
+
s.mu.Unlock()
if err := rr.Start(); err != nil {
return err
}
- defer s.rr.Stop()
+ defer rr.Stop()
return s.http.ListenAndServe()
}
@@ -118,13 +111,13 @@ func (s *Service) Stop() {
s.http.Shutdown(context.Background())
}
-// middleware handles connection using set of mdws and rr PSR-7 server.
+// mdwr handles connection using set of mdwr and rr PSR-7 server.
func (s *Service) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r = attributes.Init(r)
- // chaining middlewares
+ // chaining mdwr
f := s.srv.ServeHTTP
- for _, m := range s.mdws {
+ for _, m := range s.mdwr {
f = m(f)
}
f(w, r)
diff --git a/service/http/service_test.go b/service/http/service_test.go
index 8dab7cd4..d1d601dc 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -25,6 +25,10 @@ type testCfg struct {
func (cfg *testCfg) Get(name string) service.Config {
if name == ID {
+ if cfg.httpCfg == "" {
+ return nil
+ }
+
return &testCfg{target: cfg.httpCfg}
}
@@ -63,24 +67,7 @@ func Test_Service_Configure_Disable(t *testing.T) {
c := service.NewContainer(logger)
c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
- "enable": false,
- "address": ":8070",
- "maxRequest": 1024,
- "uploads": {
- "dir": ` + tmpDir() + `,
- "forbid": []
- },
- "workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
- "relay": "pipes",
- "pool": {
- "numWorkers": 1,
- "allocateTimeout": 10000000,
- "destroyTimeout": 10000000
- }
- }
- }`}))
+ assert.NoError(t, c.Init(&testCfg{}))
s, st := c.Get(ID)
assert.NotNil(t, s)
@@ -103,7 +90,7 @@ func Test_Service_Configure_Enable(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -134,7 +121,7 @@ func Test_Service_Echo(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -187,7 +174,7 @@ func Test_Service_Env(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php env pipes",
+ "command": "php ../../tests/http/client.php env pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -239,7 +226,7 @@ func Test_Service_ErrorEcho(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echoerr pipes",
+ "command": "php ../../tests/http/client.php echoerr pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -299,7 +286,7 @@ func Test_Service_Middleware(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -373,7 +360,7 @@ func Test_Service_Listener(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -417,7 +404,7 @@ func Test_Service_Error(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "---",
"pool": {
"numWorkers": 1,
@@ -446,7 +433,7 @@ func Test_Service_Error2(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php broken pipes",
+ "command": "php ../../tests/http/client.php broken pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -475,7 +462,7 @@ func Test_Service_Error3(t *testing.T) {
"forbid": []
},
"workers"
- "command": "php ../../php-src/tests/http/client.php broken pipes",
+ "command": "php ../../tests/http/client.php broken pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -502,7 +489,7 @@ func Test_Service_Error4(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php broken pipes",
+ "command": "php ../../tests/http/client.php broken pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
diff --git a/service/http/uploads_config.go b/service/http/uploads_config.go
index e90d9b70..3f655064 100644
--- a/service/http/uploads_config.go
+++ b/service/http/uploads_config.go
@@ -16,6 +16,12 @@ type UploadsConfig struct {
Forbid []string
}
+// InitDefaults sets missing values to their default values.
+func (cfg *UploadsConfig) InitDefaults() error {
+ cfg.Forbid = []string{".php", ".exe", ".bat"}
+ return nil
+}
+
// TmpDir returns temporary directory.
func (cfg *UploadsConfig) TmpDir() string {
if cfg.Dir != "" {
diff --git a/service/http/uploads_test.go b/service/http/uploads_test.go
index b2662bf7..96e95733 100644
--- a/service/http/uploads_test.go
+++ b/service/http/uploads_test.go
@@ -27,7 +27,7 @@ func TestServer_Upload_File(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php upload pipes",
+ Command: "php ../../tests/http/client.php upload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -88,7 +88,7 @@ func TestServer_Upload_NestedFile(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php upload pipes",
+ Command: "php ../../tests/http/client.php upload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -149,7 +149,7 @@ func TestServer_Upload_File_NoTmpDir(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php upload pipes",
+ Command: "php ../../tests/http/client.php upload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
@@ -210,7 +210,7 @@ func TestServer_Upload_File_Forbids(t *testing.T) {
},
},
rr: roadrunner.NewServer(&roadrunner.ServerConfig{
- Command: "php ../../php-src/tests/http/client.php upload pipes",
+ Command: "php ../../tests/http/client.php upload pipes",
Relay: "pipes",
Pool: &roadrunner.Config{
NumWorkers: 1,
diff --git a/service/rpc/config.go b/service/rpc/config.go
index 93fce7ca..653da6ea 100644
--- a/service/rpc/config.go
+++ b/service/rpc/config.go
@@ -26,6 +26,14 @@ func (c *Config) Hydrate(cfg service.Config) error {
return c.Valid()
}
+// InitDefaults allows to init blank config with pre-defined set of default values.
+func (c *Config) InitDefaults() error {
+ c.Enable = true
+ c.Listen = "tcp://127.0.0.1:6001"
+
+ return nil
+}
+
// Valid returns nil if config is valid.
func (c *Config) Valid() error {
if dsn := strings.Split(c.Listen, "://"); len(dsn) != 2 {
diff --git a/service/rpc/config_test.go b/service/rpc/config_test.go
index a7c51c0f..b335be19 100644
--- a/service/rpc/config_test.go
+++ b/service/rpc/config_test.go
@@ -27,6 +27,13 @@ func Test_Config_Hydrate_Error(t *testing.T) {
assert.Error(t, c.Hydrate(cfg))
}
+func Test_Config_Hydrate_Error2(t *testing.T) {
+ cfg := &testCfg{`{"enable": true, "listen": "invalid"`}
+ c := &Config{}
+
+ assert.Error(t, c.Hydrate(cfg))
+}
+
func TestConfig_Listener(t *testing.T) {
cfg := &Config{Listen: "tcp://:18001"}
@@ -128,3 +135,10 @@ func Test_Config_DialerErrorMethod(t *testing.T) {
assert.Nil(t, ln)
assert.Error(t, err)
}
+
+func Test_Config_Defaults(t *testing.T) {
+ c := &Config{}
+ c.InitDefaults()
+ assert.Equal(t, true, c.Enable)
+ assert.Equal(t, "tcp://127.0.0.1:6001", c.Listen)
+}
diff --git a/service/rpc/service_test.go b/service/rpc/service_test.go
index 59e0e05d..467cbe3f 100644
--- a/service/rpc/service_test.go
+++ b/service/rpc/service_test.go
@@ -1,6 +1,7 @@
package rpc
import (
+ "github.com/spiral/roadrunner/service/env"
"github.com/stretchr/testify/assert"
"testing"
"time"
@@ -80,3 +81,15 @@ func Test_Serve_Client(t *testing.T) {
assert.NoError(t, client.Call("test.Echo", "hello world", &resp))
assert.Equal(t, "hello world", resp)
}
+
+func TestSetEnv(t *testing.T) {
+ s := &Service{}
+ e := env.NewService(map[string]string{})
+ ok, err := s.Init(&Config{Enable: true, Listen: "tcp://localhost:9018"}, e)
+
+ assert.NoError(t, err)
+ assert.True(t, ok)
+
+ v, _ := e.GetEnv()
+ assert.Equal(t, "tcp://localhost:9018", v["rr_rpc"])
+}
diff --git a/service/static/service_test.go b/service/static/service_test.go
index 842e5e0b..7b40b8ad 100644
--- a/service/static/service_test.go
+++ b/service/static/service_test.go
@@ -56,7 +56,7 @@ func Test_Files(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[]}`,
+ static: `{"enable":true, "dir":"../../tests", "forbid":[]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -66,7 +66,7 @@ func Test_Files(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "command": "php ../../tests/http/client.php pid pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -93,7 +93,7 @@ func Test_Files_Disable(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":false, "dir":"../../php-src/tests", "forbid":[".php"]}`,
+ static: `{"enable":false, "dir":"../../tests", "forbid":[".php"]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -103,7 +103,7 @@ func Test_Files_Disable(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -140,7 +140,7 @@ func Test_Files_Error(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -170,7 +170,7 @@ func Test_Files_Error2(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -190,7 +190,7 @@ func Test_Files_Forbid(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`,
+ static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -200,7 +200,7 @@ func Test_Files_Forbid(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -227,7 +227,7 @@ func Test_Files_NotFound(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`,
+ static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -237,7 +237,7 @@ func Test_Files_NotFound(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -264,7 +264,7 @@ func Test_Files_Dir(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[".php"]}`,
+ static: `{"enable":true, "dir":"../../tests", "forbid":[".php"]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -274,7 +274,7 @@ func Test_Files_Dir(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php echo pipes",
+ "command": "php ../../tests/http/client.php echo pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -301,7 +301,7 @@ func Test_Files_NotForbid(t *testing.T) {
c.Register(ID, &Service{})
assert.NoError(t, c.Init(&testCfg{
- static: `{"enable":true, "dir":"../../php-src/tests", "forbid":[]}`,
+ static: `{"enable":true, "dir":"../../tests", "forbid":[]}`,
httpCfg: `{
"enable": true,
"address": ":6029",
@@ -311,7 +311,7 @@ func Test_Files_NotForbid(t *testing.T) {
"forbid": []
},
"workers":{
- "command": "php ../../php-src/tests/http/client.php pid pipes",
+ "command": "php ../../tests/http/client.php pid pipes",
"relay": "pipes",
"pool": {
"numWorkers": 1,
@@ -326,7 +326,7 @@ func Test_Files_NotForbid(t *testing.T) {
defer c.Stop()
b, _, _ := get("http://localhost:6029/client.php")
- assert.Equal(t, all("../../php-src/tests/client.php"), b)
+ assert.Equal(t, all("../../tests/client.php"), b)
}
func tmpDir() string {
diff --git a/socket_factory_test.go b/socket_factory_test.go
index 214a4851..00d0e93c 100644
--- a/socket_factory_test.go
+++ b/socket_factory_test.go
@@ -19,7 +19,7 @@ func Test_Tcp_Start(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "echo", "tcp")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.NoError(t, err)
@@ -41,7 +41,7 @@ func Test_Tcp_StartCloseFactory(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "echo", "tcp")
f := NewSocketFactory(ls, time.Minute)
defer f.Close()
@@ -67,7 +67,7 @@ func Test_Tcp_StartError(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
cmd.Start()
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
@@ -85,7 +85,7 @@ func Test_Tcp_Failboot(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/failboot.php")
+ cmd := exec.Command("php", "tests/failboot.php")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.Nil(t, w)
@@ -103,7 +103,7 @@ func Test_Tcp_Timeout(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/slow-client.php", "echo", "tcp", "200", "0")
+ cmd := exec.Command("php", "tests/slow-client.php", "echo", "tcp", "200", "0")
w, err := NewSocketFactory(ls, time.Millisecond*100).SpawnWorker(cmd)
assert.Nil(t, w)
@@ -121,7 +121,7 @@ func Test_Tcp_Invalid(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/invalid.php")
+ cmd := exec.Command("php", "tests/invalid.php")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.Error(t, err)
@@ -138,7 +138,7 @@ func Test_Tcp_Broken(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "broken", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "broken", "tcp")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
@@ -165,7 +165,7 @@ func Test_Tcp_Echo(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "echo", "tcp")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
@@ -195,7 +195,7 @@ func Test_Unix_Start(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "unix")
+ cmd := exec.Command("php", "tests/client.php", "echo", "unix")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.NoError(t, err)
@@ -220,7 +220,7 @@ func Test_Unix_Failboot(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/failboot.php")
+ cmd := exec.Command("php", "tests/failboot.php")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.Nil(t, w)
@@ -240,7 +240,7 @@ func Test_Unix_Timeout(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/slow-client.php", "echo", "unix", "200", "0")
+ cmd := exec.Command("php", "tests/slow-client.php", "echo", "unix", "200", "0")
w, err := NewSocketFactory(ls, time.Millisecond*100).SpawnWorker(cmd)
assert.Nil(t, w)
@@ -260,7 +260,7 @@ func Test_Unix_Invalid(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/invalid.php")
+ cmd := exec.Command("php", "tests/invalid.php")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
assert.Error(t, err)
@@ -279,7 +279,7 @@ func Test_Unix_Broken(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "broken", "unix")
+ cmd := exec.Command("php", "tests/client.php", "broken", "unix")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
@@ -308,7 +308,7 @@ func Test_Unix_Echo(t *testing.T) {
t.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "unix")
+ cmd := exec.Command("php", "tests/client.php", "echo", "unix")
w, err := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
@@ -336,7 +336,7 @@ func Benchmark_Tcp_SpawnWorker_Stop(b *testing.B) {
f := NewSocketFactory(ls, time.Minute)
for n := 0; n < b.N; n++ {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "echo", "tcp")
w, _ := f.SpawnWorker(cmd)
go func() {
@@ -357,7 +357,7 @@ func Benchmark_Tcp_Worker_ExecEcho(b *testing.B) {
b.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "tcp")
+ cmd := exec.Command("php", "tests/client.php", "echo", "tcp")
w, _ := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
@@ -386,7 +386,7 @@ func Benchmark_Unix_SpawnWorker_Stop(b *testing.B) {
f := NewSocketFactory(ls, time.Minute)
for n := 0; n < b.N; n++ {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "unix")
+ cmd := exec.Command("php", "tests/client.php", "echo", "unix")
w, _ := f.SpawnWorker(cmd)
go func() {
@@ -411,7 +411,7 @@ func Benchmark_Unix_Worker_ExecEcho(b *testing.B) {
b.Skip("socket is busy")
}
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "unix")
+ cmd := exec.Command("php", "tests/client.php", "echo", "unix")
w, _ := NewSocketFactory(ls, time.Minute).SpawnWorker(cmd)
go func() {
diff --git a/src/Exception/RoadRunnerException.php b/src/Exception/RoadRunnerException.php
new file mode 100644
index 00000000..ee99bb2b
--- /dev/null
+++ b/src/Exception/RoadRunnerException.php
@@ -0,0 +1,13 @@
+<?php
+/**
+ * High-performance PHP process supervisor and load balancer written in Go
+ *
+ * @author Wolfy-J
+ */
+
+namespace Spiral\RoadRunner\Exception;
+
+class RoadRunnerException extends \Spiral\RoadRunner\Exceptions\RoadRunnerException
+{
+
+} \ No newline at end of file
diff --git a/src/Exceptions/RoadRunnerException.php b/src/Exceptions/RoadRunnerException.php
new file mode 100644
index 00000000..1a5da18c
--- /dev/null
+++ b/src/Exceptions/RoadRunnerException.php
@@ -0,0 +1,17 @@
+<?php
+/**
+ * Spiral Framework.
+ *
+ * @license MIT
+ * @author Anton Titov (Wolfy-J)
+ */
+
+namespace Spiral\RoadRunner\Exceptions;
+
+/**
+ * @deprecated use \Spiral\RoadRunner\Exception\RoadRunnerException instead
+ */
+class RoadRunnerException extends \RuntimeException
+{
+
+} \ No newline at end of file
diff --git a/php-src/PSR7Client.php b/src/PSR7Client.php
index e8d93fe8..e8d93fe8 100644
--- a/php-src/PSR7Client.php
+++ b/src/PSR7Client.php
diff --git a/php-src/Worker.php b/src/Worker.php
index 3e013090..4405cd70 100644
--- a/php-src/Worker.php
+++ b/src/Worker.php
@@ -9,7 +9,7 @@ namespace Spiral\RoadRunner;
use Spiral\Goridge\Exceptions\GoridgeException;
use Spiral\Goridge\RelayInterface as Relay;
-use Spiral\RoadRunner\Exceptions\RoadRunnerException;
+use Spiral\RoadRunner\Exception\RoadRunnerException;
/**
* Accepts connection from RoadRunner server over given Goridge relay.
diff --git a/static_pool.go b/static_pool.go
index 95d2fe14..be1a6f3b 100644
--- a/static_pool.go
+++ b/static_pool.go
@@ -26,6 +26,7 @@ type StaticPool struct {
factory Factory
// active task executions
+ tmu sync.Mutex
tasks sync.WaitGroup
// workers circular allocation buf
@@ -42,6 +43,7 @@ type StaticPool struct {
// pool is being destroyed
inDestroy int32
+ destroy chan interface{}
// lsn is optional callback to handle worker create/destruct/error events.
mul sync.Mutex
@@ -60,6 +62,7 @@ func NewPool(cmd func() *exec.Cmd, factory Factory, cfg Config) (*StaticPool, er
factory: factory,
workers: make([]*Worker, 0, cfg.NumWorkers),
free: make(chan *Worker, cfg.NumWorkers),
+ destroy: make(chan interface{}),
}
// constant number of workers simplify logic
@@ -110,7 +113,10 @@ func (p *StaticPool) Workers() (workers []*Worker) {
// Exec one task with given payload and context, returns result or error.
func (p *StaticPool) Exec(rqs *Payload) (rsp *Payload, err error) {
+ p.tmu.Lock()
p.tasks.Add(1)
+ p.tmu.Unlock()
+
defer p.tasks.Done()
w, err := p.allocateWorker()
@@ -145,7 +151,10 @@ func (p *StaticPool) Exec(rqs *Payload) (rsp *Payload, err error) {
func (p *StaticPool) Destroy() {
atomic.AddInt32(&p.inDestroy, 1)
+ p.tmu.Lock()
+ close(p.destroy)
p.tasks.Wait()
+ p.tmu.Unlock()
var wg sync.WaitGroup
for _, w := range p.Workers() {
@@ -173,6 +182,8 @@ func (p *StaticPool) allocateWorker() (w *Worker, err error) {
}
return w, nil
+ case <-p.destroy:
+ return nil, fmt.Errorf("pool has been stopped")
default:
// enable timeout handler
}
@@ -189,6 +200,8 @@ func (p *StaticPool) allocateWorker() (w *Worker, err error) {
continue
}
return w, nil
+ case <-p.destroy:
+ return nil, fmt.Errorf("pool has been stopped")
}
}
diff --git a/static_pool_test.go b/static_pool_test.go
index 13c96d09..1e4906a5 100644
--- a/static_pool_test.go
+++ b/static_pool_test.go
@@ -19,7 +19,7 @@ var cfg = Config{
func Test_NewPool(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -33,7 +33,7 @@ func Test_NewPool(t *testing.T) {
func Test_StaticPool_Invalid(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/invalid.php") },
+ func() *exec.Cmd { return exec.Command("php", "tests/invalid.php") },
NewPipeFactory(),
cfg,
)
@@ -44,7 +44,7 @@ func Test_StaticPool_Invalid(t *testing.T) {
func Test_ConfigError(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
Config{
AllocateTimeout: time.Second,
@@ -58,7 +58,7 @@ func Test_ConfigError(t *testing.T) {
func Test_StaticPool_Echo(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -79,7 +79,7 @@ func Test_StaticPool_Echo(t *testing.T) {
func Test_StaticPool_Echo_NilContext(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -100,7 +100,7 @@ func Test_StaticPool_Echo_NilContext(t *testing.T) {
func Test_StaticPool_Echo_Context(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "head", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "head", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -121,7 +121,7 @@ func Test_StaticPool_Echo_Context(t *testing.T) {
func Test_StaticPool_JobError(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "error", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "error", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -141,7 +141,7 @@ func Test_StaticPool_JobError(t *testing.T) {
func Test_StaticPool_Broken_Replace(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "broken", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "broken", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -164,7 +164,7 @@ func Test_StaticPool_Broken_Replace(t *testing.T) {
func Test_StaticPool_Broken_FromOutside(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -203,7 +203,7 @@ func Test_StaticPool_Broken_FromOutside(t *testing.T) {
func Test_StaticPool_AllocateTimeout(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "delay", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "delay", "pipes") },
NewPipeFactory(),
Config{
NumWorkers: 1,
@@ -235,7 +235,7 @@ func Test_StaticPool_AllocateTimeout(t *testing.T) {
func Test_StaticPool_Replace_Worker(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "pid", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "pid", "pipes") },
NewPipeFactory(),
Config{
NumWorkers: 1,
@@ -271,7 +271,7 @@ func Test_StaticPool_Replace_Worker(t *testing.T) {
// identical to replace but controlled on worker side
func Test_StaticPool_Stop_Worker(t *testing.T) {
p, err := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "stop", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "stop", "pipes") },
NewPipeFactory(),
Config{
NumWorkers: 1,
@@ -303,9 +303,93 @@ func Test_StaticPool_Stop_Worker(t *testing.T) {
}
}
+// identical to replace but controlled on worker side
+func Test_Static_Pool_Destroy_And_Close(t *testing.T) {
+ p, err := NewPool(
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "delay", "pipes") },
+ NewPipeFactory(),
+ Config{
+ NumWorkers: 1,
+ AllocateTimeout: time.Second,
+ DestroyTimeout: time.Second,
+ },
+ )
+
+ assert.NotNil(t, p)
+ assert.NoError(t, err)
+
+ p.Destroy()
+ _, err = p.Exec(&Payload{Body: []byte("100")})
+ assert.Error(t, err)
+}
+
+// identical to replace but controlled on worker side
+func Test_Static_Pool_Destroy_And_Close_While_Wait(t *testing.T) {
+ p, err := NewPool(
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "delay", "pipes") },
+ NewPipeFactory(),
+ Config{
+ NumWorkers: 1,
+ AllocateTimeout: time.Second,
+ DestroyTimeout: time.Second,
+ },
+ )
+
+ assert.NotNil(t, p)
+ assert.NoError(t, err)
+
+ go p.Exec(&Payload{Body: []byte("100")})
+ time.Sleep(time.Millisecond * 10)
+
+ p.Destroy()
+ _, err = p.Exec(&Payload{Body: []byte("100")})
+ assert.Error(t, err)
+}
+
+// identical to replace but controlled on worker side
+func Test_Static_Pool_Handle_Dead(t *testing.T) {
+ p, err := NewPool(
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
+ NewPipeFactory(),
+ Config{
+ NumWorkers: 5,
+ AllocateTimeout: time.Second,
+ DestroyTimeout: time.Second,
+ },
+ )
+ defer p.Destroy()
+
+ assert.NotNil(t, p)
+ assert.NoError(t, err)
+
+ for _, w := range p.workers {
+ w.state.value = StateErrored
+ }
+
+ _, err = p.Exec(&Payload{Body: []byte("hello")})
+ assert.Error(t, err)
+}
+
+// identical to replace but controlled on worker side
+func Test_Static_Pool_Slow_Destroy(t *testing.T) {
+ p, err := NewPool(
+ func() *exec.Cmd { return exec.Command("php", "tests/slow-destroy.php", "echo", "pipes") },
+ NewPipeFactory(),
+ Config{
+ NumWorkers: 5,
+ AllocateTimeout: time.Second,
+ DestroyTimeout: time.Second,
+ },
+ )
+ p.Destroy()
+
+ assert.NotNil(t, p)
+ assert.NoError(t, err)
+}
+
func Benchmark_Pool_Allocate(b *testing.B) {
p, _ := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -324,7 +408,7 @@ func Benchmark_Pool_Allocate(b *testing.B) {
func Benchmark_Pool_Echo(b *testing.B) {
p, _ := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -339,7 +423,7 @@ func Benchmark_Pool_Echo(b *testing.B) {
func Benchmark_Pool_Echo_Batched(b *testing.B) {
p, _ := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
cfg,
)
@@ -362,7 +446,7 @@ func Benchmark_Pool_Echo_Batched(b *testing.B) {
func Benchmark_Pool_Echo_Replaced(b *testing.B) {
p, _ := NewPool(
- func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") },
+ func() *exec.Cmd { return exec.Command("php", "tests/client.php", "echo", "pipes") },
NewPipeFactory(),
Config{
NumWorkers: 1,
diff --git a/php-src/tests/broken.php b/tests/broken.php
index b1a3839e..b1a3839e 100644
--- a/php-src/tests/broken.php
+++ b/tests/broken.php
diff --git a/php-src/tests/client.php b/tests/client.php
index fd5d60be..31caa410 100644
--- a/php-src/tests/client.php
+++ b/tests/client.php
@@ -3,7 +3,7 @@
use Spiral\Goridge;
ini_set('display_errors', 'stderr');
-require dirname(__DIR__) . "/../vendor/autoload.php";
+require dirname(__DIR__) . "/vendor/autoload.php";
if (count($argv) < 3) {
die("need 2 arguments");
diff --git a/php-src/tests/delay.php b/tests/delay.php
index bfde2fc4..bfde2fc4 100644
--- a/php-src/tests/delay.php
+++ b/tests/delay.php
diff --git a/php-src/tests/echo.php b/tests/echo.php
index ba58ff30..ba58ff30 100644
--- a/php-src/tests/echo.php
+++ b/tests/echo.php
diff --git a/php-src/tests/error.php b/tests/error.php
index ebd3418b..ebd3418b 100644
--- a/php-src/tests/error.php
+++ b/tests/error.php
diff --git a/php-src/tests/failboot.php b/tests/failboot.php
index fa8b96f6..fa8b96f6 100644
--- a/php-src/tests/failboot.php
+++ b/tests/failboot.php
diff --git a/php-src/tests/head.php b/tests/head.php
index 4f4e4061..4f4e4061 100644
--- a/php-src/tests/head.php
+++ b/tests/head.php
diff --git a/php-src/tests/http/client.php b/tests/http/client.php
index 3b6b5dc6..166acfb6 100644
--- a/php-src/tests/http/client.php
+++ b/tests/http/client.php
@@ -4,7 +4,7 @@ use Spiral\Goridge;
use Spiral\RoadRunner;
ini_set('display_errors', 'stderr');
-require dirname(__DIR__) . "/../../vendor/autoload.php";
+require dirname(__DIR__) . "/../vendor/autoload.php";
if (count($argv) < 3) {
die("need 2 arguments");
diff --git a/php-src/tests/http/cookie.php b/tests/http/cookie.php
index 196ceee2..196ceee2 100644
--- a/php-src/tests/http/cookie.php
+++ b/tests/http/cookie.php
diff --git a/php-src/tests/http/data.php b/tests/http/data.php
index c5e0bab0..c5e0bab0 100644
--- a/php-src/tests/http/data.php
+++ b/tests/http/data.php
diff --git a/php-src/tests/http/echo.php b/tests/http/echo.php
index 7004ada0..7004ada0 100644
--- a/php-src/tests/http/echo.php
+++ b/tests/http/echo.php
diff --git a/php-src/tests/http/echoerr.php b/tests/http/echoerr.php
index da2ff4d8..da2ff4d8 100644
--- a/php-src/tests/http/echoerr.php
+++ b/tests/http/echoerr.php
diff --git a/php-src/tests/http/env.php b/tests/http/env.php
index 1e29926f..1e29926f 100644
--- a/php-src/tests/http/env.php
+++ b/tests/http/env.php
diff --git a/php-src/tests/http/error.php b/tests/http/error.php
index 6df0d4b5..6df0d4b5 100644
--- a/php-src/tests/http/error.php
+++ b/tests/http/error.php
diff --git a/php-src/tests/http/error2.php b/tests/http/error2.php
index 617b5a3f..617b5a3f 100644
--- a/php-src/tests/http/error2.php
+++ b/tests/http/error2.php
diff --git a/php-src/tests/http/header.php b/tests/http/header.php
index e5b295b6..e5b295b6 100644
--- a/php-src/tests/http/header.php
+++ b/tests/http/header.php
diff --git a/php-src/tests/http/payload.php b/tests/http/payload.php
index a16984c5..a16984c5 100644
--- a/php-src/tests/http/payload.php
+++ b/tests/http/payload.php
diff --git a/php-src/tests/http/pid.php b/tests/http/pid.php
index 1cc322bf..1cc322bf 100644
--- a/php-src/tests/http/pid.php
+++ b/tests/http/pid.php
diff --git a/php-src/tests/http/upload.php b/tests/http/upload.php
index 2f7c0b64..2f7c0b64 100644
--- a/php-src/tests/http/upload.php
+++ b/tests/http/upload.php
diff --git a/php-src/tests/pid.php b/tests/pid.php
index a8cfa229..a8cfa229 100644
--- a/php-src/tests/pid.php
+++ b/tests/pid.php
diff --git a/php-src/tests/sample.txt b/tests/sample.txt
index eed7e79a..eed7e79a 100644
--- a/php-src/tests/sample.txt
+++ b/tests/sample.txt
diff --git a/php-src/tests/slow-client.php b/tests/slow-client.php
index 2722868c..f09142b5 100644
--- a/php-src/tests/slow-client.php
+++ b/tests/slow-client.php
@@ -3,7 +3,7 @@
use Spiral\Goridge;
ini_set('display_errors', 'stderr');
-require dirname(__DIR__) . "/../vendor/autoload.php";
+require dirname(__DIR__) . "/vendor/autoload.php";
if (count($argv) < 3) {
die("need 2 arguments");
diff --git a/tests/slow-destroy.php b/tests/slow-destroy.php
new file mode 100644
index 00000000..0f0abc1c
--- /dev/null
+++ b/tests/slow-destroy.php
@@ -0,0 +1,37 @@
+<?php
+
+use Spiral\Goridge;
+
+ini_set('display_errors', 'stderr');
+require dirname(__DIR__) . "/vendor/autoload.php";
+
+if (count($argv) < 3) {
+ die("need 2 arguments");
+}
+
+list($test, $goridge) = [$argv[1], $argv[2]];
+
+switch ($goridge) {
+ case "pipes":
+ $relay = new Goridge\StreamRelay(STDIN, STDOUT);
+ break;
+
+ case "tcp":
+ $relay = new Goridge\SocketRelay("localhost", 9007);
+ break;
+
+ case "unix":
+ $relay = new Goridge\SocketRelay(
+ "sock.unix",
+ null,
+ Goridge\SocketRelay::SOCK_UNIX
+ );
+ break;
+
+ default:
+ die("invalid protocol selection");
+}
+
+require_once sprintf("%s/%s.php", __DIR__, $test);
+
+sleep(10); \ No newline at end of file
diff --git a/php-src/tests/stop.php b/tests/stop.php
index caa485d6..caa485d6 100644
--- a/php-src/tests/stop.php
+++ b/tests/stop.php
diff --git a/worker_test.go b/worker_test.go
index 053a97b1..b3a3fc46 100644
--- a/worker_test.go
+++ b/worker_test.go
@@ -7,7 +7,7 @@ import (
)
func Test_GetState(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, err := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -23,7 +23,7 @@ func Test_GetState(t *testing.T) {
}
func Test_Kill(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, err := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -39,7 +39,7 @@ func Test_Kill(t *testing.T) {
}
func Test_Echo(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -58,7 +58,7 @@ func Test_Echo(t *testing.T) {
}
func Test_BadPayload(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -75,16 +75,16 @@ func Test_BadPayload(t *testing.T) {
}
func Test_NotStarted_String(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := newWorker(cmd)
- assert.Contains(t, w.String(), "php php-src/tests/client.php echo pipes")
+ assert.Contains(t, w.String(), "php tests/client.php echo pipes")
assert.Contains(t, w.String(), "inactive")
assert.Contains(t, w.String(), "numExecs: 0")
}
func Test_NotStarted_Exec(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := newWorker(cmd)
@@ -97,7 +97,7 @@ func Test_NotStarted_Exec(t *testing.T) {
}
func Test_String(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -105,13 +105,13 @@ func Test_String(t *testing.T) {
}()
defer w.Stop()
- assert.Contains(t, w.String(), "php php-src/tests/client.php echo pipes")
+ assert.Contains(t, w.String(), "php tests/client.php echo pipes")
assert.Contains(t, w.String(), "ready")
assert.Contains(t, w.String(), "numExecs: 0")
}
func Test_Echo_Slow(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/slow-client.php", "echo", "pipes", "10", "10")
+ cmd := exec.Command("php", "tests/slow-client.php", "echo", "pipes", "10", "10")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -130,7 +130,7 @@ func Test_Echo_Slow(t *testing.T) {
}
func Test_Broken(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "broken", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "broken", "pipes")
w, err := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -146,7 +146,7 @@ func Test_Broken(t *testing.T) {
}
func Test_OnStarted(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "broken", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "broken", "pipes")
assert.Nil(t, cmd.Start())
w, err := newWorker(cmd)
@@ -157,7 +157,7 @@ func Test_OnStarted(t *testing.T) {
}
func Test_Error(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "error", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "error", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {
@@ -174,7 +174,7 @@ func Test_Error(t *testing.T) {
}
func Test_NumExecs(t *testing.T) {
- cmd := exec.Command("php", "php-src/tests/client.php", "echo", "pipes")
+ cmd := exec.Command("php", "tests/client.php", "echo", "pipes")
w, _ := NewPipeFactory().SpawnWorker(cmd)
go func() {