summaryrefslogtreecommitdiff
path: root/service/http/service_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'service/http/service_test.go')
-rw-r--r--service/http/service_test.go607
1 files changed, 402 insertions, 205 deletions
diff --git a/service/http/service_test.go b/service/http/service_test.go
index 53dbb3df..f7ee33cc 100644
--- a/service/http/service_test.go
+++ b/service/http/service_test.go
@@ -1,6 +1,7 @@
package http
import (
+ "github.com/cenkalti/backoff/v4"
json "github.com/json-iterator/go"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
@@ -77,13 +78,17 @@ func Test_Service_Configure_Disable(t *testing.T) {
}
func Test_Service_Configure_Enable(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":8070",
"maxRequestSize": 1024,
@@ -100,21 +105,36 @@ func Test_Service_Configure_Enable(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
+
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
+
+ return nil
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
}
func Test_Service_Echo(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6536",
"maxRequestSize": 1024,
@@ -131,51 +151,74 @@ func Test_Service_Echo(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
+
+ // should do nothing
+ s.(*Service).Stop()
+
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
- // should do nothing
- s.(*Service).Stop()
+ time.Sleep(time.Millisecond * 100)
- go func() {
- err := c.Serve()
+ req, err := http.NewRequest("GET", "http://localhost:6536?hello=world", nil)
if err != nil {
- t.Errorf("serve error: %v", err)
+ c.Stop()
+ return err
}
- }()
- time.Sleep(time.Millisecond * 100)
- req, err := http.NewRequest("GET", "http://localhost:6536?hello=world", nil)
- assert.NoError(t, err)
+ r, err := http.DefaultClient.Do(req)
+ if err != nil {
+ c.Stop()
+ return err
+ }
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ c.Stop()
+ return err
+ }
+ assert.Equal(t, 201, r.StatusCode)
+ assert.Equal(t, "WORLD", string(b))
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
- b, err := ioutil.ReadAll(r.Body)
- assert.NoError(t, err)
+ err = r.Body.Close()
+ if err != nil {
+ c.Stop()
+ return err
+ }
- assert.NoError(t, err)
- assert.Equal(t, 201, r.StatusCode)
- assert.Equal(t, "WORLD", string(b))
+ c.Stop()
+ return nil
+ }, bkoff)
- err2 := r.Body.Close()
- if err2 != nil {
- t.Errorf("error closing the Body: error %v", err2)
+ if err != nil {
+ t.Fatal(err)
}
- c.Stop()
}
func Test_Service_Env(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(env.ID, env.NewService(map[string]string{"rr": "test"}))
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ c := service.NewContainer(logger)
+ c.Register(env.ID, env.NewService(map[string]string{"rr": "test"}))
+ c.Register(ID, &Service{})
+
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":10031",
"maxRequestSize": 1024,
@@ -192,53 +235,76 @@ func Test_Service_Env(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`, envCfg: `{"env_key":"ENV_VALUE"}`}))
+ }`, envCfg: `{"env_key":"ENV_VALUE"}`})
+ if err != nil {
+ return err
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
- // should do nothing
- s.(*Service).Stop()
+ // should do nothing
+ s.(*Service).Stop()
- go func() {
- err := c.Serve()
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
+
+ time.Sleep(time.Millisecond * 500)
+
+ req, err := http.NewRequest("GET", "http://localhost:10031", nil)
if err != nil {
- t.Errorf("serve error: %v", err)
+ c.Stop()
+ return err
}
- }()
- time.Sleep(time.Second * 1)
+ r, err := http.DefaultClient.Do(req)
+ if err != nil {
+ c.Stop()
+ return err
+ }
- req, err := http.NewRequest("GET", "http://localhost:10031", nil)
- assert.NoError(t, err)
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ c.Stop()
+ return err
+ }
+
+ assert.Equal(t, 200, r.StatusCode)
+ assert.Equal(t, "ENV_VALUE", string(b))
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
- defer func() {
- err := r.Body.Close()
+ err = r.Body.Close()
if err != nil {
- t.Errorf("error closing the Body: error %v", err)
+ c.Stop()
+ return err
}
- }()
- b, err := ioutil.ReadAll(r.Body)
- assert.NoError(t, err)
+ c.Stop()
+ return nil
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
- assert.NoError(t, err)
- assert.Equal(t, 200, r.StatusCode)
- assert.Equal(t, "ENV_VALUE", string(b))
- c.Stop()
}
func Test_Service_ErrorEcho(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6030",
"maxRequestSize": 1024,
@@ -255,61 +321,83 @@ func Test_Service_ErrorEcho(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
- goterr := make(chan interface{})
- s.(*Service).AddListener(func(event int, ctx interface{}) {
- if event == roadrunner.EventStderrOutput {
- if string(ctx.([]byte)) == "WORLD\n" {
- goterr <- nil
+ goterr := make(chan interface{})
+ s.(*Service).AddListener(func(event int, ctx interface{}) {
+ if event == roadrunner.EventStderrOutput {
+ if string(ctx.([]byte)) == "WORLD\n" {
+ goterr <- nil
+ }
}
+ })
+
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
+
+ time.Sleep(time.Millisecond * 500)
+
+ req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil)
+ if err != nil {
+ c.Stop()
+ return err
}
- })
- go func() {
- err := c.Serve()
+ r, err := http.DefaultClient.Do(req)
if err != nil {
- t.Errorf("serve error: %v", err)
+ c.Stop()
+ return err
}
- }()
- time.Sleep(time.Second)
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ c.Stop()
+ return err
+ }
- req, err := http.NewRequest("GET", "http://localhost:6030?hello=world", nil)
- assert.NoError(t, err)
+ <-goterr
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
- defer func() {
- err := r.Body.Close()
+ assert.Equal(t, 201, r.StatusCode)
+ assert.Equal(t, "WORLD", string(b))
+ err = r.Body.Close()
if err != nil {
- t.Errorf("error closing the Body: error %v", err)
+ c.Stop()
+ return err
}
- }()
- b, err := ioutil.ReadAll(r.Body)
- assert.NoError(t, err)
+ c.Stop()
- <-goterr
+ return nil
+ }, bkoff)
- assert.NoError(t, err)
- assert.Equal(t, 201, r.StatusCode)
- assert.Equal(t, "WORLD", string(b))
- c.Stop()
+ if err != nil {
+ t.Fatal(err)
+ }
}
func Test_Service_Middleware(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
+
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6032",
"maxRequestSize": 1024,
@@ -326,80 +414,112 @@ func Test_Service_Middleware(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
-
- s.(*Service).AddMiddleware(func(f http.HandlerFunc) http.HandlerFunc {
- return func(w http.ResponseWriter, r *http.Request) {
- if r.URL.Path == "/halt" {
- w.WriteHeader(500)
- _, err := w.Write([]byte("halted"))
- if err != nil {
- t.Errorf("error writing the data to the http reply: error %v", err)
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
+
+ s.(*Service).AddMiddleware(func(f http.HandlerFunc) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/halt" {
+ w.WriteHeader(500)
+ _, err := w.Write([]byte("halted"))
+ if err != nil {
+ t.Errorf("error writing the data to the http reply: error %v", err)
+ }
+ } else {
+ f(w, r)
}
- } else {
- f(w, r)
}
+ })
+
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
+ time.Sleep(time.Millisecond * 500)
+
+ req, err := http.NewRequest("GET", "http://localhost:6032?hello=world", nil)
+ if err != nil {
+ c.Stop()
+ return err
}
- })
- go func() {
- err := c.Serve()
+ r, err := http.DefaultClient.Do(req)
if err != nil {
- t.Errorf("serve error: %v", err)
+ c.Stop()
+ return err
}
- }()
- time.Sleep(time.Second)
- req, err := http.NewRequest("GET", "http://localhost:6032?hello=world", nil)
- assert.NoError(t, err)
+ b, err := ioutil.ReadAll(r.Body)
+ if err != nil {
+ c.Stop()
+ return err
+ }
- r, err := http.DefaultClient.Do(req)
- assert.NoError(t, err)
+ assert.Equal(t, 201, r.StatusCode)
+ assert.Equal(t, "WORLD", string(b))
- b, err := ioutil.ReadAll(r.Body)
- assert.NoError(t, err)
+ err = r.Body.Close()
+ if err != nil {
+ c.Stop()
+ return err
+ }
- assert.NoError(t, err)
- assert.Equal(t, 201, r.StatusCode)
- assert.Equal(t, "WORLD", string(b))
+ req, err = http.NewRequest("GET", "http://localhost:6032/halt", nil)
+ if err != nil {
+ c.Stop()
+ return err
+ }
- err = r.Body.Close()
- if err != nil {
- t.Errorf("error closing the Body: error %v", err)
- }
+ r, err = http.DefaultClient.Do(req)
+ if err != nil {
+ c.Stop()
+ return err
+ }
+ b, err = ioutil.ReadAll(r.Body)
+ if err != nil {
+ c.Stop()
+ return err
+ }
- req, err = http.NewRequest("GET", "http://localhost:6032/halt", nil)
- assert.NoError(t, err)
+ assert.Equal(t, 500, r.StatusCode)
+ assert.Equal(t, "halted", string(b))
- r, err = http.DefaultClient.Do(req)
- assert.NoError(t, err)
- b, err = ioutil.ReadAll(r.Body)
- assert.NoError(t, err)
+ err = r.Body.Close()
+ if err != nil {
+ c.Stop()
+ return err
+ }
+ c.Stop()
- assert.NoError(t, err)
- assert.Equal(t, 500, r.StatusCode)
- assert.Equal(t, "halted", string(b))
+ return nil
+ }, bkoff)
- err = r.Body.Close()
if err != nil {
- c.Stop()
- t.Errorf("error closing the Body: error %v", err)
+ t.Fatal(err)
}
- c.Stop()
+
}
func Test_Service_Listener(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6033",
"maxRequestSize": 1024,
@@ -416,39 +536,53 @@ func Test_Service_Listener(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
- s, st := c.Get(ID)
- assert.NotNil(t, s)
- assert.Equal(t, service.StatusOK, st)
+ s, st := c.Get(ID)
+ assert.NotNil(t, s)
+ assert.Equal(t, service.StatusOK, st)
- stop := make(chan interface{})
- s.(*Service).AddListener(func(event int, ctx interface{}) {
- if event == roadrunner.EventServerStart {
- stop <- nil
- }
- })
+ stop := make(chan interface{})
+ s.(*Service).AddListener(func(event int, ctx interface{}) {
+ if event == roadrunner.EventServerStart {
+ stop <- nil
+ }
+ })
- go func() {
- err := c.Serve()
- if err != nil {
- t.Errorf("serve error: %v", err)
- }
- }()
- time.Sleep(time.Millisecond * 100)
+ go func() {
+ err := c.Serve()
+ if err != nil {
+ t.Errorf("serve error: %v", err)
+ }
+ }()
+ time.Sleep(time.Millisecond * 500)
- c.Stop()
- assert.True(t, true)
+ c.Stop()
+ assert.True(t, true)
+
+ return nil
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
}
func Test_Service_Error(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6034",
"maxRequestSize": 1024,
@@ -465,19 +599,37 @@ func Test_Service_Error(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
+
+ // assert error
+ err = c.Serve()
+ if err == nil {
+ return err
+ }
+
+ return nil
+ }, bkoff)
- assert.Error(t, c.Serve())
+ if err != nil {
+ t.Fatal(err)
+ }
}
func Test_Service_Error2(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
- assert.NoError(t, c.Init(&testCfg{httpCfg: `{
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
+
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6035",
"maxRequestSize": 1024,
@@ -494,19 +646,37 @@ func Test_Service_Error2(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ if err != nil {
+ return err
+ }
+
+ // assert error
+ err = c.Serve()
+ if err == nil {
+ return err
+ }
- assert.Error(t, c.Serve())
+ return nil
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
}
func Test_Service_Error3(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.Error(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": ":6036",
"maxRequestSize": 1024,
@@ -523,17 +693,33 @@ func Test_Service_Error3(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ // assert error
+ if err == nil {
+ return err
+ }
+
+ return nil
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
+
}
func Test_Service_Error4(t *testing.T) {
- logger, _ := test.NewNullLogger()
- logger.SetLevel(logrus.DebugLevel)
+ bkoff := backoff.NewExponentialBackOff()
+ bkoff.MaxElapsedTime = time.Second * 15
- c := service.NewContainer(logger)
- c.Register(ID, &Service{})
+ err := backoff.Retry(func() error {
+ logger, _ := test.NewNullLogger()
+ logger.SetLevel(logrus.DebugLevel)
+
+ c := service.NewContainer(logger)
+ c.Register(ID, &Service{})
- assert.Error(t, c.Init(&testCfg{httpCfg: `{
+ err := c.Init(&testCfg{httpCfg: `{
"enable": true,
"address": "----",
"maxRequestSize": 1024,
@@ -550,7 +736,18 @@ func Test_Service_Error4(t *testing.T) {
"destroyTimeout": 10000000
}
}
- }`}))
+ }`})
+ // assert error
+ if err != nil {
+ return nil
+ }
+
+ return err
+ }, bkoff)
+
+ if err != nil {
+ t.Fatal(err)
+ }
}
func tmpDir() string {