Skip to content

Commit f44b445

Browse files
test: add more tests
1 parent 11bc79a commit f44b445

File tree

2 files changed

+109
-10
lines changed

2 files changed

+109
-10
lines changed

pkg/smartrequeue/store.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ func (s *Store) deleteEntry(toDelete *Entry) {
5757
}
5858
}
5959

60-
// cap limits the next duration to the maximum interval.
61-
func (s *Store) cap(next time.Duration) time.Duration {
62-
if next > s.maxInterval {
63-
return s.maxInterval
64-
}
65-
return next
66-
}
67-
6860
func keyFromObject(obj client.Object) key {
6961
return key{
7062
Kind: reflect.TypeOf(obj).Elem().Name(),
@@ -122,7 +114,15 @@ func (e *Entry) Never() (ctrl.Result, error) {
122114
return ctrl.Result{}, nil
123115
}
124116

117+
// setNext updates the next requeue duration using exponential backoff.
118+
// It multiplies the current duration by the store's multiplier and ensures
119+
// the result doesn't exceed the configured maximum interval.
125120
func (e *Entry) setNext() {
126-
e.nextDuration = time.Duration(float32(e.nextDuration) * e.store.multiplier)
127-
e.nextDuration = e.store.cap(e.nextDuration)
121+
newDuration := time.Duration(float32(e.nextDuration) * e.store.multiplier)
122+
123+
if newDuration > e.store.maxInterval {
124+
newDuration = e.store.maxInterval
125+
}
126+
127+
e.nextDuration = newDuration
128128
}

pkg/smartrequeue/store_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1"
78
"github.com/stretchr/testify/assert"
89
ctrl "sigs.k8s.io/controller-runtime"
10+
"sigs.k8s.io/controller-runtime/pkg/client"
911
)
1012

1113
func Test_Entry(t *testing.T) {
@@ -34,3 +36,100 @@ func Test_Entry(t *testing.T) {
3436
func getRequeueAfter(res ctrl.Result, _ error) time.Duration {
3537
return res.RequeueAfter.Round(time.Second)
3638
}
39+
40+
func TestFor(t *testing.T) {
41+
tests := []struct {
42+
name string
43+
firstObj client.Object
44+
secondObj client.Object
45+
expectSame bool
46+
description string
47+
}{
48+
{
49+
name: "same object returns same entry",
50+
firstObj: &v1alpha1.Cluster{
51+
ObjectMeta: ctrl.ObjectMeta{
52+
Name: "test",
53+
Namespace: "test",
54+
},
55+
},
56+
secondObj: &v1alpha1.Cluster{
57+
ObjectMeta: ctrl.ObjectMeta{
58+
Name: "test",
59+
Namespace: "test",
60+
},
61+
},
62+
expectSame: true,
63+
description: "Expected to get the same entry back",
64+
},
65+
{
66+
name: "different namespace returns different entry",
67+
firstObj: &v1alpha1.Cluster{
68+
ObjectMeta: ctrl.ObjectMeta{
69+
Name: "test",
70+
Namespace: "test",
71+
},
72+
},
73+
secondObj: &v1alpha1.Cluster{
74+
ObjectMeta: ctrl.ObjectMeta{
75+
Name: "test",
76+
Namespace: "test2",
77+
},
78+
},
79+
expectSame: false,
80+
description: "Expected to get a different entry back",
81+
},
82+
{
83+
name: "different name returns different entry",
84+
firstObj: &v1alpha1.Cluster{
85+
ObjectMeta: ctrl.ObjectMeta{
86+
Name: "test",
87+
Namespace: "test",
88+
},
89+
},
90+
secondObj: &v1alpha1.Cluster{
91+
ObjectMeta: ctrl.ObjectMeta{
92+
Name: "test2",
93+
Namespace: "test",
94+
},
95+
},
96+
expectSame: false,
97+
description: "Expected to get a different entry back",
98+
},
99+
{
100+
name: "different kind returns different entry",
101+
firstObj: &v1alpha1.Cluster{
102+
ObjectMeta: ctrl.ObjectMeta{
103+
Name: "test",
104+
Namespace: "test",
105+
},
106+
},
107+
secondObj: &v1alpha1.AccessRequest{
108+
ObjectMeta: ctrl.ObjectMeta{
109+
Name: "test",
110+
Namespace: "test",
111+
},
112+
},
113+
expectSame: false,
114+
description: "Expected to get a different entry back",
115+
},
116+
}
117+
118+
for _, tt := range tests {
119+
t.Run(tt.name, func(t *testing.T) {
120+
store := NewStore(time.Second, time.Minute, 2)
121+
entry1 := store.For(tt.firstObj)
122+
123+
assert.NotNil(t, entry1, "Expected entry to be created")
124+
assert.Equal(t, 1*time.Second, getRequeueAfter(entry1.Stable()))
125+
126+
entry2 := store.For(tt.secondObj)
127+
128+
if tt.expectSame {
129+
assert.Equal(t, entry1, entry2, tt.description)
130+
} else {
131+
assert.NotEqual(t, entry1, entry2, tt.description)
132+
}
133+
})
134+
}
135+
}

0 commit comments

Comments
 (0)