k8s.io/apiserver@v0.31.1/pkg/util/flowcontrol/request/object_count_tracker_test.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package request
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/google/go-cmp/cmp"
    25  	testclock "k8s.io/utils/clock/testing"
    26  )
    27  
    28  func TestStorageObjectCountTracker(t *testing.T) {
    29  	tests := []struct {
    30  		name          string
    31  		lastUpdated   time.Duration
    32  		count         int64
    33  		errExpected   error
    34  		countExpected int64
    35  	}{
    36  		{
    37  			name:        "object count not tracked for given resource",
    38  			count:       -2,
    39  			errExpected: ObjectCountNotFoundErr,
    40  		},
    41  		{
    42  			name:        "transient failure",
    43  			count:       -1,
    44  			errExpected: ObjectCountNotFoundErr,
    45  		},
    46  		{
    47  			name:          "object count is zero",
    48  			count:         0,
    49  			countExpected: 0,
    50  			errExpected:   nil,
    51  		},
    52  		{
    53  			name:          "object count is more than zero",
    54  			count:         799,
    55  			countExpected: 799,
    56  			errExpected:   nil,
    57  		},
    58  		{
    59  			name:          "object count stale",
    60  			count:         799,
    61  			countExpected: 799,
    62  			lastUpdated:   staleTolerationThreshold + time.Millisecond,
    63  			errExpected:   ObjectCountStaleErr,
    64  		},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		t.Run(test.name, func(t *testing.T) {
    69  			fakeClock := &testclock.FakePassiveClock{}
    70  			tracker := &objectCountTracker{
    71  				clock:  fakeClock,
    72  				counts: map[string]*timestampedCount{},
    73  			}
    74  
    75  			key := "foo.bar.resource"
    76  			now := time.Now()
    77  			fakeClock.SetTime(now.Add(-test.lastUpdated))
    78  			tracker.Set(key, test.count)
    79  
    80  			fakeClock.SetTime(now)
    81  			countGot, err := tracker.Get(key)
    82  			if test.errExpected != err {
    83  				t.Errorf("Expected error: %v, but got: %v", test.errExpected, err)
    84  			}
    85  			if test.countExpected != countGot {
    86  				t.Errorf("Expected count: %d, but got: %d", test.countExpected, countGot)
    87  			}
    88  			if test.count <= -1 && len(tracker.counts) > 0 {
    89  				t.Errorf("Expected the cache to be empty, but got: %d", len(tracker.counts))
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func TestStorageObjectCountTrackerWithPrune(t *testing.T) {
    96  	fakeClock := &testclock.FakePassiveClock{}
    97  	tracker := &objectCountTracker{
    98  		clock:  fakeClock,
    99  		counts: map[string]*timestampedCount{},
   100  	}
   101  
   102  	now := time.Now()
   103  	fakeClock.SetTime(now.Add(-61 * time.Minute))
   104  	tracker.Set("k1", 61)
   105  	fakeClock.SetTime(now.Add(-60 * time.Minute))
   106  	tracker.Set("k2", 60)
   107  	// we are going to prune keys that are stale for >= 1h
   108  	// so the above keys are expected to be pruned and the
   109  	// key below should not be pruned.
   110  	mostRecent := now.Add(-59 * time.Minute)
   111  	fakeClock.SetTime(mostRecent)
   112  	tracker.Set("k3", 59)
   113  	expected := map[string]*timestampedCount{
   114  		"k3": {
   115  			count:         59,
   116  			lastUpdatedAt: mostRecent,
   117  		},
   118  	}
   119  
   120  	fakeClock.SetTime(now)
   121  	if err := tracker.prune(time.Hour); err != nil {
   122  		t.Fatalf("Expected no error, but got: %v", err)
   123  	}
   124  
   125  	// we expect only one entry in the map, so DeepEqual should work.
   126  	if !reflect.DeepEqual(expected, tracker.counts) {
   127  		t.Errorf("Expected prune to remove stale entries - diff: %s", cmp.Diff(expected, tracker.counts))
   128  	}
   129  }