|
1 | 1 | package smartrequeue |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
4 | 6 | "testing" |
5 | 7 | "time" |
6 | 8 |
|
7 | 9 | "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1" |
8 | 10 | "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
9 | 12 | ctrl "sigs.k8s.io/controller-runtime" |
10 | 13 | "sigs.k8s.io/controller-runtime/pkg/client" |
11 | 14 | ) |
@@ -94,15 +97,89 @@ func TestFor(t *testing.T) { |
94 | 97 | entry1 := store.For(tt.firstObj) |
95 | 98 |
|
96 | 99 | assert.NotNil(t, entry1, "Expected entry to be created") |
97 | | - assert.Equal(t, 1*time.Second, getRequeueAfter(entry1.Stable())) |
| 100 | + result, err := entry1.Stable() |
| 101 | + require.NoError(t, err) |
| 102 | + assert.Equal(t, 1*time.Second, getRequeueAfter(result, err)) |
98 | 103 |
|
99 | 104 | entry2 := store.For(tt.secondObj) |
100 | 105 |
|
101 | 106 | if tt.expectSame { |
102 | | - assert.Equal(t, entry1, entry2, tt.description) |
| 107 | + assert.Same(t, entry1, entry2, tt.description) |
103 | 108 | } else { |
104 | | - assert.NotEqual(t, entry1, entry2, tt.description) |
| 109 | + assert.NotSame(t, entry1, entry2, tt.description) |
105 | 110 | } |
106 | 111 | }) |
107 | 112 | } |
108 | 113 | } |
| 114 | + |
| 115 | +// TestClear ensures the Clear method removes all entries |
| 116 | +func TestClear(t *testing.T) { |
| 117 | + store := NewStore(time.Second, time.Minute, 2) |
| 118 | + |
| 119 | + // Add some entries |
| 120 | + obj1 := &v1alpha1.Cluster{ |
| 121 | + ObjectMeta: ctrl.ObjectMeta{ |
| 122 | + Name: "test1", |
| 123 | + Namespace: "test", |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + obj2 := &v1alpha1.Cluster{ |
| 128 | + ObjectMeta: ctrl.ObjectMeta{ |
| 129 | + Name: "test2", |
| 130 | + Namespace: "test", |
| 131 | + }, |
| 132 | + } |
| 133 | + |
| 134 | + // Get entries to populate the store |
| 135 | + entry1 := store.For(obj1) |
| 136 | + entry2 := store.For(obj2) |
| 137 | + |
| 138 | + // Verify entries exist |
| 139 | + assert.NotNil(t, entry1) |
| 140 | + assert.NotNil(t, entry2) |
| 141 | + |
| 142 | + // Clear the store |
| 143 | + store.Clear() |
| 144 | + |
| 145 | + // Get entries again - they should be new instances |
| 146 | + entry1After := store.For(obj1) |
| 147 | + entry2After := store.For(obj2) |
| 148 | + |
| 149 | + // Verify they're different instances |
| 150 | + assert.NotSame(t, entry1, entry1After) |
| 151 | + assert.NotSame(t, entry2, entry2After) |
| 152 | +} |
| 153 | + |
| 154 | +// TestConcurrentAccess tests that the store handles concurrent access properly |
| 155 | +func TestConcurrentAccess(t *testing.T) { |
| 156 | + store := NewStore(time.Second, time.Minute, 2) |
| 157 | + |
| 158 | + // Create a series of objects |
| 159 | + const numObjects = 100 |
| 160 | + objects := make([]client.Object, numObjects) |
| 161 | + |
| 162 | + for i := 0; i < numObjects; i++ { |
| 163 | + objects[i] = &v1alpha1.Cluster{ |
| 164 | + ObjectMeta: ctrl.ObjectMeta{ |
| 165 | + Name: fmt.Sprintf("test-%d", i), |
| 166 | + Namespace: "test", |
| 167 | + }, |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // Access concurrently |
| 172 | + var wg sync.WaitGroup |
| 173 | + wg.Add(numObjects) |
| 174 | + |
| 175 | + for i := 0; i < numObjects; i++ { |
| 176 | + go func(idx int) { |
| 177 | + defer wg.Done() |
| 178 | + obj := objects[idx] |
| 179 | + entry := store.For(obj) |
| 180 | + _, _ = entry.Stable() // Just exercise the API |
| 181 | + }(i) |
| 182 | + } |
| 183 | + |
| 184 | + wg.Wait() |
| 185 | +} |
0 commit comments