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
|
#include "retry.h"
#include <library/cpp/testing/unittest/registar.h>
namespace {
class TDoOnSecondOrThrow {
public:
ui32 operator()() {
if (attempt++ != 1) {
throw yexception();
}
return 42;
}
private:
ui32 attempt = 0;
};
class TDoOnSecondOrFail {
public:
bool operator()() {
return (attempt++ == 1);
}
private:
ui32 attempt = 0;
};
}
Y_UNIT_TEST_SUITE(Retry) {
Y_UNIT_TEST(RetryOnExceptionSuccess) {
UNIT_ASSERT_NO_EXCEPTION(DoWithRetry(TDoOnSecondOrThrow{}, TRetryOptions(1, TDuration::Zero())));
}
Y_UNIT_TEST(RetryOnExceptionSuccessWithOnFail) {
ui32 value = 0;
std::function<void(const yexception&)> cb = [&value](const yexception&){ value += 1; };
UNIT_ASSERT_NO_EXCEPTION(DoWithRetry<ui32>(TDoOnSecondOrThrow{}, cb, TRetryOptions(1, TDuration::Zero()), true));
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnExceptionFail) {
UNIT_ASSERT_EXCEPTION(DoWithRetry(TDoOnSecondOrThrow{}, TRetryOptions(0, TDuration::Zero())), yexception);
}
Y_UNIT_TEST(RetryOnExceptionFailWithOnFail) {
ui32 value = 0;
std::function<void(const yexception&)> cb = [&value](const yexception&) { value += 1; };
UNIT_ASSERT_EXCEPTION(DoWithRetry<ui32>(TDoOnSecondOrThrow{}, cb, TRetryOptions(0, TDuration::Zero()), true), yexception);
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnExceptionSuccessWithValue) {
std::function<ui32()> f = TDoOnSecondOrThrow{};
UNIT_ASSERT(42 == *DoWithRetry<ui32>(f, TRetryOptions(1, TDuration::Zero()), false));
}
Y_UNIT_TEST(RetryOnExceptionSuccessWithValueWithOnFail) {
ui32 value = 0;
std::function<ui32()> f = TDoOnSecondOrThrow{};
std::function<void(const yexception&)> cb = [&value](const yexception&){ value += 1; };
UNIT_ASSERT(42 == *DoWithRetry<ui32>(f, cb, TRetryOptions(1, TDuration::Zero()), false));
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnExceptionFailWithValue) {
std::function<ui32()> f = TDoOnSecondOrThrow{};
UNIT_ASSERT(!DoWithRetry<ui32>(f, TRetryOptions(0, TDuration::Zero()), false).Defined());
}
Y_UNIT_TEST(RetryOnExceptionFailWithValueWithOnFail) {
ui32 value = 0;
std::function<ui32()> f = TDoOnSecondOrThrow{};
std::function<void(const yexception&)> cb = [&value](const yexception&){ value += 1; };
UNIT_ASSERT(!DoWithRetry<ui32>(f, cb, TRetryOptions(0, TDuration::Zero()), false).Defined());
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnExceptionSuccessWithValueAndRethrow) {
std::function<ui32()> f = TDoOnSecondOrThrow{};
UNIT_ASSERT(42 == *DoWithRetry<ui32>(f, TRetryOptions(1, TDuration::Zero()), true));
}
Y_UNIT_TEST(RetryOnExceptionSuccessWithValueAndRethrowWithOnFail) {
ui32 value = 0;
std::function<ui32()> f = TDoOnSecondOrThrow{};
std::function<void(const yexception&)> cb = [&value](const yexception&){ value += 1; };
UNIT_ASSERT(42 == *DoWithRetry<ui32>(f, cb, TRetryOptions(1, TDuration::Zero()), true));
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnExceptionFailWithValueAndRethrow) {
std::function<ui32()> f = TDoOnSecondOrThrow{};
UNIT_ASSERT_EXCEPTION(DoWithRetry<ui32>(f, TRetryOptions(0, TDuration::Zero()), true), yexception);
}
Y_UNIT_TEST(RetryOnExceptionFailWithValueAndRethrowWithOnFail) {
ui32 value = 0;
std::function<ui32()> f = TDoOnSecondOrThrow{};
std::function<void(const yexception&)> cb = [&value](const yexception&){ value += 1; };
UNIT_ASSERT_EXCEPTION(42 == *DoWithRetry<ui32>(f, cb, TRetryOptions(0, TDuration::Zero()), true), yexception);
UNIT_ASSERT_EQUAL(value, 1);
}
Y_UNIT_TEST(RetryOnRetCodeSuccess) {
UNIT_ASSERT(true == DoWithRetryOnRetCode(TDoOnSecondOrFail{}, TRetryOptions(1, TDuration::Zero())));
}
Y_UNIT_TEST(RetryOnRetCodeFail) {
UNIT_ASSERT(false == DoWithRetryOnRetCode(TDoOnSecondOrFail{}, TRetryOptions(0, TDuration::Zero())));
}
Y_UNIT_TEST(MakeRetryOptionsFromProto) {
NRetry::TRetryOptionsPB protoOptions;
protoOptions.SetMaxTries(1);
protoOptions.SetInitialSleepMs(2);
protoOptions.SetSleepIncrementMs(3);
protoOptions.SetRandomDeltaMs(4);
protoOptions.SetExponentalMultiplierMs(5);
const TRetryOptions options = MakeRetryOptions(protoOptions);
UNIT_ASSERT_EQUAL(options.RetryCount, 1);
UNIT_ASSERT_EQUAL(options.SleepDuration, TDuration::MilliSeconds(2));
UNIT_ASSERT_EQUAL(options.SleepIncrement, TDuration::MilliSeconds(3));
UNIT_ASSERT_EQUAL(options.SleepRandomDelta, TDuration::MilliSeconds(4));
UNIT_ASSERT_EQUAL(options.SleepExponentialMultiplier, TDuration::MilliSeconds(5));
}
}
|