summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorValery Piashchynski <[email protected]>2020-03-14 00:57:28 +0300
committerValery Piashchynski <[email protected]>2020-03-14 00:57:28 +0300
commit4ae5a7e7213798892d511a7a53ecd148e22ca7f6 (patch)
treec246bb679d5ba3f8bdb4857df074d9ea51e9deeb /service
parentbb96eee28c4a171ad663861e08fce73708ce09ad (diff)
Fix panic in Serve
Add test for the bug Uncomment test in container. It's working properly now
Diffstat (limited to 'service')
-rw-r--r--service/container.go7
-rw-r--r--service/container_test.go33
-rw-r--r--service/gzip/service.go5
-rw-r--r--service/gzip/service_test.go18
4 files changed, 47 insertions, 16 deletions
diff --git a/service/container.go b/service/container.go
index 85f63eeb..77a6dfc0 100644
--- a/service/container.go
+++ b/service/container.go
@@ -166,8 +166,10 @@ func (c *container) Init(cfg Config) error {
// Serve all configured services. Non blocking.
func (c *container) Serve() error {
+ var running = 0
for _, e := range c.services {
if e.hasStatus(StatusOK) && e.canServe() {
+ running++
c.log.Debugf("[%s]: started", e.name)
go func(e *entry) {
e.setStatus(StatusServing)
@@ -187,6 +189,11 @@ func (c *container) Serve() error {
}
}
+ // simple handler to handle empty configs
+ if running == 0 {
+ return nil
+ }
+
for fail := range c.errc {
if fail.err == errTempFix223 {
// if we call stop, then stop all plugins
diff --git a/service/container_test.go b/service/container_test.go
index 94bc243f..d7ca73a7 100644
--- a/service/container_test.go
+++ b/service/container_test.go
@@ -302,22 +302,23 @@ func TestContainer_ConfigureTwice(t *testing.T) {
assert.Error(t, c.Init(&testCfg{`{"test":"something"}`}))
}
-//func TestContainer_ServeEmptyContainer(t *testing.T) {
-// logger, hook := test.NewNullLogger()
-// logger.SetLevel(logrus.DebugLevel)
-//
-// svc := &testService{ok: true}
-//
-// c := NewContainer(logger)
-// c.Register("test", svc)
-// assert.Equal(t, 0, len(hook.Entries))
-//
-// go assert.NoError(t, c.Serve())
-//
-// time.Sleep(time.Millisecond * 500)
-//
-// c.Stop()
-//}
+// bug #276 test
+func TestContainer_ServeEmptyContainer(t *testing.T) {
+ logger, hook := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ svc := &testService{ok: true}
+
+ c := NewContainer(logger)
+ c.Register("test", svc)
+ assert.Equal(t, 0, len(hook.Entries))
+
+ go assert.NoError(t, c.Serve())
+
+ time.Sleep(time.Millisecond * 500)
+
+ c.Stop()
+}
func TestContainer_Serve(t *testing.T) {
logger, hook := test.NewNullLogger()
diff --git a/service/gzip/service.go b/service/gzip/service.go
index bb6bcfcd..5ae3ffd5 100644
--- a/service/gzip/service.go
+++ b/service/gzip/service.go
@@ -1,6 +1,7 @@
package gzip
import (
+ "errors"
"github.com/NYTimes/gziphandler"
rrhttp "github.com/spiral/roadrunner/service/http"
"net/http"
@@ -8,12 +9,16 @@ import (
// ID contains default service name.
const ID = "gzip"
+var httpNotInitialized = errors.New("http service should be defined properly in config")
type Service struct {
cfg *Config
}
func (s *Service) Init(cfg *Config, r *rrhttp.Service) (bool, error) {
+ if r == nil {
+ return false, httpNotInitialized
+ }
s.cfg = cfg
if !s.cfg.Enable {
diff --git a/service/gzip/service_test.go b/service/gzip/service_test.go
index 858dbe56..ba14b862 100644
--- a/service/gzip/service_test.go
+++ b/service/gzip/service_test.go
@@ -47,3 +47,21 @@ func Test_Disabled(t *testing.T) {
assert.NotNil(t, s)
assert.Equal(t, service.StatusInactive, st)
}
+
+// TEST bug #275
+func Test_Bug275(t *testing.T) {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
+
+ assert.Error(t, c.Init(&testCfg{
+ httpCfg:"",
+ gzip: `{"enable":false}`,
+ }))
+
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusInactive, st)
+}