2019-02-03 00:27:28 +00:00
|
|
|
package m
|
|
|
|
|
|
|
|
import (
|
2019-02-09 19:08:30 +00:00
|
|
|
"context"
|
2019-02-03 00:27:28 +00:00
|
|
|
"encoding/json"
|
|
|
|
. "testing"
|
|
|
|
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mcfg"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceCtx(t *T) {
|
|
|
|
t.Run("log-level", func(t *T) {
|
2019-06-17 21:56:14 +00:00
|
|
|
cmp := RootComponent()
|
2019-02-03 00:27:28 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// pull the Logger out of the component and set the Handler on it, so we
|
|
|
|
// can check the log level
|
2019-02-03 00:27:28 +00:00
|
|
|
var msgs []mlog.Message
|
2019-06-17 21:56:14 +00:00
|
|
|
logger := mlog.GetLogger(cmp)
|
2019-02-03 00:27:28 +00:00
|
|
|
logger.SetHandler(func(msg mlog.Message) error {
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
return nil
|
|
|
|
})
|
2019-06-17 21:56:14 +00:00
|
|
|
mlog.SetLogger(cmp, logger)
|
2019-02-03 00:27:28 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
// create a child Component before running to ensure it the change propagates
|
2019-02-03 00:27:28 +00:00
|
|
|
// correctly.
|
2019-06-17 21:56:14 +00:00
|
|
|
cmpA := cmp.Child("A")
|
2019-02-03 00:27:28 +00:00
|
|
|
|
|
|
|
params := mcfg.ParamValues{{Name: "log-level", Value: json.RawMessage(`"DEBUG"`)}}
|
2019-06-17 21:56:14 +00:00
|
|
|
cmp.SetValue(cmpKeyCfgSrc, params)
|
|
|
|
MustInit(cmp)
|
2019-02-03 00:27:28 +00:00
|
|
|
|
2019-06-17 21:56:14 +00:00
|
|
|
mlog.From(cmpA).Info("foo")
|
|
|
|
mlog.From(cmpA).Debug("bar")
|
2019-03-10 23:23:37 +00:00
|
|
|
massert.Require(t,
|
2019-06-23 17:43:54 +00:00
|
|
|
massert.Length(msgs, 3),
|
|
|
|
massert.Equal(msgs[0].Level.String(), "DEBUG"),
|
|
|
|
massert.Equal(msgs[0].Description, "initialization completed successfully"),
|
|
|
|
massert.Equal(msgs[0].Contexts, []context.Context{cmp.Context()}),
|
|
|
|
massert.Equal(msgs[1].Level.String(), "INFO"),
|
|
|
|
massert.Equal(msgs[1].Description, "foo"),
|
2019-06-17 21:56:14 +00:00
|
|
|
massert.Equal(msgs[1].Contexts, []context.Context{cmpA.Context()}),
|
2019-06-23 17:43:54 +00:00
|
|
|
massert.Equal(msgs[2].Level.String(), "DEBUG"),
|
|
|
|
massert.Equal(msgs[2].Description, "bar"),
|
|
|
|
massert.Equal(msgs[2].Contexts, []context.Context{cmpA.Context()}),
|
2019-03-10 23:23:37 +00:00
|
|
|
)
|
2019-02-03 00:27:28 +00:00
|
|
|
})
|
|
|
|
}
|