From 2faf7bc2cd883294306466282f313bc5a1dc79ef Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 20 Apr 2021 12:09:39 +0300 Subject: - Make http.Serve() async Signed-off-by: Valery Piashchynski --- tests/plugins/gzip/plugin_test.go | 1 + tests/plugins/http/http_plugin_test.go | 39 +++++++++++++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) (limited to 'tests/plugins') diff --git a/tests/plugins/gzip/plugin_test.go b/tests/plugins/gzip/plugin_test.go index 9a9c760b..844fd411 100644 --- a/tests/plugins/gzip/plugin_test.go +++ b/tests/plugins/gzip/plugin_test.go @@ -81,6 +81,7 @@ func TestGzipPlugin(t *testing.T) { } }() + time.Sleep(time.Second * 2) t.Run("GzipCheckHeader", headerCheck) stopCh <- struct{}{} diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go index 73d6d102..4a491990 100644 --- a/tests/plugins/http/http_plugin_test.go +++ b/tests/plugins/http/http_plugin_test.go @@ -1227,10 +1227,43 @@ func TestHttpBrokenPipes(t *testing.T) { err = cont.Init() assert.NoError(t, err) - _, err = cont.Serve() - assert.Error(t, err) + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + wg := &sync.WaitGroup{} + wg.Add(1) + + stopCh := make(chan struct{}, 1) + + go func() { + defer wg.Done() + for { + select { + // should be error from the plugin + case e := <-ch: + assert.Error(t, e.Error) + return + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() - assert.NoError(t, cont.Stop()) + wg.Wait() } func TestHTTPSupervisedPool(t *testing.T) { -- cgit v1.2.3 From 027ab253d7d4280132c89d8694f3d0c65ccddf79 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 20 Apr 2021 12:14:14 +0300 Subject: - Delay to wait HTTP Signed-off-by: Valery Piashchynski --- tests/plugins/http/http_plugin_test.go | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests/plugins') diff --git a/tests/plugins/http/http_plugin_test.go b/tests/plugins/http/http_plugin_test.go index 4a491990..0e43dac4 100644 --- a/tests/plugins/http/http_plugin_test.go +++ b/tests/plugins/http/http_plugin_test.go @@ -1545,6 +1545,8 @@ func TestHTTPBigRequestSize(t *testing.T) { } }() + time.Sleep(time.Second * 2) + t.Run("HTTPBigEcho10Mb", bigEchoHTTP) stopCh <- struct{}{} -- cgit v1.2.3 From c24f86e6eb1be40d71b8e15099dbc88cc7fad97f Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 20 Apr 2021 14:20:05 +0300 Subject: - Add test cases for the issue 571 --- tests/plugins/metrics/.rr-test.yaml | 18 --- tests/plugins/metrics/configs/.rr-issue-571.yaml | 13 +++ tests/plugins/metrics/configs/.rr-test.yaml | 18 +++ tests/plugins/metrics/metrics_test.go | 141 ++++++++++++++++++++++- 4 files changed, 169 insertions(+), 21 deletions(-) delete mode 100644 tests/plugins/metrics/.rr-test.yaml create mode 100644 tests/plugins/metrics/configs/.rr-issue-571.yaml create mode 100644 tests/plugins/metrics/configs/.rr-test.yaml (limited to 'tests/plugins') diff --git a/tests/plugins/metrics/.rr-test.yaml b/tests/plugins/metrics/.rr-test.yaml deleted file mode 100644 index 4890076f..00000000 --- a/tests/plugins/metrics/.rr-test.yaml +++ /dev/null @@ -1,18 +0,0 @@ -rpc: - listen: tcp://127.0.0.1:6001 - -metrics: - # prometheus client address (path /metrics added automatically) - address: localhost:2112 - collect: - app_metric: - type: histogram - help: "Custom application metric" - labels: [ "type" ] - buckets: [ 0.1, 0.2, 0.3, 1.0 ] - app_metric_counter: - type: counter - help: "Custom application counter." -logs: - mode: development - level: error \ No newline at end of file diff --git a/tests/plugins/metrics/configs/.rr-issue-571.yaml b/tests/plugins/metrics/configs/.rr-issue-571.yaml new file mode 100644 index 00000000..872f777a --- /dev/null +++ b/tests/plugins/metrics/configs/.rr-issue-571.yaml @@ -0,0 +1,13 @@ +rpc: + listen: tcp://127.0.0.1:6001 + +server: + command: "php ../../metrics-issue-571.php" + +http: + address: "0.0.0.0:56444" + pool: + num_workers: 5 + +metrics: + address: "0.0.0.0:23557" diff --git a/tests/plugins/metrics/configs/.rr-test.yaml b/tests/plugins/metrics/configs/.rr-test.yaml new file mode 100644 index 00000000..4890076f --- /dev/null +++ b/tests/plugins/metrics/configs/.rr-test.yaml @@ -0,0 +1,18 @@ +rpc: + listen: tcp://127.0.0.1:6001 + +metrics: + # prometheus client address (path /metrics added automatically) + address: localhost:2112 + collect: + app_metric: + type: histogram + help: "Custom application metric" + labels: [ "type" ] + buckets: [ 0.1, 0.2, 0.3, 1.0 ] + app_metric_counter: + type: counter + help: "Custom application counter." +logs: + mode: development + level: error \ No newline at end of file diff --git a/tests/plugins/metrics/metrics_test.go b/tests/plugins/metrics/metrics_test.go index d552107e..298345b0 100644 --- a/tests/plugins/metrics/metrics_test.go +++ b/tests/plugins/metrics/metrics_test.go @@ -7,6 +7,7 @@ import ( "net/rpc" "os" "os/signal" + "sync" "syscall" "testing" "time" @@ -15,9 +16,11 @@ import ( endure "github.com/spiral/endure/pkg/container" goridgeRpc "github.com/spiral/goridge/v3/pkg/rpc" "github.com/spiral/roadrunner/v2/plugins/config" + httpPlugin "github.com/spiral/roadrunner/v2/plugins/http" "github.com/spiral/roadrunner/v2/plugins/logger" "github.com/spiral/roadrunner/v2/plugins/metrics" rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" + "github.com/spiral/roadrunner/v2/plugins/server" "github.com/spiral/roadrunner/v2/tests/mocks" "github.com/stretchr/testify/assert" ) @@ -54,7 +57,7 @@ func TestMetricsInit(t *testing.T) { cfg := &config.Viper{} cfg.Prefix = "rr" - cfg.Path = ".rr-test.yaml" + cfg.Path = "configs/.rr-test.yaml" err = cont.RegisterAll( cfg, @@ -110,6 +113,138 @@ func TestMetricsInit(t *testing.T) { } } +func TestMetricsIssue571(t *testing.T) { + cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) + if err != nil { + t.Fatal(err) + } + + cfg := &config.Viper{} + cfg.Prefix = "rr" + cfg.Path = "configs/.rr-issue-571.yaml" + + controller := gomock.NewController(t) + mockLogger := mocks.NewMockLogger(controller) + + mockLogger.EXPECT().Debug("worker destructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("worker constructed", "pid", gomock.Any()).AnyTimes() + mockLogger.EXPECT().Debug("Started RPC service", "address", "tcp://127.0.0.1:6001", "services", []string{"metrics"}).MinTimes(1) + mockLogger.EXPECT().Debug("200 GET http://localhost:56444/", "remote", "127.0.0.1", "elapsed", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Info("declaring new metric", "name", "test", "type", gomock.Any(), "namespace", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Info("metric successfully added", "name", "test", "type", gomock.Any(), "namespace", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Info("metric successfully added", "name", "test", "labels", []string{}, "value", gomock.Any()).MinTimes(1) + mockLogger.EXPECT().Info("adding metric", "name", "test", "value", gomock.Any(), "labels", []string{}).MinTimes(1) + mockLogger.EXPECT().Error("metric with provided name already exist", "name", "test", "type", gomock.Any(), "namespace", gomock.Any()).MinTimes(3) + + err = cont.RegisterAll( + cfg, + &metrics.Plugin{}, + &rpcPlugin.Plugin{}, + &server.Plugin{}, + mockLogger, + &httpPlugin.Plugin{}, + ) + assert.NoError(t, err) + + err = cont.Init() + if err != nil { + t.Fatal(err) + } + + ch, err := cont.Serve() + assert.NoError(t, err) + + sig := make(chan os.Signal, 1) + signal.Notify(sig, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) + + // give some time to wait http + time.Sleep(time.Second * 2) + _, err = issue571Http() + assert.NoError(t, err) + + out, err := issue571Metrics() + assert.NoError(t, err) + + assert.Contains(t, out, "HELP test Test counter") + assert.Contains(t, out, "TYPE test counter") + + stopCh := make(chan struct{}, 1) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + defer wg.Done() + for { + select { + case e := <-ch: + assert.Fail(t, "error", e.Error.Error()) + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + case <-sig: + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + case <-stopCh: + // timeout + err = cont.Stop() + if err != nil { + assert.FailNow(t, "error", err.Error()) + } + return + } + } + }() + + time.Sleep(time.Second * 2) + stopCh <- struct{}{} + wg.Wait() +} + +// get request and return body +func issue571Http() (string, error) { + r, err := http.Get("http://localhost:56444") + if err != nil { + return "", err + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", err + } + + err = r.Body.Close() + if err != nil { + return "", err + } + // unsafe + return string(b), err +} + +// get request and return body +func issue571Metrics() (string, error) { + r, err := http.Get("http://localhost:23557") + if err != nil { + return "", err + } + + b, err := ioutil.ReadAll(r.Body) + if err != nil { + return "", err + } + + err = r.Body.Close() + if err != nil { + return "", err + } + // unsafe + return string(b), err +} + func TestMetricsGaugeCollector(t *testing.T) { cont, err := endure.NewContainer(nil, endure.SetLogLevel(endure.ErrorLevel)) if err != nil { @@ -118,7 +253,7 @@ func TestMetricsGaugeCollector(t *testing.T) { cfg := &config.Viper{} cfg.Prefix = "rr" - cfg.Path = ".rr-test.yaml" + cfg.Path = "configs/.rr-test.yaml" err = cont.RegisterAll( cfg, @@ -186,7 +321,7 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { cfg := &config.Viper{} cfg.Prefix = "rr" - cfg.Path = ".rr-test.yaml" + cfg.Path = "configs/.rr-test.yaml" controller := gomock.NewController(t) mockLogger := mocks.NewMockLogger(controller) -- cgit v1.2.3 From c8edd94d7a8c13fe6a2e2580ad0442c16ce97e54 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 20 Apr 2021 15:05:41 +0300 Subject: - Fix bug that after Reset, http handler was witout log listener --- tests/plugins/reload/reload_plugin_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/plugins') diff --git a/tests/plugins/reload/reload_plugin_test.go b/tests/plugins/reload/reload_plugin_test.go index dcbedb0f..6db7b6d0 100644 --- a/tests/plugins/reload/reload_plugin_test.go +++ b/tests/plugins/reload/reload_plugin_test.go @@ -54,7 +54,7 @@ func TestReloadInit(t *testing.T) { mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", "file.txt", "size", gomock.Any()).Times(2) mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").Times(1) mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").Times(1) - mockLogger.EXPECT().Info("HTTP listeners successfully re-added").Times(1) + mockLogger.EXPECT().Info("HTTP handler listeners successfully re-added").Times(1) mockLogger.EXPECT().Info("HTTP plugin successfully restarted").Times(1) mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror @@ -150,7 +150,7 @@ func TestReloadHugeNumberOfFiles(t *testing.T) { mockLogger.EXPECT().Debug("file was added to watcher", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(1) mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").MinTimes(1) mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").MinTimes(1) - mockLogger.EXPECT().Info("HTTP listeners successfully re-added").MinTimes(1) + mockLogger.EXPECT().Info("HTTP handler listeners successfully re-added").MinTimes(1) mockLogger.EXPECT().Info("HTTP plugin successfully restarted").MinTimes(1) mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror @@ -259,7 +259,7 @@ func TestReloadFilterFileExt(t *testing.T) { mockLogger.EXPECT().Debug("file added to the list of removed files", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).AnyTimes() mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").Times(1) mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").Times(1) - mockLogger.EXPECT().Info("HTTP listeners successfully re-added").Times(1) + mockLogger.EXPECT().Info("HTTP handler listeners successfully re-added").MinTimes(1) mockLogger.EXPECT().Info("HTTP plugin successfully restarted").Times(1) mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror @@ -389,7 +389,7 @@ func TestReloadCopy100(t *testing.T) { mockLogger.EXPECT().Debug("file was updated", "path", gomock.Any(), "name", gomock.Any(), "size", gomock.Any()).MinTimes(50) mockLogger.EXPECT().Info("HTTP plugin got restart request. Restarting...").MinTimes(1) mockLogger.EXPECT().Info("HTTP workers Pool successfully restarted").MinTimes(1) - mockLogger.EXPECT().Info("HTTP listeners successfully re-added").MinTimes(1) + mockLogger.EXPECT().Info("HTTP handler listeners successfully re-added").MinTimes(1) mockLogger.EXPECT().Info("HTTP plugin successfully restarted").MinTimes(1) mockLogger.EXPECT().Error(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes() // placeholder for the workerlogerror -- cgit v1.2.3 From 5c63b9b8b774917712bab165b37f360aab509828 Mon Sep 17 00:00:00 2001 From: Valery Piashchynski Date: Tue, 20 Apr 2021 15:19:39 +0300 Subject: - Delay for the http plugin --- tests/plugins/metrics/metrics_test.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests/plugins') diff --git a/tests/plugins/metrics/metrics_test.go b/tests/plugins/metrics/metrics_test.go index 298345b0..8be567ec 100644 --- a/tests/plugins/metrics/metrics_test.go +++ b/tests/plugins/metrics/metrics_test.go @@ -82,6 +82,7 @@ func TestMetricsInit(t *testing.T) { tt := time.NewTimer(time.Second * 5) defer tt.Stop() + time.Sleep(time.Second * 2) out, err := get() assert.NoError(t, err) @@ -279,6 +280,7 @@ func TestMetricsGaugeCollector(t *testing.T) { tt := time.NewTimer(time.Second * 5) defer tt.Stop() + time.Sleep(time.Second * 2) out, err := get() assert.NoError(t, err) assert.Contains(t, out, "my_gauge 100") @@ -439,6 +441,7 @@ func TestMetricsDifferentRPCCalls(t *testing.T) { } }() + time.Sleep(time.Second * 2) t.Run("DeclareMetric", declareMetricsTest) genericOut, err := get() assert.NoError(t, err) -- cgit v1.2.3