@@ -517,6 +517,30 @@ void RemoveLastIncompleteLogUnittest::TestRemoveLastIncompleteLogNotValidJson()
517517 APSARA_TEST_EQUAL_FATAL (std::string (testLog.data (), matchSize), expectMatch);
518518 APSARA_TEST_EQUAL_FATAL (0 , rollbackLineFeedCount);
519519 }
520+ { // test invalid \n at the end of buffer
521+ std::string firstLog = R"( {
522+ "key": {
523+ "nested_key": "second value"
524+ }
525+ })" ;
526+ std::string notjson = " not a json at all.\n not a json at all." ;
527+ std::string testLog = firstLog + ' \n ' + notjson;
528+ int32_t logSize = testLog.size ();
529+ testLog += " \n not a json at all." ;
530+ std::replace (notjson.begin (), notjson.end (), ' \n ' , ' \0 ' );
531+ size_t pos = notjson.rfind (" not a json at all." );
532+ if (pos != std::string::npos) {
533+ notjson = notjson.substr (0 , pos);
534+ }
535+ std::string expectMatch = firstLog + ' \0 ' + notjson;
536+ ;
537+ int32_t rollbackLineFeedCount = 0 ;
538+ size_t matchSize = mLogFileReader ->RemoveLastIncompleteLog (
539+ const_cast <char *>(testLog.data ()), logSize, rollbackLineFeedCount);
540+ APSARA_TEST_EQUAL_FATAL (expectMatch.size (), matchSize);
541+ APSARA_TEST_EQUAL_FATAL (std::string (testLog.data (), matchSize), expectMatch);
542+ APSARA_TEST_EQUAL_FATAL (1 , rollbackLineFeedCount);
543+ }
520544}
521545
522546void RemoveLastIncompleteLogUnittest::TestRemoveLastIncompleteLogNotValidJsonNoRollback () {
0 commit comments