mirror of https://github.com/knative/pkg.git
				
				
				
			Remove caching in test logger setup
This commit is contained in:
		
							parent
							
								
									ec03725bc7
								
							
						
					
					
						commit
						e3c5349e3c
					
				| 
						 | 
				
			
			@ -73,7 +73,6 @@ func TestStoreWatchConfigs(t *testing.T) {
 | 
			
		|||
		return c.Name, nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	store := NewUntypedStore(
 | 
			
		||||
		"name",
 | 
			
		||||
		TestLogger(t),
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -482,7 +482,6 @@ func TestEnqueues(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
	for _, test := range tests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			impl := NewImplWithStats(&NopReconciler{}, TestLogger(t), "Testing", &FakeStatsReporter{})
 | 
			
		||||
			test.work(impl)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -499,7 +498,6 @@ func TestEnqueues(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestEnqeueAfter(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	impl := NewImplWithStats(&NopReconciler{}, TestLogger(t), "Testing", &FakeStatsReporter{})
 | 
			
		||||
	impl.EnqueueAfter(&Resource{
 | 
			
		||||
		ObjectMeta: metav1.ObjectMeta{
 | 
			
		||||
| 
						 | 
				
			
			@ -535,7 +533,6 @@ func TestEnqeueAfter(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestEnqeueKeyAfter(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	impl := NewImplWithStats(&NopReconciler{}, TestLogger(t), "Testing", &FakeStatsReporter{})
 | 
			
		||||
	impl.EnqueueKeyAfter("waiting/for", 5*time.Second)
 | 
			
		||||
	impl.EnqueueKeyAfter("the/waterfall", 500*time.Millisecond)
 | 
			
		||||
| 
						 | 
				
			
			@ -568,7 +565,6 @@ func (cr *CountingReconciler) Reconcile(context.Context, string) error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestStartAndShutdown(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	r := &CountingReconciler{}
 | 
			
		||||
	impl := NewImplWithStats(r, TestLogger(t), "Testing", &FakeStatsReporter{})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -601,7 +597,6 @@ func TestStartAndShutdown(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestStartAndShutdownWithWork(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	r := &CountingReconciler{}
 | 
			
		||||
	reporter := &FakeStatsReporter{}
 | 
			
		||||
	impl := NewImplWithStats(r, TestLogger(t), "Testing", reporter)
 | 
			
		||||
| 
						 | 
				
			
			@ -648,7 +643,6 @@ func (er *ErrorReconciler) Reconcile(context.Context, string) error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestStartAndShutdownWithErroringWork(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	r := &ErrorReconciler{}
 | 
			
		||||
	reporter := &FakeStatsReporter{}
 | 
			
		||||
	impl := NewImplWithStats(r, TestLogger(t), "Testing", reporter)
 | 
			
		||||
| 
						 | 
				
			
			@ -703,7 +697,6 @@ func (er *PermanentErrorReconciler) Reconcile(context.Context, string) error {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestStartAndShutdownWithPermanentErroringWork(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	r := &PermanentErrorReconciler{}
 | 
			
		||||
	reporter := &FakeStatsReporter{}
 | 
			
		||||
	impl := NewImplWithStats(r, TestLogger(t), "Testing", reporter)
 | 
			
		||||
| 
						 | 
				
			
			@ -795,7 +788,6 @@ func (*dummyStore) List() []interface{} {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestImplGlobalResync(t *testing.T) {
 | 
			
		||||
	defer ClearAll()
 | 
			
		||||
	r := &CountingReconciler{}
 | 
			
		||||
	impl := NewImplWithStats(r, TestLogger(t), "Testing", &FakeStatsReporter{})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -18,7 +18,6 @@ package testing
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"context"
 | 
			
		||||
	"sync"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"go.uber.org/zap"
 | 
			
		||||
| 
						 | 
				
			
			@ -27,39 +26,14 @@ import (
 | 
			
		|||
	"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
 | 
			
		||||
func TestLogger(t *testing.T) *zap.SugaredLogger {
 | 
			
		||||
	m.Lock()
 | 
			
		||||
	defer m.Unlock()
 | 
			
		||||
 | 
			
		||||
	logger, ok := loggers[t.Name()]
 | 
			
		||||
 | 
			
		||||
	if ok {
 | 
			
		||||
		return logger
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	opts := zaptest.WrapOptions(
 | 
			
		||||
		zap.AddCaller(),
 | 
			
		||||
		zap.Development(),
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	logger = zaptest.NewLogger(t, opts).Sugar().Named(t.Name())
 | 
			
		||||
	loggers[t.Name()] = logger
 | 
			
		||||
 | 
			
		||||
	return logger
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ClearAll removes all the testing loggers.
 | 
			
		||||
// `go test -count=X` executes runs in the same process, thus the map
 | 
			
		||||
// persists between the runs, but the `t` will no longer be valid and will
 | 
			
		||||
// cause a panic deep inside testing code.
 | 
			
		||||
func ClearAll() {
 | 
			
		||||
	loggers = make(map[string]*zap.SugaredLogger)
 | 
			
		||||
	return zaptest.NewLogger(t, opts).Sugar()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// TestContextWithLogger returns a context with a logger to be used in tests
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -407,7 +407,6 @@ var (
 | 
			
		|||
func TestGetMetricsConfig(t *testing.T) {
 | 
			
		||||
	for _, test := range errorTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			_, err := getMetricsConfig(test.ops, TestLogger(t))
 | 
			
		||||
			if err.Error() != test.expectedErr {
 | 
			
		||||
				t.Errorf("Wanted err: %v, got: %v", test.expectedErr, err)
 | 
			
		||||
| 
						 | 
				
			
			@ -417,7 +416,6 @@ func TestGetMetricsConfig(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
	for _, test := range successTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			mc, err := getMetricsConfig(test.ops, TestLogger(t))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				t.Errorf("Wanted valid config %v, got error %v", test.expectedConfig, err)
 | 
			
		||||
| 
						 | 
				
			
			@ -433,7 +431,6 @@ func TestGetMetricsConfig_fromEnv(t *testing.T) {
 | 
			
		|||
	os.Setenv(defaultBackendEnvName, "stackdriver")
 | 
			
		||||
	for _, test := range envTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			mc, err := getMetricsConfig(test.ops, TestLogger(t))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				t.Errorf("Wanted valid config %v, got error %v", test.expectedConfig, err)
 | 
			
		||||
| 
						 | 
				
			
			@ -450,7 +447,6 @@ func TestIsNewExporterRequired(t *testing.T) {
 | 
			
		|||
	setCurMetricsConfig(nil)
 | 
			
		||||
	for _, test := range successTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			mc, err := getMetricsConfig(test.ops, TestLogger(t))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				t.Errorf("Wanted valid config %v, got error %v", test.expectedConfig, err)
 | 
			
		||||
| 
						 | 
				
			
			@ -484,7 +480,6 @@ func TestUpdateExporter(t *testing.T) {
 | 
			
		|||
	oldConfig := getCurMetricsConfig()
 | 
			
		||||
	for _, test := range successTests[1:] {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			UpdateExporter(test.ops, TestLogger(t))
 | 
			
		||||
			mConfig := getCurMetricsConfig()
 | 
			
		||||
			if mConfig == oldConfig {
 | 
			
		||||
| 
						 | 
				
			
			@ -499,7 +494,6 @@ func TestUpdateExporter(t *testing.T) {
 | 
			
		|||
 | 
			
		||||
	for _, test := range errorTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			UpdateExporter(test.ops, TestLogger(t))
 | 
			
		||||
			mConfig := getCurMetricsConfig()
 | 
			
		||||
			if mConfig != oldConfig {
 | 
			
		||||
| 
						 | 
				
			
			@ -513,7 +507,6 @@ func TestUpdateExporter_doesNotCreateExporter(t *testing.T) {
 | 
			
		|||
	setCurMetricsConfig(nil)
 | 
			
		||||
	for _, test := range errorTests {
 | 
			
		||||
		t.Run(test.name, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			UpdateExporter(test.ops, TestLogger(t))
 | 
			
		||||
			mConfig := getCurMetricsConfig()
 | 
			
		||||
			if mConfig != nil {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -498,7 +498,6 @@ func TestStrictValidation(t *testing.T) {
 | 
			
		|||
	}
 | 
			
		||||
	for n, tc := range testCases {
 | 
			
		||||
		t.Run(n, func(t *testing.T) {
 | 
			
		||||
			defer ClearAll()
 | 
			
		||||
			ctx := TestContextWithLogger(t)
 | 
			
		||||
			if tc.strict {
 | 
			
		||||
				ctx = apis.DisallowDeprecated(ctx)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -320,7 +320,6 @@ func TestDoubleShutdown(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestDurableConnectionWhenConnectionBreaksDown(t *testing.T) {
 | 
			
		||||
	defer ktesting.ClearAll()
 | 
			
		||||
	testPayload := "test"
 | 
			
		||||
	reconnectChan := make(chan struct{})
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -360,7 +359,6 @@ func TestDurableConnectionWhenConnectionBreaksDown(t *testing.T) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func TestDurableConnectionSendsPingsRegularly(t *testing.T) {
 | 
			
		||||
	defer ktesting.ClearAll()
 | 
			
		||||
	// Reset pongTimeout to something quite short.
 | 
			
		||||
	pongTimeout = 100 * time.Millisecond
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue