Skip to content

test: add a case to test timeformat without year #1432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 110 additions & 15 deletions core/unittest/processor/ProcessorParseTimestampNativeUnittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ProcessorParseTimestampNativeUnittest : public ::testing::Test {
void TestInit();
void TestProcessNoFormat();
void TestProcessRegularFormat();
void TestProcessNoYearFormat();
void TestProcessRegularFormatFailed();
void TestProcessHistoryDiscard();
void TestProcessEventPreciseTimestampLegacy();
Expand All @@ -45,6 +46,7 @@ class ProcessorParseTimestampNativeUnittest : public ::testing::Test {
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestInit);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessNoFormat);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessRegularFormat);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessNoYearFormat);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessRegularFormatFailed);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessHistoryDiscard);
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessEventPreciseTimestampLegacy);
Expand Down Expand Up @@ -394,14 +396,107 @@ void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormat() {
APSARA_TEST_EQUAL_FATAL(CompactJson(expectJsonSs.str()), CompactJson(outJson));
// check observablity
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
// discard history, so output is 0
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcOutRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
// size of one timestamp and one nanosecond equals to 8 byte, respectively
APSARA_TEST_EQUAL_FATAL(8 * 4, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(0, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseErrorTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(8UL * 4, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
}

void ProcessorParseTimestampNativeUnittest::TestProcessNoYearFormat() {
// make config
Json::Value config;
config["SourceKey"] = "time";
config["SourceFormat"] = "%m-%d %H:%M:%S.%f";
config["SourceTimezone"] = "GMT+08:00";
// make events
auto sourceBuffer = std::make_shared<SourceBuffer>();
PipelineEventGroup eventGroup(sourceBuffer);
time_t now = time(nullptr); // will not be discarded by history timeout
char timebuff[32] = "";
std::tm* now_tm = std::localtime(&now);
config["SourceYear"] = now_tm->tm_year + 1900;
strftime(timebuff, sizeof(timebuff), "%m-%d %H:%M:%S.999999999", now_tm);
std::stringstream inJsonSs;
inJsonSs << R"({
"events":
[
{
"contents" :
{
"time" : ")"
<< timebuff << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
},
{
"contents" :
{
"time" : ")"
<< timebuff << R"("
},
"timestamp" : 12345678901,
"timestampNanosecond" : 0,
"type" : 1
}
]
})";
eventGroup.FromJsonString(inJsonSs.str());
// run function
ProcessorParseTimestampNative& processor = *(new ProcessorParseTimestampNative);
std::string pluginId = "testID";
ProcessorInstance processorInstance(&processor, pluginId);
APSARA_TEST_TRUE_FATAL(processorInstance.Init(config, mContext));
std::vector<PipelineEventGroup> eventGroupList;
eventGroupList.emplace_back(std::move(eventGroup));
processorInstance.Process(eventGroupList);

// judge result
std::string outJson = eventGroupList[0].ToJsonString();
std::stringstream expectJsonSs;
expectJsonSs << R"({
"events":
[
{
"contents" :
{
"time" : ")"
<< timebuff << R"("
},
"timestamp" : )"
<< now - processor.mLogTimeZoneOffsetSecond << R"(,
"timestampNanosecond" : 999999999,
"type" : 1
},
{
"contents" :
{
"time" : ")"
<< timebuff << R"("
},
"timestamp" : )"
<< now - processor.mLogTimeZoneOffsetSecond << R"(,
"timestampNanosecond" : 999999999,
"type" : 1
}
]
})";
APSARA_TEST_EQUAL_FATAL(CompactJson(expectJsonSs.str()), CompactJson(outJson));
// check observablity
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
// discard history, so output is 0
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
// size of one timestamp and one nanosecond equals to 8 byte, respectively
APSARA_TEST_EQUAL_FATAL(8UL * 4, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
}

void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormatFailed() {
Expand Down Expand Up @@ -460,14 +555,14 @@ void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormatFailed() {
APSARA_TEST_STREQ_FATAL(CompactJson(inJson).c_str(), CompactJson(outJson).c_str());
// check observablity
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
// discard history, so output is 0
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcOutRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
// size of one timestamp and one nanosecond equals to 8 byte, respectively
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(0, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2, processor.mProcParseErrorTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processor.mProcParseErrorTotal->GetValue());
}

void ProcessorParseTimestampNativeUnittest::TestProcessHistoryDiscard() {
Expand Down Expand Up @@ -520,13 +615,13 @@ void ProcessorParseTimestampNativeUnittest::TestProcessHistoryDiscard() {
// check observablity
std::string outJson = eventGroupList[0].ToJsonString();
APSARA_TEST_EQUAL_FATAL(2, processor.GetContext().GetProcessProfile().historyFailures);
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
// discard history, so output is 0
APSARA_TEST_EQUAL_FATAL(0, processorInstance.mProcOutRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(2, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseErrorTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processorInstance.mProcOutRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseOutSizeBytes->GetValue());
APSARA_TEST_EQUAL_FATAL(2UL, processor.mProcDiscardRecordsTotal->GetValue());
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
}

void ProcessorParseTimestampNativeUnittest::TestProcessEventPreciseTimestampLegacy() {
Expand Down
Loading