Skip to content

Commit 30496b2

Browse files
authored
test: add a case to test timeformat without year (#1432)
1 parent 80e2f6a commit 30496b2

File tree

1 file changed

+110
-15
lines changed

1 file changed

+110
-15
lines changed

core/unittest/processor/ProcessorParseTimestampNativeUnittest.cpp

+110-15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ProcessorParseTimestampNativeUnittest : public ::testing::Test {
3434
void TestInit();
3535
void TestProcessNoFormat();
3636
void TestProcessRegularFormat();
37+
void TestProcessNoYearFormat();
3738
void TestProcessRegularFormatFailed();
3839
void TestProcessHistoryDiscard();
3940
void TestProcessEventPreciseTimestampLegacy();
@@ -45,6 +46,7 @@ class ProcessorParseTimestampNativeUnittest : public ::testing::Test {
4546
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestInit);
4647
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessNoFormat);
4748
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessRegularFormat);
49+
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessNoYearFormat);
4850
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessRegularFormatFailed);
4951
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessHistoryDiscard);
5052
UNIT_TEST_CASE(ProcessorParseTimestampNativeUnittest, TestProcessEventPreciseTimestampLegacy);
@@ -394,14 +396,107 @@ void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormat() {
394396
APSARA_TEST_EQUAL_FATAL(CompactJson(expectJsonSs.str()), CompactJson(outJson));
395397
// check observablity
396398
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
397-
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
399+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
398400
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
399401
// discard history, so output is 0
400-
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcOutRecordsTotal->GetValue());
402+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
401403
// size of one timestamp and one nanosecond equals to 8 byte, respectively
402-
APSARA_TEST_EQUAL_FATAL(8 * 4, processor.mProcParseOutSizeBytes->GetValue());
403-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcDiscardRecordsTotal->GetValue());
404-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseErrorTotal->GetValue());
404+
APSARA_TEST_EQUAL_FATAL(8UL * 4, processor.mProcParseOutSizeBytes->GetValue());
405+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
406+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
407+
}
408+
409+
void ProcessorParseTimestampNativeUnittest::TestProcessNoYearFormat() {
410+
// make config
411+
Json::Value config;
412+
config["SourceKey"] = "time";
413+
config["SourceFormat"] = "%m-%d %H:%M:%S.%f";
414+
config["SourceTimezone"] = "GMT+08:00";
415+
// make events
416+
auto sourceBuffer = std::make_shared<SourceBuffer>();
417+
PipelineEventGroup eventGroup(sourceBuffer);
418+
time_t now = time(nullptr); // will not be discarded by history timeout
419+
char timebuff[32] = "";
420+
std::tm* now_tm = std::localtime(&now);
421+
config["SourceYear"] = now_tm->tm_year + 1900;
422+
strftime(timebuff, sizeof(timebuff), "%m-%d %H:%M:%S.999999999", now_tm);
423+
std::stringstream inJsonSs;
424+
inJsonSs << R"({
425+
"events":
426+
[
427+
{
428+
"contents" :
429+
{
430+
"time" : ")"
431+
<< timebuff << R"("
432+
},
433+
"timestamp" : 12345678901,
434+
"timestampNanosecond" : 0,
435+
"type" : 1
436+
},
437+
{
438+
"contents" :
439+
{
440+
"time" : ")"
441+
<< timebuff << R"("
442+
},
443+
"timestamp" : 12345678901,
444+
"timestampNanosecond" : 0,
445+
"type" : 1
446+
}
447+
]
448+
})";
449+
eventGroup.FromJsonString(inJsonSs.str());
450+
// run function
451+
ProcessorParseTimestampNative& processor = *(new ProcessorParseTimestampNative);
452+
std::string pluginId = "testID";
453+
ProcessorInstance processorInstance(&processor, pluginId);
454+
APSARA_TEST_TRUE_FATAL(processorInstance.Init(config, mContext));
455+
std::vector<PipelineEventGroup> eventGroupList;
456+
eventGroupList.emplace_back(std::move(eventGroup));
457+
processorInstance.Process(eventGroupList);
458+
459+
// judge result
460+
std::string outJson = eventGroupList[0].ToJsonString();
461+
std::stringstream expectJsonSs;
462+
expectJsonSs << R"({
463+
"events":
464+
[
465+
{
466+
"contents" :
467+
{
468+
"time" : ")"
469+
<< timebuff << R"("
470+
},
471+
"timestamp" : )"
472+
<< now - processor.mLogTimeZoneOffsetSecond << R"(,
473+
"timestampNanosecond" : 999999999,
474+
"type" : 1
475+
},
476+
{
477+
"contents" :
478+
{
479+
"time" : ")"
480+
<< timebuff << R"("
481+
},
482+
"timestamp" : )"
483+
<< now - processor.mLogTimeZoneOffsetSecond << R"(,
484+
"timestampNanosecond" : 999999999,
485+
"type" : 1
486+
}
487+
]
488+
})";
489+
APSARA_TEST_EQUAL_FATAL(CompactJson(expectJsonSs.str()), CompactJson(outJson));
490+
// check observablity
491+
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
492+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
493+
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
494+
// discard history, so output is 0
495+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
496+
// size of one timestamp and one nanosecond equals to 8 byte, respectively
497+
APSARA_TEST_EQUAL_FATAL(8UL * 4, processor.mProcParseOutSizeBytes->GetValue());
498+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
499+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
405500
}
406501

407502
void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormatFailed() {
@@ -460,14 +555,14 @@ void ProcessorParseTimestampNativeUnittest::TestProcessRegularFormatFailed() {
460555
APSARA_TEST_STREQ_FATAL(CompactJson(inJson).c_str(), CompactJson(outJson).c_str());
461556
// check observablity
462557
APSARA_TEST_EQUAL_FATAL(0, processor.GetContext().GetProcessProfile().historyFailures);
463-
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
558+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
464559
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
465560
// discard history, so output is 0
466-
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcOutRecordsTotal->GetValue());
561+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcOutRecordsTotal->GetValue());
467562
// size of one timestamp and one nanosecond equals to 8 byte, respectively
468-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseOutSizeBytes->GetValue());
469-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcDiscardRecordsTotal->GetValue());
470-
APSARA_TEST_EQUAL_FATAL(2, processor.mProcParseErrorTotal->GetValue());
563+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseOutSizeBytes->GetValue());
564+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcDiscardRecordsTotal->GetValue());
565+
APSARA_TEST_EQUAL_FATAL(2UL, processor.mProcParseErrorTotal->GetValue());
471566
}
472567

473568
void ProcessorParseTimestampNativeUnittest::TestProcessHistoryDiscard() {
@@ -520,13 +615,13 @@ void ProcessorParseTimestampNativeUnittest::TestProcessHistoryDiscard() {
520615
// check observablity
521616
std::string outJson = eventGroupList[0].ToJsonString();
522617
APSARA_TEST_EQUAL_FATAL(2, processor.GetContext().GetProcessProfile().historyFailures);
523-
APSARA_TEST_EQUAL_FATAL(2, processorInstance.mProcInRecordsTotal->GetValue());
618+
APSARA_TEST_EQUAL_FATAL(2UL, processorInstance.mProcInRecordsTotal->GetValue());
524619
APSARA_TEST_EQUAL_FATAL(strlen(timebuff) * 2, processor.mProcParseInSizeBytes->GetValue());
525620
// discard history, so output is 0
526-
APSARA_TEST_EQUAL_FATAL(0, processorInstance.mProcOutRecordsTotal->GetValue());
527-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseOutSizeBytes->GetValue());
528-
APSARA_TEST_EQUAL_FATAL(2, processor.mProcDiscardRecordsTotal->GetValue());
529-
APSARA_TEST_EQUAL_FATAL(0, processor.mProcParseErrorTotal->GetValue());
621+
APSARA_TEST_EQUAL_FATAL(0UL, processorInstance.mProcOutRecordsTotal->GetValue());
622+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseOutSizeBytes->GetValue());
623+
APSARA_TEST_EQUAL_FATAL(2UL, processor.mProcDiscardRecordsTotal->GetValue());
624+
APSARA_TEST_EQUAL_FATAL(0UL, processor.mProcParseErrorTotal->GetValue());
530625
}
531626

532627
void ProcessorParseTimestampNativeUnittest::TestProcessEventPreciseTimestampLegacy() {

0 commit comments

Comments
 (0)