@@ -218,6 +218,39 @@ TEST_F(CrashRecoveryTest, CrashRecoveryDuringInsertion) {
218218}
219219
220220
221+ TEST_F (CrashRecoveryTest, OptimizeAfterCrashRecoveryPersistsReplayedData) {
222+ {
223+ auto schema = CreateTestSchema (collection_name_);
224+ auto result = Collection::CreateAndOpen (dir_path_, *schema, options_);
225+ ASSERT_TRUE (result.has_value ()) << result.error ().message ();
226+ }
227+
228+ RunGeneratorAndCrash (" 0" , " 10000" , " insert" , " 0" , 1 );
229+
230+ uint64_t recovered_doc_count = 0 ;
231+ {
232+ auto result = Collection::Open (dir_path_, options_);
233+ ASSERT_TRUE (result.has_value ())
234+ << " Failed to reopen collection after crash recovery" ;
235+ auto collection = result.value ();
236+
237+ recovered_doc_count = collection->Stats ().value ().doc_count ;
238+ ASSERT_GT (recovered_doc_count, 0 )
239+ << " No documents were recovered from the WAL" ;
240+
241+ auto status = collection->Optimize ();
242+ ASSERT_TRUE (status.ok ()) << status.message ();
243+ ASSERT_EQ (collection->Stats ().value ().doc_count , recovered_doc_count);
244+ }
245+
246+ auto result = Collection::Open (dir_path_, options_);
247+ ASSERT_TRUE (result.has_value ())
248+ << " Failed to reopen collection after optimizing recovered data" ;
249+ ASSERT_EQ (result.value ()->Stats ().value ().doc_count , recovered_doc_count)
250+ << " Optimize discarded documents restored from the WAL" ;
251+ }
252+
253+
221254TEST_F (CrashRecoveryTest, CrashRecoveryDuringUpsert) {
222255 {
223256 auto schema = CreateTestSchema (collection_name_);
0 commit comments