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
|
#include "failure_injector.h"
#include <yql/essentials/utils/log/log.h>
#include <library/cpp/testing/unittest/registar.h>
#include <util/datetime/base.h>
#include <chrono>
using namespace NYql;
using namespace NYql::NLog;
using namespace std::chrono;
// do nothing
void OnReach(std::atomic<bool>& called) {
called.store(true);
}
void SetUpLogger() {
TString logType = "cout";
NLog::InitLogger(logType, false);
NLog::EComponentHelpers::ForEach([](NLog::EComponent component) {
NLog::YqlLogger().SetComponentLevel(component, ELevel::DEBUG);
});
}
Y_UNIT_TEST_SUITE(TFailureInjectorTests) {
Y_UNIT_TEST(BasicFailureTest) {
SetUpLogger();
std::atomic<bool> called;
called.store(false);
auto behavior = [&called] { OnReach(called); };
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(false, called.load());
TFailureInjector::Activate();
TFailureInjector::Set("misc_failure", 0, 1);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(true, called.load());
}
Y_UNIT_TEST(CheckSkipTest) {
SetUpLogger();
std::atomic<bool> called;
called.store(false);
auto behavior = [&called] { OnReach(called); };
TFailureInjector::Activate();
TFailureInjector::Set("misc_failure", 1, 1);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(false, called.load());
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(true, called.load());
}
Y_UNIT_TEST(CheckFailCountTest) {
SetUpLogger();
int called = 0;
auto behavior = [&called] { ++called; };
TFailureInjector::Activate();
TFailureInjector::Set("misc_failure", 1, 2);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(0, called);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(1, called);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(2, called);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(2, called);
TFailureInjector::Reach("misc_failure", behavior);
UNIT_ASSERT_EQUAL(2, called);
}
Y_UNIT_TEST(SlowDownTest) {
SetUpLogger();
TFailureInjector::Activate();
TFailureInjector::Set("misc_failure", 0, 1);
auto start = system_clock::now();
TFailureInjector::Reach("misc_failure", [] { ::Sleep(TDuration::Seconds(5)); });
auto finish = system_clock::now();
auto duration = duration_cast<std::chrono::seconds>(finish - start);
YQL_LOG(DEBUG) << "Duration :" << duration.count();
UNIT_ASSERT_GE(duration.count(), 5);
}
}
|