diff options
author | Valery Piashchynski <[email protected]> | 2021-04-20 14:20:05 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-04-20 14:20:05 +0300 |
commit | c24f86e6eb1be40d71b8e15099dbc88cc7fad97f (patch) | |
tree | 7204e7a1b14cb1b316a958731494e67c8fcfe7f2 /tests | |
parent | 027ab253d7d4280132c89d8694f3d0c65ccddf79 (diff) |
- Add test cases for the issue 571
Diffstat (limited to 'tests')
-rw-r--r-- | tests/composer.json | 27 | ||||
-rw-r--r-- | tests/metrics-issue-571.php | 37 | ||||
-rw-r--r-- | tests/plugins/metrics/configs/.rr-issue-571.yaml | 13 | ||||
-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.go | 141 |
5 files changed, 202 insertions, 16 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/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..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) |