(t *testing.T)
| 158 | } |
| 159 | |
| 160 | func TestNewPolicyCache(t *testing.T) { |
| 161 | tests := []struct { |
| 162 | name string |
| 163 | setupContext func() context.Context |
| 164 | expectNewCache bool |
| 165 | }{ |
| 166 | { |
| 167 | name: "create new cache when none exists in context", |
| 168 | setupContext: func() context.Context { |
| 169 | return context.Background() |
| 170 | }, |
| 171 | expectNewCache: true, |
| 172 | }, |
| 173 | { |
| 174 | name: "return existing cache from context", |
| 175 | setupContext: func() context.Context { |
| 176 | ctx := context.Background() |
| 177 | cache := NewPolicyCache(ctx) |
| 178 | return WithPolicyCache(ctx, cache) |
| 179 | }, |
| 180 | expectNewCache: false, |
| 181 | }, |
| 182 | { |
| 183 | name: "handle nil cache in context", |
| 184 | setupContext: func() context.Context { |
| 185 | ctx := context.Background() |
| 186 | return context.WithValue(ctx, policyCacheKey, (*PolicyCache)(nil)) |
| 187 | }, |
| 188 | expectNewCache: true, |
| 189 | }, |
| 190 | } |
| 191 | |
| 192 | for _, tt := range tests { |
| 193 | t.Run(tt.name, func(t *testing.T) { |
| 194 | ctx := tt.setupContext() |
| 195 | |
| 196 | // Execute test |
| 197 | cache := NewPolicyCache(ctx) |
| 198 | |
| 199 | // Assert results |
| 200 | assert.NotNil(t, cache) |
| 201 | |
| 202 | if tt.expectNewCache { |
| 203 | // Test that we get a new cache instance |
| 204 | // by calling NewPolicyCache again and comparing |
| 205 | secondCache := NewPolicyCache(ctx) |
| 206 | assert.Equal(t, cache, secondCache, "Should return the same cache instance") |
| 207 | } |
| 208 | }) |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | func TestPolicyCacheFromContext(t *testing.T) { |
| 213 | tests := []struct { |
nothing calls this directly
no test coverage detected