1919import java .io .IOException ;
2020import java .util .List ;
2121import java .util .UUID ;
22+ import java .util .concurrent .TimeUnit ;
2223
2324import co .elastic .clients .elasticsearch .ElasticsearchClient ;
2425import co .elastic .clients .json .jackson .JacksonJsonpMapper ;
@@ -91,6 +92,7 @@ void add_shouldInsertSingleMessage(String content, MessageType messageType) {
9192 };
9293
9394 chatMemory .saveAll (conversationId , List .of (message ));
95+ sleepForSearchable ();
9496 assertThat (chatMemory .findConversationIds ()).isNotEmpty ();
9597 assertThat (chatMemory .findConversationIds ()).contains (conversationId );
9698
@@ -101,6 +103,10 @@ void add_shouldInsertSingleMessage(String content, MessageType messageType) {
101103 });
102104 }
103105
106+ private static void sleepForSearchable () throws InterruptedException {
107+ TimeUnit .SECONDS .sleep (2 );
108+ }
109+
104110 @ Test
105111 void shouldSaveAndRetrieveMultipleMessages () {
106112 this .contextRunner .run (context -> {
@@ -114,6 +120,8 @@ void shouldSaveAndRetrieveMultipleMessages() {
114120
115121 chatMemory .saveAll (conversationId , messages );
116122
123+ sleepForSearchable ();
124+
117125 List <Message > retrievedMessages = chatMemory .findByConversationId (conversationId );
118126 assertThat (retrievedMessages ).hasSize (3 );
119127
@@ -140,6 +148,8 @@ void shouldReplaceExistingMessages() {
140148 new AssistantMessage ("Initial assistant message" ));
141149 chatMemory .saveAll (conversationId , initialMessages );
142150
151+ sleepForSearchable ();
152+
143153 // Verify initial save
144154 List <Message > retrievedMessages = chatMemory .findByConversationId (conversationId );
145155 assertThat (retrievedMessages ).hasSize (2 );
@@ -149,6 +159,8 @@ void shouldReplaceExistingMessages() {
149159 new UserMessage ("New user message" ));
150160 chatMemory .saveAll (conversationId , newMessages );
151161
162+ sleepForSearchable ();
163+
152164 // Verify replacement
153165 retrievedMessages = chatMemory .findByConversationId (conversationId );
154166 assertThat (retrievedMessages ).hasSize (2 );
@@ -168,12 +180,16 @@ void shouldDeleteConversation() {
168180 new AssistantMessage ("Assistant message" ));
169181 chatMemory .saveAll (conversationId , messages );
170182
183+ sleepForSearchable ();
184+
171185 // Verify messages exist
172186 assertThat (chatMemory .findByConversationId (conversationId )).hasSize (2 );
173187
174188 // Delete conversation
175189 chatMemory .deleteByConversationId (conversationId );
176190
191+ sleepForSearchable ();
192+
177193 // Verify messages are deleted
178194 assertThat (chatMemory .findByConversationId (conversationId )).isEmpty ();
179195 });
@@ -190,6 +206,8 @@ void shouldFindAllConversationIds() {
190206 chatMemory .saveAll (conversationId1 , List .of (new UserMessage ("Message 1" )));
191207 chatMemory .saveAll (conversationId2 , List .of (new UserMessage ("Message 2" )));
192208
209+ sleepForSearchable ();
210+
193211 // Verify both conversation IDs are found
194212 List <String > conversationIds = chatMemory .findConversationIds ();
195213 assertThat (conversationIds ).contains (conversationId1 , conversationId2 );
0 commit comments