Skip to content

Commit 1eec50b

Browse files
committed
wrapped ServerLeaseCounter into an interface and added a unit test for the ServerCount() method in clientset
1 parent db2cd97 commit 1eec50b

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

pkg/agent/clientset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type ClientSet struct {
3939
agentID string // ID of this agent
4040
address string // proxy server address. Assuming HA proxy server
4141

42-
leaseCounter *ServerLeaseCounter // counts number of proxy server leases
42+
leaseCounter ServerCounter // counts number of proxy server leases
4343
lastReceivedServerCount int // last server count received from a proxy server
4444
lastServerCount int // last server count value from either lease system or proxy server, former takes priority
4545

@@ -147,7 +147,7 @@ type ClientSetConfig struct {
147147
WarnOnChannelLimit bool
148148
SyncForever bool
149149
XfrChannelSize int
150-
ServerLeaseCounter *ServerLeaseCounter
150+
ServerLeaseCounter ServerCounter
151151
}
152152

153153
func (cc *ClientSetConfig) NewAgentClientSet(drainCh, stopCh <-chan struct{}) *ClientSet {

pkg/agent/clientset_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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 TestServerCount(t *testing.T) {
32+
testCases := []struct{
33+
name string
34+
responseCount int
35+
leaseCount int
36+
want int
37+
} {
38+
{
39+
name: "higher from response",
40+
responseCount: 42,
41+
leaseCount: 24,
42+
want: 42,
43+
},
44+
{
45+
name: "higher from leases",
46+
responseCount: 3,
47+
leaseCount: 6,
48+
want: 6,
49+
},
50+
{
51+
name: "both zero",
52+
responseCount: 0,
53+
leaseCount: 0,
54+
want: 1,
55+
},
56+
}
57+
58+
for _, tc := range testCases {
59+
t.Run(tc.name, func(t *testing.T) {
60+
lc := &FakeServerCounter{
61+
count: tc.leaseCount,
62+
}
63+
64+
cs := &ClientSet{
65+
clients: make(map[string]*Client),
66+
leaseCounter: lc,
67+
}
68+
cs.lastReceivedServerCount = tc.responseCount
69+
if got := cs.ServerCount(); got != tc.want {
70+
t.Errorf("cs.ServerCount() = %v, want: %v", got, tc.want)
71+
}
72+
})
73+
}
74+
75+
}

pkg/agent/lease_counter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ import (
3636
coordinationv1lister "k8s.io/client-go/listers/coordination/v1"
3737
)
3838

39+
type ServerCounter interface {
40+
Count() int
41+
}
42+
3943
// A ServerLeaseCounter counts leases in the k8s apiserver to determine the
4044
// current proxy server count.
4145
type ServerLeaseCounter struct {

0 commit comments

Comments
 (0)