diff options
author | bricelalu <[email protected]> | 2021-04-03 16:26:48 +0200 |
---|---|---|
committer | bricelalu <[email protected]> | 2021-04-03 16:35:39 +0200 |
commit | 68a8cd78ba52f90ae263e970fbfa8920207e8b5d (patch) | |
tree | 2afa9a6e4da7bd0a26665b701cd5e94011eb059e /tests/plugins/logger | |
parent | 2feb852ce0a5d29f9ad9c5cb5329de3ce00e7955 (diff) |
test: [Logger] add test for raw mode
Signed-off-by: bricelalu <[email protected]>
Diffstat (limited to 'tests/plugins/logger')
-rw-r--r-- | tests/plugins/logger/configs/.rr-raw-mode.yaml | 2 | ||||
-rw-r--r-- | tests/plugins/logger/logger_test.go | 65 | ||||
-rw-r--r-- | tests/plugins/logger/plugin.go | 7 |
3 files changed, 74 insertions, 0 deletions
diff --git a/tests/plugins/logger/configs/.rr-raw-mode.yaml b/tests/plugins/logger/configs/.rr-raw-mode.yaml new file mode 100644 index 00000000..77bd2f59 --- /dev/null +++ b/tests/plugins/logger/configs/.rr-raw-mode.yaml @@ -0,0 +1,2 @@ +logs: + mode: raw
\ No newline at end of file diff --git a/tests/plugins/logger/logger_test.go b/tests/plugins/logger/logger_test.go index 7f378026..a1bc0f4d 100644 --- a/tests/plugins/logger/logger_test.go +++ b/tests/plugins/logger/logger_test.go @@ -6,6 +6,8 @@ import ( "sync" "testing" + "github.com/kami-zh/go-capturer" + endure "github.com/spiral/endure/pkg/container" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/http" @@ -74,6 +76,69 @@ func TestLogger(t *testing.T) { wg.Wait() } +func TestLoggerRawMode(t *testing.T) { + out := capturer.CaptureOutput(func() { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = "configs/.rr-raw-mode.yaml" + vp.Prefix = "rr" + + err = container.RegisterAll( + vp, + &Plugin{}, + &logger.ZapLogger{}, + ) + assert.NoError(t, err) + + err = container.Init() + if err != nil { + t.Fatal(err) + } + + errCh, err := container.Serve() + if err != nil { + t.Fatal(err) + } + + // stop by CTRL+C + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt) + + stopCh := make(chan struct{}, 1) + + wg := &sync.WaitGroup{} + wg.Add(1) + + go func() { + defer wg.Done() + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + err = container.Stop() + assert.NoError(t, err) + return + case <-stopCh: + assert.NoError(t, container.Stop()) + return + } + } + }() + + stopCh <- struct{}{} + wg.Wait() + }) + + assert.Contains(t, out, `{"field": "value"}`) +} + func TestLoggerNoConfig(t *testing.T) { container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.ErrorLevel)) if err != nil { diff --git a/tests/plugins/logger/plugin.go b/tests/plugins/logger/plugin.go index 9ddf9ec9..38da7266 100644 --- a/tests/plugins/logger/plugin.go +++ b/tests/plugins/logger/plugin.go @@ -1,6 +1,8 @@ package logger import ( + "strings" + "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/plugins/config" "github.com/spiral/roadrunner/v2/plugins/logger" @@ -28,6 +30,11 @@ func (p1 *Plugin) Serve() chan error { p1.log.Info("error", "test") p1.log.Debug("error", "test") p1.log.Warn("error", "test") + + // test the `raw` mode + messageJson := []byte(`{"field": "value"}`) + p1.log.Debug(strings.TrimRight(string(messageJson), " \n\t")) + return errCh } |