summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/composer.json27
-rw-r--r--tests/metrics-issue-571.php37
-rw-r--r--tests/plugins/gzip/plugin_test.go1
-rw-r--r--tests/plugins/http/http_plugin_test.go41
-rw-r--r--tests/plugins/metrics/configs/.rr-issue-571.yaml13
-rw-r--r--tests/plugins/metrics/configs/.rr-test.yaml (renamed from tests/plugins/metrics/.rr-test.yaml)0
-rw-r--r--tests/plugins/metrics/metrics_test.go144
-rw-r--r--tests/plugins/reload/reload_plugin_test.go8
8 files changed, 248 insertions, 23 deletions
diff --git a/tests/composer.json b/tests/composer.json
index cff9bd6b..543b2053 100644
--- a/tests/composer.json
+++ b/tests/composer.json
@@ -1,16 +1,17 @@
{
- "minimum-stability": "beta",
- "prefer-stable": true,
- "require": {
- "nyholm/psr7": "^1.3",
- "spiral/roadrunner": "^2.0",
- "spiral/roadrunner-http": "^2.0",
- "temporal/sdk": ">=1.0",
- "spiral/tokenizer": ">=2.7"
- },
- "autoload": {
- "psr-4": {
- "Temporal\\Tests\\": "src"
+ "minimum-stability": "beta",
+ "prefer-stable": true,
+ "require": {
+ "nyholm/psr7": "^1.3",
+ "spiral/roadrunner": "^2.0",
+ "spiral/roadrunner-http": "^2.0",
+ "temporal/sdk": ">=1.0",
+ "spiral/tokenizer": ">=2.7",
+ "spiral/roadrunner-metrics": "^2.0"
+ },
+ "autoload": {
+ "psr-4": {
+ "Temporal\\Tests\\": "src"
+ }
}
- }
}
diff --git a/tests/metrics-issue-571.php b/tests/metrics-issue-571.php
new file mode 100644
index 00000000..947ae1f7
--- /dev/null
+++ b/tests/metrics-issue-571.php
@@ -0,0 +1,37 @@
+<?php
+
+use Spiral\Goridge;
+use Spiral\RoadRunner;
+use Nyholm\Psr7\Factory;
+
+ini_set('display_errors', 'stderr');
+require __DIR__ . "/vendor/autoload.php";
+
+$worker = new RoadRunner\Http\PSR7Worker(
+ RoadRunner\Worker::create(),
+ new Factory\Psr17Factory(),
+ new Factory\Psr17Factory(),
+ new Factory\Psr17Factory()
+);
+
+$metrics = new RoadRunner\Metrics\Metrics(
+ Goridge\RPC\RPC::create(RoadRunner\Environment::fromGlobals()->getRPCAddress())
+);
+
+$metrics->declare(
+ 'test',
+ RoadRunner\Metrics\Collector::counter()->withHelp('Test counter')
+);
+
+while ($req = $worker->waitRequest()) {
+ try {
+ $rsp = new \Nyholm\Psr7\Response();
+ $rsp->getBody()->write("hello world");
+
+ $metrics->add('test', 1);
+
+ $worker->respond($rsp);
+ } catch (\Throwable $e) {
+ $worker->getWorker()->error((string)$e);
+ }
+}
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..0e43dac4 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) {
@@ -1512,6 +1545,8 @@ func TestHTTPBigRequestSize(t *testing.T) {
}
}()
+ time.Sleep(time.Second * 2)
+
t.Run("HTTPBigEcho10Mb", bigEchoHTTP)
stopCh <- struct{}{}
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/.rr-test.yaml b/tests/plugins/metrics/configs/.rr-test.yaml
index 4890076f..4890076f 100644
--- a/tests/plugins/metrics/.rr-test.yaml
+++ b/tests/plugins/metrics/configs/.rr-test.yaml
diff --git a/tests/plugins/metrics/metrics_test.go b/tests/plugins/metrics/metrics_test.go
index d552107e..8be567ec 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,
@@ -79,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)
@@ -110,6 +114,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 +254,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,
@@ -144,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")
@@ -186,7 +323,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)
@@ -304,6 +441,7 @@ func TestMetricsDifferentRPCCalls(t *testing.T) {
}
}()
+ time.Sleep(time.Second * 2)
t.Run("DeclareMetric", declareMetricsTest)
genericOut, err := get()
assert.NoError(t, err)
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