|
1 | 1 | package org.a2aproject.sdk.server; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertFalse; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue; |
7 | 8 |
|
8 | 9 | import java.util.Collections; |
9 | 10 | import java.util.List; |
10 | 11 | import java.util.Set; |
| 12 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
11 | 14 | import java.util.logging.Handler; |
12 | 15 | import java.util.logging.LogRecord; |
13 | 16 | import java.util.logging.Logger; |
@@ -229,6 +232,44 @@ void testSkipPropertiesFilterWarnings() { |
229 | 232 | } |
230 | 233 | } |
231 | 234 |
|
| 235 | + @Test |
| 236 | + void resolveAndValidateOnceRetriesAfterFailure() { |
| 237 | + AgentCard card = createTestAgentCardBuilder().build(); |
| 238 | + AtomicBoolean guard = new AtomicBoolean(false); |
| 239 | + AtomicInteger validationCount = new AtomicInteger(0); |
| 240 | + |
| 241 | + assertThrows(IllegalStateException.class, () -> |
| 242 | + AgentCardValidator.resolveAndValidateOnce( |
| 243 | + () -> card, guard, c -> { |
| 244 | + validationCount.incrementAndGet(); |
| 245 | + throw new IllegalStateException("transient failure"); |
| 246 | + })); |
| 247 | + |
| 248 | + assertFalse(guard.get(), "guard should be reset after failure"); |
| 249 | + assertEquals(1, validationCount.get()); |
| 250 | + |
| 251 | + AgentCard result = AgentCardValidator.resolveAndValidateOnce( |
| 252 | + () -> card, guard, c -> validationCount.incrementAndGet()); |
| 253 | + |
| 254 | + assertTrue(guard.get(), "guard should be set after success"); |
| 255 | + assertEquals(2, validationCount.get()); |
| 256 | + assertEquals(card, result); |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + void resolveAndValidateOnceSkipsValidationOnSubsequentCalls() { |
| 261 | + AgentCard card = createTestAgentCardBuilder().build(); |
| 262 | + AtomicBoolean guard = new AtomicBoolean(false); |
| 263 | + AtomicInteger validationCount = new AtomicInteger(0); |
| 264 | + |
| 265 | + AgentCardValidator.resolveAndValidateOnce( |
| 266 | + () -> card, guard, c -> validationCount.incrementAndGet()); |
| 267 | + AgentCardValidator.resolveAndValidateOnce( |
| 268 | + () -> card, guard, c -> validationCount.incrementAndGet()); |
| 269 | + |
| 270 | + assertEquals(1, validationCount.get(), "validation should run only once"); |
| 271 | + } |
| 272 | + |
232 | 273 | // A simple log handler for testing |
233 | 274 | private static class TestLogHandler extends Handler { |
234 | 275 | private final List<String> logMessages = new java.util.ArrayList<>(); |
|
0 commit comments