void LastMatchedContainerdTextLineUnittest::TestLastContainerdTextLineMerge()

in core/unittest/reader/GetLastLineDataUnittest.cpp [522:998]


void LastMatchedContainerdTextLineUnittest::TestLastContainerdTextLineMerge() {
    {
        MultilineOptions multilineOpts;
        LogFileReader logFileReader(logPathDir,
                                    utf8File,
                                    DevInode(),
                                    std::make_pair(&readerOpts, &ctx),
                                    std::make_pair(&multilineOpts, &ctx),
                                    std::make_pair(&tagOpts, &ctx));
        BaseLineParse* baseLineParsePtr = nullptr;
        baseLineParsePtr = logFileReader.GetParser<ContainerdTextParser>(LogFileReader::BUFFER_SIZE);
        logFileReader.mLineParsers.emplace_back(baseLineParsePtr);
        {
            {
                std::string testLog = "\n2024-01-05T23:28:06.818486411+08:00 stdout P 123123\n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(0, line.lineEnd);
                APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(1, line.forceRollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
            {
                std::string testLog = "\n2024-01-05T23:28:06.818486411+08:00 stdout F 123123\n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("123123", line.data.to_string());
                APSARA_TEST_EQUAL(1, line.lineBegin);
                APSARA_TEST_EQUAL(endPs, line.lineEnd);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
        }
        // 异常情况+有回车
        { // case: PartLogFlag存在,第三个空格存在但空格后无内容
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout P \n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(1, line.forceRollbackLineFeedCount);
                APSARA_TEST_EQUAL(false, line.fullLine);
            }
            // case: PartLogFlag存在,第三个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout P\n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(1, line.forceRollbackLineFeedCount);
                APSARA_TEST_EQUAL(false, line.fullLine);
            }
            // case: PartLogFlag不存在,第二个空格存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout \n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
            // case: 第二个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout\n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL(testLog.substr(0, size - 1), line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
            // case: 第一个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00stdout\n";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL(testLog.substr(0, size - 1), line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
        }
        // 异常情况+无回车
        { // case: PartLogFlag存在,第三个空格存在但空格后无内容
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout P ";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(1, line.forceRollbackLineFeedCount);
                APSARA_TEST_EQUAL(false, line.fullLine);
            }
            // case: PartLogFlag存在,第三个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout P";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(1, line.forceRollbackLineFeedCount);
                APSARA_TEST_EQUAL(false, line.fullLine);
            }
            // case: PartLogFlag不存在,第二个空格存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout ";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL("", line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
            // case: 第二个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00 stdout";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL(testLog, line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
            // case: 第一个空格不存在
            {
                std::string testLog = "2024-01-05T23:28:06.818486411+08:00stdout";

                int32_t size = testLog.size();
                int32_t endPs; // the position of \n or \0
                if (testLog[size - 1] == '\n') {
                    endPs = size - 1;
                } else {
                    endPs = size;
                }
                LineInfo line = logFileReader.GetLastLine(testLog, endPs);

                APSARA_TEST_EQUAL(testLog, line.data.to_string());
                APSARA_TEST_EQUAL(0, line.lineBegin);
                APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
                APSARA_TEST_EQUAL(true, line.fullLine);
            }
        }
        // case: F + P + P
        {
            std::string testLog = LOG_FULL + "789\n" + LOG_PART + "123\n" + LOG_PART + "456";
            std::string expectedLog = LOG_FULL + "789\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(2, line.forceRollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // case: F + P + P + '\n'
        {
            std::string testLog = LOG_FULL + "789\n" + LOG_PART + "123\n" + LOG_PART + "456\n";
            std::string expectedLog = LOG_FULL + "789\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(2, line.forceRollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }

        // case: F + P + P + F
        {
            std::string testLog = LOG_FULL + "789\n" + LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_FULL + "789";
            std::string expectedLog = LOG_FULL + "789\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456789", line.data.to_string());
            APSARA_TEST_EQUAL(int(expectedLog.size()), line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // case: F + P + P + F + '\n'
        {
            std::string testLog = LOG_FULL + "789\n" + LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_FULL + "789\n";
            std::string expectedLog = LOG_FULL + "789\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456789", line.data.to_string());
            APSARA_TEST_EQUAL(int(expectedLog.size()), line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }

        // F + errorLog
        {
            std::string testLog = LOG_FULL + "456\n" + LOG_ERROR + "789";
            std::string expectedLog = LOG_FULL + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL(LOG_ERROR + "789", line.data.to_string());
            APSARA_TEST_EQUAL(int(expectedLog.size()), line.lineBegin);
            APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // F + errorLog + '\n'
        {
            std::string testLog = LOG_FULL + "456\n" + LOG_ERROR + "789\n";
            std::string expectedLog = LOG_FULL + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL(LOG_ERROR + "789", line.data.to_string());
            APSARA_TEST_EQUAL(int(expectedLog.size()), line.lineBegin);
            APSARA_TEST_EQUAL(1, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }

        // case: P + P + F
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_FULL + "789";
            std::string expectedLog = LOG_PART + "123\n" + LOG_PART + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // case: P + P + F + '\n'
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_FULL + "789\n";
            std::string expectedLog = LOG_PART + "123\n" + LOG_PART + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }

        // case: P + P + error
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_ERROR + "789";
            std::string expectedLog = LOG_PART + "123\n" + LOG_PART + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456" + LOG_ERROR + "789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // case: P + P + error + '\n'
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456\n" + LOG_ERROR + "789\n";
            std::string expectedLog = LOG_PART + "123\n" + LOG_PART + "456\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("123456" + LOG_ERROR + "789", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(3, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(true, line.fullLine);
        }
        // case: P + P
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456";
            std::string expectedLog = LOG_PART + "123\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(2, line.forceRollbackLineFeedCount);
            APSARA_TEST_EQUAL(false, line.fullLine);
        }
        // case: P + P + '\n'
        {
            std::string testLog = LOG_PART + "123\n" + LOG_PART + "456\n";
            std::string expectedLog = LOG_PART + "123\n";

            int32_t size = testLog.size();
            int32_t endPs; // the position of \n or \0
            if (testLog[size - 1] == '\n') {
                endPs = size - 1;
            } else {
                endPs = size;
            }
            LineInfo line = logFileReader.GetLastLine(testLog, endPs);

            APSARA_TEST_EQUAL("", line.data.to_string());
            APSARA_TEST_EQUAL(0, line.lineBegin);
            APSARA_TEST_EQUAL(0, line.rollbackLineFeedCount);
            APSARA_TEST_EQUAL(2, line.forceRollbackLineFeedCount);
            APSARA_TEST_EQUAL(false, line.fullLine);
        }
    }
}