diff options
author | Valery Piashchynski <[email protected]> | 2020-11-05 17:58:23 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2020-11-05 17:58:23 +0300 |
commit | 9fbe7726dd55cfedda724b7644e1b6bf7c1a6cb4 (patch) | |
tree | f2bf9b97d38103de51e2d140aa76666f9c6341c8 /plugins/logger | |
parent | edc45b3e24afdb5e56e74ffbbbd50e0e3b04922b (diff) | |
parent | 73da7300fcc9b8b22faa1c91fc1faff22ab944ff (diff) |
Merge pull request #388 from spiral/enhancement/testsv2.0.0-alpha15
Tests for the new plugins
Diffstat (limited to 'plugins/logger')
-rw-r--r-- | plugins/logger/encoder.go | 2 | ||||
-rw-r--r-- | plugins/logger/plugin.go (renamed from plugins/logger/zap_logger.go) | 31 | ||||
-rw-r--r-- | plugins/logger/tests/.rr.yaml | 0 | ||||
-rw-r--r-- | plugins/logger/tests/logger_test.go | 74 | ||||
-rw-r--r-- | plugins/logger/tests/plugin.go | 40 |
5 files changed, 129 insertions, 18 deletions
diff --git a/plugins/logger/encoder.go b/plugins/logger/encoder.go index 66cd84f1..4ff583c4 100644 --- a/plugins/logger/encoder.go +++ b/plugins/logger/encoder.go @@ -61,6 +61,6 @@ func UTCTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder) { // returns string hash func stringHash(name string, base int) int { h := fnv.New32a() - h.Write([]byte(name)) + _, _ = h.Write([]byte(name)) return int(h.Sum32()) % base } diff --git a/plugins/logger/zap_logger.go b/plugins/logger/plugin.go index 8c1739f2..f05d0ff0 100644 --- a/plugins/logger/zap_logger.go +++ b/plugins/logger/plugin.go @@ -2,6 +2,7 @@ package logger import ( "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/log" "github.com/spiral/roadrunner/v2/plugins/config" "go.uber.org/zap" ) @@ -9,14 +10,6 @@ import ( // ServiceName declares service name. const ServiceName = "logs" -type LogFactory interface { - // GlobalLogger returns global log instance. - GlobalLogger() *zap.Logger - - // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. - NamedLogger(name string) *zap.Logger -} - // ZapLogger manages zap logger. type ZapLogger struct { base *zap.Logger @@ -25,8 +18,8 @@ type ZapLogger struct { } // Init logger service. -func (z *ZapLogger) Init(cfg config.Provider) (err error) { - err = cfg.UnmarshalKey(ServiceName, &z.cfg) +func (z *ZapLogger) Init(cfg config.Configurer) error { + err := cfg.UnmarshalKey(ServiceName, &z.cfg) if err != nil { return err } @@ -41,28 +34,32 @@ func (z *ZapLogger) Init(cfg config.Provider) (err error) { } // DefaultLogger returns default logger. -func (z *ZapLogger) DefaultLogger() (*zap.Logger, error) { - return z.base, nil +func (z *ZapLogger) DefaultLogger() (log.Logger, error) { + return log.NewZapAdapter(z.base), nil } // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. -func (z *ZapLogger) NamedLogger(name string) (*zap.Logger, error) { +func (z *ZapLogger) NamedLogger(name string) (log.Logger, error) { if cfg, ok := z.channels.Channels[name]; ok { - return cfg.BuildLogger() + l, err := cfg.BuildLogger() + if err != nil { + return nil, err + } + return log.NewZapAdapter(l), nil } - return z.base.Named(name), nil + return log.NewZapAdapter(z.base.Named(name)), nil } // NamedLogger returns logger dedicated to the specific channel. Similar to Named() but also reads the core params. -func (z *ZapLogger) ServiceLogger(n endure.Named) (*zap.Logger, error) { +func (z *ZapLogger) ServiceLogger(n endure.Named) (log.Logger, error) { return z.NamedLogger(n.Name()) } // Provides declares factory methods. func (z *ZapLogger) Provides() []interface{} { return []interface{}{ - z.DefaultLogger, z.ServiceLogger, + z.DefaultLogger, } } diff --git a/plugins/logger/tests/.rr.yaml b/plugins/logger/tests/.rr.yaml new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/plugins/logger/tests/.rr.yaml diff --git a/plugins/logger/tests/logger_test.go b/plugins/logger/tests/logger_test.go new file mode 100644 index 00000000..1df74c47 --- /dev/null +++ b/plugins/logger/tests/logger_test.go @@ -0,0 +1,74 @@ +package tests + +import ( + "os" + "os/signal" + "testing" + "time" + + "github.com/spiral/endure" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/stretchr/testify/assert" +) + +func TestLogger(t *testing.T) { + container, err := endure.NewContainer(nil, endure.RetryOnFail(true), endure.SetLogLevel(endure.DebugLevel)) + if err != nil { + t.Fatal(err) + } + // config plugin + vp := &config.Viper{} + vp.Path = ".rr.yaml" + vp.Prefix = "rr" + err = container.Register(vp) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&Plugin{}) + if err != nil { + t.Fatal(err) + } + + err = container.Register(&logger.ZapLogger{}) + if err != nil { + t.Fatal(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) + + // stop after 10 seconds + tt := time.NewTicker(time.Second * 10) + + for { + select { + case e := <-errCh: + assert.NoError(t, e.Error) + assert.NoError(t, container.Stop()) + return + case <-c: + er := container.Stop() + if er != nil { + panic(er) + } + return + case <-tt.C: + tt.Stop() + assert.NoError(t, container.Stop()) + return + } + } +} diff --git a/plugins/logger/tests/plugin.go b/plugins/logger/tests/plugin.go new file mode 100644 index 00000000..75d2736d --- /dev/null +++ b/plugins/logger/tests/plugin.go @@ -0,0 +1,40 @@ +package tests + +import ( + "github.com/spiral/errors" + "github.com/spiral/roadrunner/v2/log" + "github.com/spiral/roadrunner/v2/plugins/config" +) + +type Plugin struct { + config config.Configurer + log log.Logger +} + +func (p1 *Plugin) Init(cfg config.Configurer, log log.Logger) error { + p1.config = cfg + p1.log = log + return nil +} + +func (p1 *Plugin) Serve() chan error { + errCh := make(chan error, 1) + p1.log.Error("error", "test", errors.E(errors.Str("test"))) + p1.log.Info("error", "test", errors.E(errors.Str("test"))) + p1.log.Debug("error", "test", errors.E(errors.Str("test"))) + p1.log.Warn("error", "test", errors.E(errors.Str("test"))) + + p1.log.Error("error", "test") + p1.log.Info("error", "test") + p1.log.Debug("error", "test") + p1.log.Warn("error", "test") + return errCh +} + +func (p1 *Plugin) Stop() error { + return nil +} + +func (p1 *Plugin) Name() string { + return "logger_plugin" +} |