|
23 | 23 | import static org.mockito.Mockito.mock; |
24 | 24 |
|
25 | 25 | import com.xxl.job.core.executor.XxlJobExecutor; |
| 26 | +import com.xxl.job.core.handler.IJobHandler; |
| 27 | +import io.agentscope.core.message.ContentBlock; |
| 28 | +import io.agentscope.core.message.Msg; |
| 29 | +import io.agentscope.core.message.TextBlock; |
| 30 | +import io.agentscope.core.model.ChatResponse; |
| 31 | +import io.agentscope.core.model.GenerateOptions; |
| 32 | +import io.agentscope.core.model.Model; |
| 33 | +import io.agentscope.core.model.ToolSchema; |
| 34 | +import io.agentscope.extensions.scheduler.BaseScheduleAgentTask; |
26 | 35 | import io.agentscope.extensions.scheduler.ScheduleAgentTask; |
27 | 36 | import io.agentscope.extensions.scheduler.config.AgentConfig; |
28 | 37 | import io.agentscope.extensions.scheduler.config.DashScopeModelConfig; |
| 38 | +import io.agentscope.extensions.scheduler.config.ModelConfig; |
29 | 39 | import io.agentscope.extensions.scheduler.config.RuntimeAgentConfig; |
30 | 40 | import io.agentscope.extensions.scheduler.config.ScheduleConfig; |
31 | 41 | import java.util.List; |
|
39 | 49 | import org.junit.jupiter.api.Timeout; |
40 | 50 | import org.mockito.MockedStatic; |
41 | 51 | import org.mockito.Mockito; |
| 52 | +import reactor.core.publisher.Flux; |
42 | 53 |
|
43 | 54 | /** Unit tests for {@link XxlJobAgentScheduler}. */ |
44 | 55 | @Timeout(60) |
@@ -636,4 +647,55 @@ void testScheduleWithRegistryJobHandlerException() { |
636 | 647 | assertEquals(0, scheduler.getAllScheduleAgentTasks().size()); |
637 | 648 | } |
638 | 649 | } |
| 650 | + |
| 651 | + @Test |
| 652 | + void testJobTriggerCountsExactlyOneExecution() throws Exception { |
| 653 | + RuntimeAgentConfig agentConfig = |
| 654 | + RuntimeAgentConfig.builder() |
| 655 | + .name("CountingAgent") |
| 656 | + .modelConfig(new FixedResponseModelConfig()) |
| 657 | + .sysPrompt("Test prompt") |
| 658 | + .build(); |
| 659 | + |
| 660 | + BaseScheduleAgentTask task = (BaseScheduleAgentTask) scheduler.schedule(agentConfig); |
| 661 | + |
| 662 | + IJobHandler handler = XxlJobExecutor.loadJobHandler("CountingAgent"); |
| 663 | + assertNotNull(handler, "schedule() should register the job handler with XXL-Job"); |
| 664 | + |
| 665 | + handler.execute(); |
| 666 | + |
| 667 | + // BaseScheduleAgentTask.run(...) already counts the execution, so a single trigger |
| 668 | + // must leave the counter at 1 -- the same invariant the Quartz job relies on. |
| 669 | + assertEquals(1L, task.getExecutionCount()); |
| 670 | + } |
| 671 | + |
| 672 | + /** |
| 673 | + * A {@link ModelConfig} whose model answers with a canned text block, so that a scheduled |
| 674 | + * task can complete without reaching a real provider. |
| 675 | + */ |
| 676 | + private static final class FixedResponseModelConfig implements ModelConfig { |
| 677 | + |
| 678 | + @Override |
| 679 | + public String getModelName() { |
| 680 | + return "test-model"; |
| 681 | + } |
| 682 | + |
| 683 | + @Override |
| 684 | + public Model createModel() { |
| 685 | + return new Model() { |
| 686 | + @Override |
| 687 | + public Flux<ChatResponse> stream( |
| 688 | + List<Msg> messages, List<ToolSchema> tools, GenerateOptions options) { |
| 689 | + ContentBlock block = TextBlock.builder().text("ok").build(); |
| 690 | + return Flux.just( |
| 691 | + ChatResponse.builder().id("r1").content(List.of(block)).build()); |
| 692 | + } |
| 693 | + |
| 694 | + @Override |
| 695 | + public String getModelName() { |
| 696 | + return "test-model"; |
| 697 | + } |
| 698 | + }; |
| 699 | + } |
| 700 | + } |
639 | 701 | } |
0 commit comments