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/mctx"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mlog"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mrun"
|
|
|
|
"github.com/mediocregopher/mediocre-go-lib/mtest/massert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestServiceCtx(t *T) {
|
|
|
|
t.Run("log-level", func(t *T) {
|
2019-02-09 19:08:30 +00:00
|
|
|
ctx := ServiceContext()
|
2019-02-03 00:27:28 +00:00
|
|
|
|
|
|
|
// pull the Logger out of the ctx and set the Handler on it, so we can check
|
|
|
|
// the log level
|
|
|
|
var msgs []mlog.Message
|
|
|
|
logger := mlog.From(ctx)
|
|
|
|
logger.SetHandler(func(msg mlog.Message) error {
|
|
|
|
msgs = append(msgs, msg)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
// create a child Context before running to ensure it the change propagates
|
|
|
|
// correctly.
|
2019-02-05 20:18:17 +00:00
|
|
|
ctxA := mctx.NewChild(ctx, "A")
|
|
|
|
ctx = mctx.WithChild(ctx, ctxA)
|
2019-02-03 00:27:28 +00:00
|
|
|
|
|
|
|
params := mcfg.ParamValues{{Name: "log-level", Value: json.RawMessage(`"DEBUG"`)}}
|
|
|
|
if err := mcfg.Populate(ctx, params); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
} else if err := mrun.Start(ctx); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-02-09 19:08:30 +00:00
|
|
|
mlog.From(ctxA).Info("foo", ctxA)
|
|
|
|
mlog.From(ctxA).Debug("bar", ctxA)
|
2019-02-03 00:27:28 +00:00
|
|
|
massert.Fatal(t, massert.All(
|
|
|
|
massert.Len(msgs, 2),
|
|
|
|
massert.Equal(msgs[0].Level.String(), "INFO"),
|
2019-02-05 20:18:17 +00:00
|
|
|
massert.Equal(msgs[0].Description, "foo"),
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal(msgs[0].Contexts, []context.Context{ctxA}),
|
2019-02-03 00:27:28 +00:00
|
|
|
massert.Equal(msgs[1].Level.String(), "DEBUG"),
|
2019-02-05 20:18:17 +00:00
|
|
|
massert.Equal(msgs[1].Description, "bar"),
|
2019-02-09 19:08:30 +00:00
|
|
|
massert.Equal(msgs[1].Contexts, []context.Context{ctxA}),
|
2019-02-03 00:27:28 +00:00
|
|
|
))
|
|
|
|
})
|
|
|
|
}
|