Commit 58e1b4b
authored
fix(generate_response): Fix flaky concurrent tests for generate_response (#1011)
## AgentScope-Java Version
[The version of AgentScope-Java you are working on, e.g. 1.0.9, check
your pom.xml dependency version or run `mvn dependency:tree | grep
agentscope-parent:pom`(only mac/linux)]
## Description
### **Background**
The unit test code `testConcurrencyConflictStructuredOutput ` contains
two `agent.call` actions.
The first thread's `agent.call` writes `generate_response` into the
*Toolkit* and then clears it in the `doFinally` block.
The second thread's `agent.call` also writes `generate_response` into
the *Toolkit* and then clears it in the `doFinally` block.
### **Problem**
The issue occurs when the first thread executes `doFinally` later than
the second thread writes `generate_response`. In this case, the first
thread will clear the content written by the second thread into the
*Toolkit*, causing the second thread to fail to retrieve
`generate_response`.
### **Objective**
Avoid "late cleanup" mistakenly deleting a tool with the same name that
has already been overwritten by another call.
### **Implementation Approach**
Use Java's `ConcurrentHashMap.remove(Object key, Object value)` to
ensure only the tool written by the current thread is removed, while
preventing race conditions.
### **How to Verify**
Add a new test method:
`io.agentscope.core.agent.ReActAgentStructuredOutputTest#testConcurrencyConflictStructuredOutput_repeated`.
Test the same method 25,000 times. If no exceptions occur, it can be
considered basically problem-free.
## Checklist
Please check the following items before code is ready to be reviewed.
- [x] Code has been formatted with `mvn spotless:apply`
- [x] All tests are passing (`mvn test`)
- [x] Javadoc comments are complete and follow project conventions
- [x] Related documentation has been updated (e.g. links, examples,
etc.)
- [x] Code is ready for review1 parent 7d898be commit 58e1b4b
File tree
5 files changed
+143
-4
lines changed- agentscope-core/src
- main/java/io/agentscope/core
- agent
- tool
- test/java/io/agentscope/core
- agent
- tool
5 files changed
+143
-4
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
193 | 192 | | |
194 | | - | |
| 193 | + | |
| 194 | + | |
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
114 | 114 | | |
115 | 115 | | |
116 | 116 | | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
117 | 133 | | |
118 | 134 | | |
119 | 135 | | |
| |||
Lines changed: 15 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
601 | 601 | | |
602 | 602 | | |
603 | 603 | | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
604 | 619 | | |
605 | 620 | | |
606 | 621 | | |
| |||
Lines changed: 26 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
| 43 | + | |
41 | 44 | | |
42 | 45 | | |
43 | 46 | | |
| |||
536 | 539 | | |
537 | 540 | | |
538 | 541 | | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
539 | 563 | | |
540 | 564 | | |
541 | 565 | | |
| |||
587 | 611 | | |
588 | 612 | | |
589 | 613 | | |
590 | | - | |
| 614 | + | |
591 | 615 | | |
592 | 616 | | |
593 | 617 | | |
| |||
604 | 628 | | |
605 | 629 | | |
606 | 630 | | |
607 | | - | |
| 631 | + | |
608 | 632 | | |
609 | 633 | | |
610 | 634 | | |
| |||
Lines changed: 84 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
241 | 242 | | |
242 | 243 | | |
243 | 244 | | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
244 | 328 | | |
245 | 329 | | |
246 | 330 | | |
| |||
0 commit comments