diff options
author | Wolfy-J <[email protected]> | 2018-06-07 15:37:21 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2018-06-07 15:37:21 +0300 |
commit | ef26e854480562de15edffe3b38f86bc22bedd76 (patch) | |
tree | 258d89fb103ae184a87dcb47de42a8f275582866 | |
parent | 33040a9b1bce85e174ee68f6ba8c73b1dbf43b56 (diff) |
server failure
-rw-r--r-- | pool.go | 4 | ||||
-rw-r--r-- | server.go | 24 | ||||
-rw-r--r-- | server_test.go | 44 | ||||
-rw-r--r-- | static_pool.go | 2 | ||||
-rw-r--r-- | static_pool_test.go | 2 |
5 files changed, 51 insertions, 25 deletions
@@ -1,8 +1,8 @@ package roadrunner const ( - // EventWorkerCreate thrown when new worker is spawned. - EventWorkerCreate = iota + // EventWorkerConstruct thrown when new worker is spawned. + EventWorkerConstruct = iota + 100 // EventWorkerDestruct thrown after worker destruction. EventWorkerDestruct @@ -7,23 +7,20 @@ import ( ) const ( - // EventNewPool triggered when server creates new pool. - EventServerStart = iota + 128 + // EventPoolConstruct triggered when server creates new pool. + EventServerStart = iota + 200 - // EventNewPool triggered when server creates new pool. + // EventPoolConstruct triggered when server creates new pool. EventServerStop // EventServerFailure triggered when server is unable to replace dead pool. EventServerFailure - // EventReplaceFailure triggered when server can not replace pool while the re-configuration. - EventReplaceFailure + // EventPoolConstruct triggered when server creates new pool. + EventPoolConstruct - // EventNewPool triggered when server creates new pool. - EventNewPool - - // EventDestroyPool triggered when server destroys existed pool. - EventDestroyPool + // EventPoolDestruct triggered when server destroys existed pool. + EventPoolDestruct ) // Service manages pool creation and swapping. @@ -77,7 +74,6 @@ func (srv *Server) Reconfigure(cfg *ServerConfig) error { pool, err := NewPool(srv.cmd, srv.factory, cfg.Pool) if err != nil { - srv.throw(EventReplaceFailure, err) return err } @@ -86,11 +82,11 @@ func (srv *Server) Reconfigure(cfg *ServerConfig) error { srv.pool.Observe(srv.poolObserver) srv.mu.Unlock() - srv.throw(EventNewPool, pool) + srv.throw(EventPoolConstruct, pool) if previous != nil { go func(previous Pool) { - srv.throw(EventDestroyPool, previous) + srv.throw(EventPoolDestruct, previous) previous.Destroy() }(previous) } @@ -132,7 +128,7 @@ func (srv *Server) Stop() error { return nil } - srv.throw(EventDestroyPool, srv.pool) + srv.throw(EventPoolDestruct, srv.pool) srv.pool.Destroy() srv.factory.Close() diff --git a/server_test.go b/server_test.go index 8706f3ec..1cab7cd3 100644 --- a/server_test.go +++ b/server_test.go @@ -161,7 +161,7 @@ func TestServer_Reset(t *testing.T) { assert.NotEqual(t, pid, srv.Workers()[0].Pid) } -func TestServer_HandleWorkerFailure(t *testing.T) { +func TestServer_ReplacePool(t *testing.T) { srv := NewServer( func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", "pipes") }, &ServerConfig{ @@ -176,18 +176,48 @@ func TestServer_HandleWorkerFailure(t *testing.T) { assert.NoError(t, srv.Start()) - destructed := make(chan interface{}) + constructed := make(chan interface{}) srv.Observe(func(e int, ctx interface{}) { - if e == EventWorkerCreate { - close(destructed) + if e == EventPoolConstruct { + close(constructed) } }) - // killing random worker and expecting pool to replace it - srv.Workers()[0].cmd.Process.Kill() - <-destructed + srv.Reset() + <-constructed for _, w := range srv.Workers() { assert.Equal(t, StateReady, w.state.Value()) } } + +func TestServer_HandleServerFailure(t *testing.T) { + mode := "pipes" + srv := NewServer( + func() *exec.Cmd { return exec.Command("php", "php-src/tests/client.php", "echo", mode) }, + &ServerConfig{ + Relay: "pipes", + Pool: Config{ + NumWorkers: 1, + AllocateTimeout: time.Second, + DestroyTimeout: time.Second, + }, + }) + defer srv.Stop() + + assert.NoError(t, srv.Start()) + + failure := make(chan interface{}) + srv.Observe(func(e int, ctx interface{}) { + if e == EventServerFailure { + close(failure) + } + }) + + // killing random worker and expecting pool to replace it + mode = "suddenly-broken" + srv.Workers()[0].cmd.Process.Kill() + + <-failure + assert.True(t, true) +} diff --git a/static_pool.go b/static_pool.go index faa2b696..a04190dd 100644 --- a/static_pool.go +++ b/static_pool.go @@ -197,7 +197,7 @@ func (p *StaticPool) createWorker() (*Worker, error) { return nil, err } - p.throw(EventWorkerCreate, w) + p.throw(EventWorkerConstruct, w) go func(w *Worker) { err := w.Wait() diff --git a/static_pool_test.go b/static_pool_test.go index b7e5b0dd..58e208d5 100644 --- a/static_pool_test.go +++ b/static_pool_test.go @@ -185,7 +185,7 @@ func Test_StaticPool_Broken_FromOutside(t *testing.T) { destructed := make(chan interface{}) p.Observe(func(e int, ctx interface{}) { - if e == EventWorkerCreate { + if e == EventWorkerConstruct { close(destructed) } }) |