fix(RoutingLLMService): 修复 commit 后流式事件乱序问题#9
Open
ASGPIPO wants to merge 1 commit intonageoffer:mainfrom
Open
Conversation
Collaborator
|
请提供个复现问题的测试用例,不然不好评估修改是否合理 |
Author
class RoutingLLMServiceTests {
@Test
void bufferedEventsShouldDrainBeforeEventsArrivingDuringCommit() throws Exception {
List<String> forwarded = Collections.synchronizedList(new ArrayList<>());
CountDownLatch firstBufferedEventDispatched = new CountDownLatch(1);
CountDownLatch allowDrainToContinue = new CountDownLatch(1);
AtomicReference<Throwable> callbackFailure = new AtomicReference<>();
AtomicReference<Throwable> commitFailure = new AtomicReference<>();
StreamCallback downstream = new StreamCallback() {
@Override
public void onContent(String content) {
forwarded.add(content);
if ("A".equals(content)) {
firstBufferedEventDispatched.countDown();
try {
allowDrainToContinue.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
callbackFailure.set(e);
}
}
}
@Override
public void onComplete() {
}
@Override
public void onError(Throwable error) {
callbackFailure.set(error);
}
};
FirstPacketAwaiter awaiter = new FirstPacketAwaiter();
StreamCallback probeCallback = newProbeBufferingCallback(downstream, awaiter);
Method commitMethod = probeCallback.getClass().getDeclaredMethod("commit");
commitMethod.setAccessible(true);
probeCallback.onContent("A");
probeCallback.onContent("B");
Thread commitThread = new Thread(() -> {
try {
commitMethod.invoke(probeCallback);
} catch (Throwable t) {
commitFailure.set(t);
}
}, "commit-thread");
commitThread.start();
assertTrue(firstBufferedEventDispatched.await(1, TimeUnit.SECONDS));
probeCallback.onContent("C");
allowDrainToContinue.countDown();
commitThread.join(1000);
assertFalse(commitThread.isAlive());
assertNull(callbackFailure.get());
assertNull(commitFailure.get());
assertEquals(List.of("A", "B", "C"), forwarded,
"buffered events should be fully replayed before new events are dispatched");
}
private static StreamCallback newProbeBufferingCallback(StreamCallback downstream,
FirstPacketAwaiter awaiter) throws Exception {
Class<?> callbackClass = Class.forName(
"com.nageoffer.ai.ragent.infra.chat.RoutingLLMService$ProbeBufferingCallback");
Constructor<?> constructor = callbackClass.getDeclaredConstructor(StreamCallback.class, FirstPacketAwaiter.class);
constructor.setAccessible(true);
return (StreamCallback) constructor.newInstance(downstream, awaiter);
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
drain机制统一分发缓存事件commit后不再使用快照回放commit后到达的事件继续入队,保证按顺序分发