Skip to content

Commit

Permalink
fix bug in delete empty memoty (#1964)
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Zhang <[email protected]>
(cherry picked from commit 45e5199)
  • Loading branch information
Zhangxunmt authored and github-actions[bot] committed Jan 31, 2024
1 parent 1218e96 commit 3ee228a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,10 @@ public void deleteConversation(String conversationId, ActionListener<Boolean> li
try (ThreadContext.StoredContext threadContext = client.threadPool().getThreadContext().stashContext()) {
ActionListener<Boolean> internalListener = ActionListener.runBefore(listener, () -> threadContext.restore());
ActionListener<List<Interaction>> searchListener = ActionListener.wrap(interactions -> {
if (interactions.size() == 0) {
internalListener.onResponse(true);
return;
}
BulkRequest request = Requests.bulkRequest();
for (Interaction interaction : interactions) {
DeleteRequest delRequest = Requests.deleteRequest(INTERACTIONS_INDEX_NAME).id(interaction.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ public class InteractionsIndexTests extends OpenSearchTestCase {

InteractionsIndex interactionsIndex;

Interaction interaction;

@Before
public void setup() {
this.client = mock(Client.class);
Expand All @@ -123,6 +125,16 @@ public void setup() {
doReturn(indicesAdminClient).when(adminClient).indices();
doReturn(threadPool).when(client).threadPool();
doReturn(new ThreadContext(Settings.EMPTY)).when(threadPool).getThreadContext();
interaction = new Interaction(
"iid",
Instant.ofEpochMilli(1234),
"cid",
"inp",
"pt",
"rsp",
"ogn",
Collections.singletonMap("metadata", "some meta")
);
this.interactionsIndex = spy(new InteractionsIndex(client, clusterService, conversationMetaIndex));
}

Expand Down Expand Up @@ -573,7 +585,7 @@ public void testDelete_BulkHasFailures_ReturnFalse() {
}).when(client).bulk(any(), any());
doAnswer(invocation -> {
ActionListener<List<Interaction>> al = invocation.getArgument(2);
al.onResponse(List.of());
al.onResponse(List.of(interaction));
return null;
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
@SuppressWarnings("unchecked")
Expand All @@ -594,7 +606,7 @@ public void testDelete_BulkFails_ThenFail() {
}).when(client).bulk(any(), any());
doAnswer(invocation -> {
ActionListener<List<Interaction>> al = invocation.getArgument(2);
al.onResponse(List.of());
al.onResponse(List.of(interaction));
return null;
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
@SuppressWarnings("unchecked")
Expand All @@ -621,6 +633,22 @@ public void testDelete_SearchFails_ThenFail() {
assert (argCaptor.getValue().getMessage().equals("Failure during GetAllInteractions"));
}

public void testDelete_SearchReturnEmpty_ThenPass() {
doReturn(true).when(metadata).hasIndex(anyString());
setupGrantAccess();
doAnswer(invocation -> {
ActionListener<List<Interaction>> al = invocation.getArgument(2);
al.onResponse(List.of());
return null;
}).when(interactionsIndex).getAllInteractions(anyString(), anyInt(), any());
@SuppressWarnings("unchecked")
ActionListener<Boolean> deleteConversationListener = mock(ActionListener.class);
interactionsIndex.deleteConversation("cid", deleteConversationListener);
ArgumentCaptor<Boolean> argCaptor = ArgumentCaptor.forClass(Boolean.class);
verify(deleteConversationListener, times(1)).onResponse(argCaptor.capture());
assert (argCaptor.getValue());
}

public void testDelete_NoAccessNoUser_ThenFail() {
doReturn(true).when(metadata).hasIndex(anyString());
setupDenyAccess(null);
Expand Down

0 comments on commit 3ee228a

Please sign in to comment.