|
1 | 1 | package io.a2a.server.events; |
2 | 2 |
|
3 | | -import io.a2a.server.tasks.InMemoryTaskStore; |
4 | | -import io.a2a.server.tasks.PushNotificationSender; |
5 | | -import io.a2a.server.tasks.TaskStateProvider; |
6 | 3 | import java.util.concurrent.atomic.AtomicInteger; |
7 | 4 |
|
8 | 5 | public class EventQueueUtil { |
9 | 6 | // Shared MainEventBus for all tests (to avoid creating one per test) |
| 7 | + // Each test creates its own MainEventBusProcessor in @BeforeEach/@AfterEach for proper isolation |
10 | 8 | private static final MainEventBus TEST_EVENT_BUS = new MainEventBus(); |
11 | 9 |
|
12 | | - // Shared MainEventBusProcessor for all tests (automatically processes events) |
13 | | - private static final MainEventBusProcessor TEST_PROCESSOR; |
14 | | - |
15 | | - static { |
16 | | - // Initialize and start the processor once for all tests |
17 | | - InMemoryTaskStore testTaskStore = new InMemoryTaskStore(); |
18 | | - PushNotificationSender testPushSender = taskId -> {}; // No-op for tests |
19 | | - TEST_PROCESSOR = new MainEventBusProcessor(TEST_EVENT_BUS, testTaskStore, testPushSender); |
20 | | - TEST_PROCESSOR.start(); // Start background thread |
21 | | - |
22 | | - // Register shutdown hook to stop processor |
23 | | - Runtime.getRuntime().addShutdownHook(new Thread(() -> TEST_PROCESSOR.stop())); |
24 | | - } |
25 | | - |
26 | 10 | // Counter for generating unique test taskIds |
27 | 11 | private static final AtomicInteger TASK_ID_COUNTER = new AtomicInteger(0); |
28 | 12 |
|
29 | | - // Since EventQueue.builder() is package protected, add a method to expose it |
30 | | - // Note: Now includes MainEventBus requirement and default taskId |
31 | | - // Returns MainQueue - tests should call .tap() if they need to consume events |
| 13 | + /** |
| 14 | + * Get an EventQueue builder pre-configured with the shared test MainEventBus and a unique taskId. |
| 15 | + * <p> |
| 16 | + * Note: Returns MainQueue - tests should call .tap() if they need to consume events. |
| 17 | + * </p> |
| 18 | + * |
| 19 | + * @return builder with TEST_EVENT_BUS and unique taskId already set |
| 20 | + */ |
32 | 21 | public static EventQueue.EventQueueBuilder getEventQueueBuilder() { |
33 | | - return new EventQueueBuilderWrapper( |
34 | | - EventQueue.builder(TEST_EVENT_BUS) |
35 | | - .taskId("test-task-" + TASK_ID_COUNTER.incrementAndGet()) |
36 | | - ); |
37 | | - } |
38 | | - |
39 | | - // Get the shared test MainEventBus instance |
40 | | - public static MainEventBus getTestEventBus() { |
41 | | - return TEST_EVENT_BUS; |
| 22 | + return EventQueue.builder(TEST_EVENT_BUS) |
| 23 | + .taskId("test-task-" + TASK_ID_COUNTER.incrementAndGet()); |
42 | 24 | } |
43 | 25 |
|
| 26 | + /** |
| 27 | + * Start a MainEventBusProcessor instance. |
| 28 | + * |
| 29 | + * @param processor the processor to start |
| 30 | + */ |
44 | 31 | public static void start(MainEventBusProcessor processor) { |
45 | 32 | processor.start(); |
46 | 33 | } |
47 | 34 |
|
| 35 | + /** |
| 36 | + * Stop a MainEventBusProcessor instance. |
| 37 | + * |
| 38 | + * @param processor the processor to stop |
| 39 | + */ |
48 | 40 | public static void stop(MainEventBusProcessor processor) { |
49 | 41 | processor.stop(); |
50 | 42 | } |
51 | | - |
52 | | - // Wrapper that delegates to actual builder |
53 | | - private static class EventQueueBuilderWrapper extends EventQueue.EventQueueBuilder { |
54 | | - private final EventQueue.EventQueueBuilder delegate; |
55 | | - |
56 | | - EventQueueBuilderWrapper(EventQueue.EventQueueBuilder delegate) { |
57 | | - this.delegate = delegate; |
58 | | - } |
59 | | - |
60 | | - @Override |
61 | | - public EventQueue.EventQueueBuilder queueSize(int queueSize) { |
62 | | - delegate.queueSize(queueSize); |
63 | | - return this; |
64 | | - } |
65 | | - |
66 | | - @Override |
67 | | - public EventQueue.EventQueueBuilder hook(EventEnqueueHook hook) { |
68 | | - delegate.hook(hook); |
69 | | - return this; |
70 | | - } |
71 | | - |
72 | | - @Override |
73 | | - public EventQueue.EventQueueBuilder taskId(String taskId) { |
74 | | - delegate.taskId(taskId); |
75 | | - return this; |
76 | | - } |
77 | | - |
78 | | - @Override |
79 | | - public EventQueue.EventQueueBuilder addOnCloseCallback(Runnable onCloseCallback) { |
80 | | - delegate.addOnCloseCallback(onCloseCallback); |
81 | | - return this; |
82 | | - } |
83 | | - |
84 | | - @Override |
85 | | - public EventQueue.EventQueueBuilder taskStateProvider(TaskStateProvider taskStateProvider) { |
86 | | - delegate.taskStateProvider(taskStateProvider); |
87 | | - return this; |
88 | | - } |
89 | | - |
90 | | - @Override |
91 | | - public EventQueue.EventQueueBuilder mainEventBus(MainEventBus mainEventBus) { |
92 | | - delegate.mainEventBus(mainEventBus); |
93 | | - return this; |
94 | | - } |
95 | | - |
96 | | - @Override |
97 | | - public EventQueue build() { |
98 | | - // Return MainQueue directly - tests should call .tap() if they need ChildQueue |
99 | | - return delegate.build(); |
100 | | - } |
101 | | - } |
102 | 43 | } |
0 commit comments