mirror of https://github.com/knative/pkg.git
Remove caching in test logger setup (#661)
* Remove caching in test logger setup * Revert "Remove caching in test logger setup" This reverts commite3c5349e3c
. * Don't create new logger in SetupFakeContextWithCancel * Revert "Don't create new logger in SetupFakeContextWithCancel" This reverts commit2d57ccf028
. * Remove test logger cache, and leave an empty shell for ClearAll()
This commit is contained in:
parent
b55b842259
commit
06918f4c69
|
@ -18,7 +18,6 @@ package testing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"sync"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
|
@ -27,40 +26,19 @@ import (
|
||||||
"knative.dev/pkg/logging"
|
"knative.dev/pkg/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
loggers = make(map[string]*zap.SugaredLogger)
|
|
||||||
m sync.Mutex
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestLogger gets a logger to use in unit and end to end tests
|
// TestLogger gets a logger to use in unit and end to end tests
|
||||||
func TestLogger(t *testing.T) *zap.SugaredLogger {
|
func TestLogger(t *testing.T) *zap.SugaredLogger {
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
|
|
||||||
logger, ok := loggers[t.Name()]
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
return logger
|
|
||||||
}
|
|
||||||
|
|
||||||
opts := zaptest.WrapOptions(
|
opts := zaptest.WrapOptions(
|
||||||
zap.AddCaller(),
|
zap.AddCaller(),
|
||||||
zap.Development(),
|
zap.Development(),
|
||||||
)
|
)
|
||||||
|
|
||||||
logger = zaptest.NewLogger(t, opts).Sugar().Named(t.Name())
|
return zaptest.NewLogger(t, opts).Sugar()
|
||||||
loggers[t.Name()] = logger
|
|
||||||
|
|
||||||
return logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearAll removes all the testing loggers.
|
// ClearAll removes all the testing loggers.
|
||||||
// `go test -count=X` executes runs in the same process, thus the map
|
// TODO(taragu) remove this after removing all ClearAll() calls from serving and eventing
|
||||||
// persists between the runs, but the `t` will no longer be valid and will
|
func ClearAll() {}
|
||||||
// cause a panic deep inside testing code.
|
|
||||||
func ClearAll() {
|
|
||||||
loggers = make(map[string]*zap.SugaredLogger)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestContextWithLogger returns a context with a logger to be used in tests
|
// TestContextWithLogger returns a context with a logger to be used in tests
|
||||||
func TestContextWithLogger(t *testing.T) context.Context {
|
func TestContextWithLogger(t *testing.T) context.Context {
|
||||||
|
|
Loading…
Reference in New Issue