forked from apache/nifi-minifi-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnScheduleErrorHandlingTests.cpp
131 lines (112 loc) · 5.96 KB
/
OnScheduleErrorHandlingTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "integration/IntegrationBase.h"
#include "core/logging/Logger.h"
#include "minifi-cpp/core/Scheduling.h"
#include "core/state/ProcessorController.h"
#include "unit/TestBase.h"
#include "../../../extensions/test-processors/KamikazeProcessor.h"
#include "utils/StringUtils.h"
#include "unit/TestUtils.h"
#include "unit/Catch.h"
namespace org::apache::nifi::minifi::test {
/*Verify behavior in case exceptions are thrown in onSchedule or onTrigger functions
* KamikazeProcessor is a test processor to trigger errors in these functions */
class KamikazeErrorHandlingTests : public IntegrationBase {
public:
void runAssertions() override {
using minifi::test::utils::verifyEventHappenedInPollTime;
REQUIRE(verifyEventHappenedInPollTime(wait_time_, [&] {
const std::string logs = LogTestController::getInstance().getLogs();
const auto result = minifi::utils::string::countOccurrences(logs, minifi::processors::KamikazeProcessor::OnScheduleExceptionStr);
const int occurrences = result.second;
return 1 < occurrences;
}));
flowController_->updatePropertyValue("kamikaze", std::string(minifi::processors::KamikazeProcessor::ThrowInOnSchedule.name), "false");
const std::vector<std::string> must_appear_byorder_msgs = {minifi::processors::KamikazeProcessor::OnUnScheduleLogStr,
minifi::processors::KamikazeProcessor::OnScheduleLogStr,
minifi::processors::KamikazeProcessor::OnTriggerExceptionStr,
"[warning] ProcessSession rollback for kamikaze executed"};
const bool test_success = verifyEventHappenedInPollTime(std::chrono::milliseconds(wait_time_), [&] {
const std::string logs = LogTestController::getInstance().getLogs();
const auto result = minifi::utils::string::countOccurrences(logs, minifi::processors::KamikazeProcessor::OnScheduleExceptionStr);
size_t last_pos = result.first;
for (const std::string& msg : must_appear_byorder_msgs) {
last_pos = logs.find(msg, last_pos);
if (last_pos == std::string::npos) {
return false;
}
}
return true;
});
REQUIRE(test_success);
REQUIRE(LogTestController::getInstance().getLogs().find(minifi::processors::KamikazeProcessor::OnTriggerLogStr) == std::string::npos);
}
void testSetup() override {
LogTestController::getInstance().setDebug<core::ProcessGroup>();
LogTestController::getInstance().setDebug<core::Processor>();
LogTestController::getInstance().setDebug<core::ProcessSession>();
LogTestController::getInstance().setDebug<minifi::processors::KamikazeProcessor>();
configuration->set(minifi::Configure::nifi_administrative_yield_duration, "100 ms");
}
};
/*Verify that event driven processors without incoming connections are not scheduled*/
class EventDriverScheduleErrorHandlingTests: public IntegrationBase {
public:
void updateProperties(minifi::FlowController& fc) override {
/* This tests depends on a configuration that contains only one KamikazeProcessor named kamikaze
* (See testOnScheduleRetry.yml)
* In this case there are two components in the flowcontroller: first is the processor that the test uses,
* second is the controller itself.
* Added here some assertions to make it clear. In case any of these fail without changing the corresponding yml file,
* that most probably means a breaking change. */
size_t controllerVecIdx = 0;
fc.executeOnAllComponents([&controllerVecIdx](minifi::state::StateController& component){
if (controllerVecIdx == 1) {
REQUIRE(component.getComponentName() == "FlowController");
} else if (controllerVecIdx == 0) {
REQUIRE(component.getComponentName() == "kamikaze");
auto process_controller = dynamic_cast<minifi::state::ProcessorController*>(&component);
REQUIRE(process_controller != nullptr);
process_controller->getProcessor().setSchedulingStrategy(minifi::core::SchedulingStrategy::EVENT_DRIVEN);
}
++controllerVecIdx;
});
// check controller vector size
REQUIRE(controllerVecIdx == 2);
}
void runAssertions() override {
std::string logs = LogTestController::getInstance().getLogs();
REQUIRE(logs.find("EventDrivenSchedulingAgent cannot schedule processor without incoming connection!") != std::string::npos);
}
void testSetup() override {
LogTestController::getInstance().setDebug<core::ProcessGroup>();
configuration->set(minifi::Configure::nifi_administrative_yield_duration, "100 ms");
}
};
TEST_CASE("KamikazeErrorHandlingTests", "[OnScheduleErrorHandlingTests]") {
KamikazeErrorHandlingTests harness_kamikaze;
const auto test_file_location = std::filesystem::path(TEST_RESOURCES) / "TestOnScheduleRetry.yml";
harness_kamikaze.run(test_file_location);
}
TEST_CASE("EventDriverScheduleErrorHandlingTests", "[OnScheduleErrorHandlingTests]") {
EventDriverScheduleErrorHandlingTests harness_eventdrivenerror;
const auto test_file_location = std::filesystem::path(TEST_RESOURCES) / "TestOnScheduleRetry.yml";
harness_eventdrivenerror.run(test_file_location);
}
} // namespace org::apache::nifi::minifi::test