|
26 | 26 | import static org.junit.Assert.assertThrows; |
27 | 27 |
|
28 | 28 | import com.google.adk.agents.Callbacks.BeforeAgentCallback; |
| 29 | +import com.google.adk.apps.ResumabilityConfig; |
29 | 30 | import com.google.adk.events.Event; |
30 | 31 | import com.google.adk.testing.TestBaseAgent; |
31 | 32 | import com.google.common.collect.ImmutableList; |
@@ -381,4 +382,63 @@ public void runAsync_resumable_pausesOnLongRunningCall_doesNotEmitEndOfAgent() { |
381 | 382 | assertThat(events.stream().anyMatch(event -> event.actions().endOfAgent())).isFalse(); |
382 | 383 | assertThat(events.stream().filter(event -> event.author().equals("sub")).count()).isEqualTo(1L); |
383 | 384 | } |
| 385 | + |
| 386 | + @Test |
| 387 | + public void runAsync_nonPositiveMaxIterations_notResumable_runsNoIterations() { |
| 388 | + for (int maxIterations : new int[] {0, -1}) { |
| 389 | + LoopAgent loopAgent = createLoopAgent(maxIterations); |
| 390 | + |
| 391 | + List<Event> events = |
| 392 | + loopAgent.runAsync(createInvocationContext(loopAgent)).toList().blockingGet(); |
| 393 | + |
| 394 | + assertThat(events).isEmpty(); |
| 395 | + } |
| 396 | + } |
| 397 | + |
| 398 | + @Test |
| 399 | + public void runAsync_nonPositiveMaxIterations_legacyResumption_runsNoIterations() { |
| 400 | + for (int maxIterations : new int[] {0, -1}) { |
| 401 | + LoopAgent loopAgent = createLoopAgent(maxIterations); |
| 402 | + |
| 403 | + List<Event> events = |
| 404 | + loopAgent |
| 405 | + .runAsync(createLegacyResumableInvocationContext(loopAgent)) |
| 406 | + .toList() |
| 407 | + .blockingGet(); |
| 408 | + |
| 409 | + assertThat(events).isEmpty(); |
| 410 | + } |
| 411 | + } |
| 412 | + |
| 413 | + @Test |
| 414 | + public void runAsync_nonPositiveMaxIterations_resumable_onlyEmitsEndOfAgent() { |
| 415 | + for (int maxIterations : new int[] {0, -1}) { |
| 416 | + LoopAgent loopAgent = createLoopAgent(maxIterations); |
| 417 | + |
| 418 | + List<Event> events = |
| 419 | + loopAgent.runAsync(createResumableInvocationContext(loopAgent)).toList().blockingGet(); |
| 420 | + |
| 421 | + assertThat(events).hasSize(1); |
| 422 | + assertThat(events.get(0).author()).isEqualTo("loop"); |
| 423 | + assertThat(events.get(0).actions().endOfAgent()).isTrue(); |
| 424 | + } |
| 425 | + } |
| 426 | + |
| 427 | + private static LoopAgent createLoopAgent(int maxIterations) { |
| 428 | + TestBaseAgent subAgent = |
| 429 | + createSubAgent("sub", createEvent("e").toBuilder().author("sub").build()); |
| 430 | + return LoopAgent.builder() |
| 431 | + .name("loop") |
| 432 | + .subAgents(subAgent) |
| 433 | + .maxIterations(maxIterations) |
| 434 | + .build(); |
| 435 | + } |
| 436 | + |
| 437 | + @SuppressWarnings("deprecation") // The legacy resumption flow is deprecated by design. |
| 438 | + private static InvocationContext createLegacyResumableInvocationContext(LoopAgent loopAgent) { |
| 439 | + return createInvocationContext(loopAgent).toBuilder() |
| 440 | + .resumabilityConfig( |
| 441 | + ResumabilityConfig.builder().plainTextContinuationAutoResume(true).build()) |
| 442 | + .build(); |
| 443 | + } |
384 | 444 | } |
0 commit comments