-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathtermination_predicate_test.cc
More file actions
82 lines (67 loc) · 3.54 KB
/
termination_predicate_test.cc
File metadata and controls
82 lines (67 loc) · 3.54 KB
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
#include <chrono>
#include "external/envoy/test/mocks/event/mocks.h"
#include "external/envoy/test/mocks/stats/mocks.h"
#include "external/envoy/test/test_common/simulated_time_system.h"
#include "external/envoy/test/test_common/utility.h"
#include "source/common/termination_predicate_impl.h"
#include "gtest/gtest.h"
using namespace std::chrono_literals;
using namespace testing;
namespace Nighthawk {
class TerminationPredicateTest : public Test {
public:
TerminationPredicateTest() : api_(Envoy::Api::createApiForTest(stats_store_)) {}
Envoy::Api::ApiPtr api_;
Envoy::Event::SimulatedTimeSystem time_system;
Envoy::Stats::MockIsolatedStatsStore stats_store_;
};
TEST_F(TerminationPredicateTest, DurationTerminationPredicateImplTest) {
const auto duration = 100us;
DurationTerminationPredicateImpl pred(time_system, duration, time_system.monotonicTime());
EXPECT_EQ(pred.evaluate(), TerminationPredicate::Status::PROCEED);
// move to the edge.
time_system.advanceTimeWait(duration);
EXPECT_EQ(pred.evaluate(), TerminationPredicate::Status::PROCEED);
// move past the edge, we expect the predicate to return TERMINATE.
time_system.advanceTimeWait(1us);
EXPECT_EQ(pred.evaluate(), TerminationPredicate::Status::TERMINATE);
}
TEST_F(TerminationPredicateTest, StatsCounterAbsoluteThresholdTerminationPredicateImpl) {
auto& counter = stats_store_.counter("foo");
const TerminationPredicate::Status termination_status = TerminationPredicate::Status::FAIL;
StatsCounterAbsoluteThresholdTerminationPredicateImpl pred(counter, 0, termination_status);
EXPECT_EQ(pred.evaluate(), TerminationPredicate::Status::PROCEED);
counter.inc();
EXPECT_EQ(pred.evaluate(), termination_status);
}
TEST_F(TerminationPredicateTest, LinkedPredicates) {
auto& fail_counter = stats_store_.counter("counter-associated-to-fail");
auto& terminate_counter = stats_store_.counter("counter-associated-to-terminate");
StatsCounterAbsoluteThresholdTerminationPredicateImpl fail_pred(
fail_counter, 0, TerminationPredicate::Status::FAIL);
fail_pred.link(std::make_unique<StatsCounterAbsoluteThresholdTerminationPredicateImpl>(
terminate_counter, 0, TerminationPredicate::Status::TERMINATE));
EXPECT_EQ(fail_pred.evaluateChain(), TerminationPredicate::Status::PROCEED);
fail_counter.inc();
EXPECT_EQ(fail_pred.evaluateChain(), TerminationPredicate::Status::FAIL);
// We expect linked child predicates to be evaluated first. Hence, bumping the
// termination counter ought to make the linked child return its terminal status,
// which is TERMINATE.
terminate_counter.inc();
EXPECT_EQ(fail_pred.evaluateChain(), TerminationPredicate::Status::TERMINATE);
}
TEST_F(TerminationPredicateTest, AppendToChain) {
auto& foo_counter = stats_store_.counter("foo");
foo_counter.inc();
StatsCounterAbsoluteThresholdTerminationPredicateImpl predicate(
foo_counter, 1, TerminationPredicate::Status::TERMINATE);
// The counter doesn't exceed the predicate threshold, so we shouldn't see TERMINATE
EXPECT_EQ(predicate.evaluateChain(), TerminationPredicate::Status::PROCEED);
auto child_predicate = std::make_unique<StatsCounterAbsoluteThresholdTerminationPredicateImpl>(
foo_counter, 0, TerminationPredicate::Status::FAIL);
const auto* unsafe_child_predicate_ptr = child_predicate.get();
EXPECT_EQ(unsafe_child_predicate_ptr, &(predicate.appendToChain(std::move(child_predicate))));
// This ought to evaluate to FAIL as the counter threshold is exceeded.
EXPECT_EQ(predicate.evaluateChain(), TerminationPredicate::Status::FAIL);
}
} // namespace Nighthawk