Debian Codegraph @

pkg/agent/clientset_test.go

raw
1 /*
2 Copyright 2024 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 agent
18
19 import (
20 "testing"
21 )
22
23 type FakeServerCounter struct {
24 count int
25 }
26
27 func (f *FakeServerCounter) Count() int {
28 return f.count
29 }
30
31 func TestAggregateServerCounter(t *testing.T) {
32 testCases := []struct {
33 name string
34 source string
35 leaseCounter ServerCounter
36 responseCounter ServerCounter
37 want int
38 }{
39 {
40 name: "max: higher from response",
41 source: "max",
42 leaseCounter: &FakeServerCounter{count: 24},
43 responseCounter: &FakeServerCounter{count: 42},
44 want: 42,
45 },
46 {
47 name: "max: higher from leases",
48 source: "max",
49 leaseCounter: &FakeServerCounter{count: 6},
50 responseCounter: &FakeServerCounter{count: 3},
51 want: 6,
52 },
53 {
54 name: "max: both zero",
55 source: "max",
56 leaseCounter: &FakeServerCounter{count: 0},
57 responseCounter: &FakeServerCounter{count: 0},
58 want: 1, // fallback
59 },
60 {
61 name: "default: lease counter is nil",
62 source: "default",
63 leaseCounter: nil,
64 responseCounter: &FakeServerCounter{count: 3},
65 want: 3,
66 },
67 {
68 name: "default: lease counter is present",
69 source: "default",
70 leaseCounter: &FakeServerCounter{count: 3},
71 responseCounter: &FakeServerCounter{count: 6},
72 want: 3, // lease count is preferred
73 },
74 {
75 name: "default: lease count is zero",
76 source: "default",
77 leaseCounter: &FakeServerCounter{count: 0},
78 responseCounter: &FakeServerCounter{count: 6},
79 want: 1, // fallback
80 },
81 }
82
83 for _, tc := range testCases {
84 t.Run(tc.name, func(t *testing.T) {
85 agg := NewAggregateServerCounter(tc.leaseCounter, tc.responseCounter, tc.source)
86 if got := agg.Count(); got != tc.want {
87 t.Errorf("agg.Count() = %v, want: %v", got, tc.want)
88 }
89 })
90 }
91 }
92
93 func TestResponseBasedCounter(t *testing.T) {
94 testCases := []struct {
95 name string
96 responseCount int
97 want int
98 }{
99 {
100 name: "non-zero count",
101 responseCount: 5,
102 want: 5,
103 },
104 {
105 name: "zero count",
106 responseCount: 0,
107 want: 1, // fallback
108 },
109 }
110
111 for _, tc := range testCases {
112 t.Run(tc.name, func(t *testing.T) {
113 cs := &ClientSet{lastReceivedServerCount: tc.responseCount}
114 rbc := NewResponseBasedCounter(cs)
115 if got := rbc.Count(); got != tc.want {
116 t.Errorf("rbc.Count() = %v, want: %v", got, tc.want)
117 }
118 })
119 }
120 }