diff options
author | Alexey Salmin <alexey.salmin@gmail.com> | 2022-02-10 16:49:37 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:37 +0300 |
commit | 3c5b1607b38f637d2f3313791ed25c2e080d2647 (patch) | |
tree | 99be7b96e7c66612fbca94331100ef3b5fedcb88 /library/cpp/retry | |
parent | de89752358147d7b25ef59a85b431bb564068a49 (diff) | |
download | ydb-3c5b1607b38f637d2f3313791ed25c2e080d2647.tar.gz |
Restoring authorship annotation for Alexey Salmin <alexey.salmin@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/retry')
-rw-r--r-- | library/cpp/retry/retry.cpp | 18 | ||||
-rw-r--r-- | library/cpp/retry/retry.h | 4 | ||||
-rw-r--r-- | library/cpp/retry/retry_ut.cpp | 100 | ||||
-rw-r--r-- | library/cpp/retry/ut/ya.make | 14 |
4 files changed, 68 insertions, 68 deletions
diff --git a/library/cpp/retry/retry.cpp b/library/cpp/retry/retry.cpp index 92466cdeca..eb589832eb 100644 --- a/library/cpp/retry/retry.cpp +++ b/library/cpp/retry/retry.cpp @@ -5,17 +5,17 @@ void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions) { DoWithRetry(func, retryOptions, true); } - -bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) { - for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) { - if (func()) { - return true; - } + +bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions) { + for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) { + if (func()) { + return true; + } auto sleep = retryOptions.SleepFunction; sleep(retryOptions.GetTimeToSleep(attempt)); - } - return false; -} + } + return false; +} TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions) { return TRetryOptions(retryOptions.GetMaxTries(), diff --git a/library/cpp/retry/retry.h b/library/cpp/retry/retry.h index c47ff5070f..d8c70bf099 100644 --- a/library/cpp/retry/retry.h +++ b/library/cpp/retry/retry.h @@ -126,8 +126,8 @@ bool DoWithRetry(std::function<void()> func, TRetryOptions retryOptions, bool th void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions); -bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions); - +bool DoWithRetryOnRetCode(std::function<bool()> func, TRetryOptions retryOptions); + Y_DECLARE_PODTYPE(TRetryOptions); TRetryOptions MakeRetryOptions(const NRetry::TRetryOptionsPB& retryOptions); diff --git a/library/cpp/retry/retry_ut.cpp b/library/cpp/retry/retry_ut.cpp index 92153e987e..f9eb58867b 100644 --- a/library/cpp/retry/retry_ut.cpp +++ b/library/cpp/retry/retry_ut.cpp @@ -1,36 +1,36 @@ -#include "retry.h" - +#include "retry.h" + #include <library/cpp/testing/unittest/registar.h> + +namespace { + class TDoOnSecondOrThrow { + public: + ui32 operator()() { + if (attempt++ != 1) { + throw yexception(); + } + return 42; + } -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; - }; -} + 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()))); - } + 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; }; @@ -38,19 +38,19 @@ Y_UNIT_TEST_SUITE(Retry) { UNIT_ASSERT_EQUAL(value, 1); } Y_UNIT_TEST(RetryOnExceptionFail) { - UNIT_ASSERT_EXCEPTION(DoWithRetry(TDoOnSecondOrThrow{}, TRetryOptions(0, TDuration::Zero())), yexception); - } + 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)); - } + 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{}; @@ -59,9 +59,9 @@ Y_UNIT_TEST_SUITE(Retry) { 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()); - } + 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{}; @@ -69,11 +69,11 @@ Y_UNIT_TEST_SUITE(Retry) { 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)); - } + 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{}; @@ -82,9 +82,9 @@ Y_UNIT_TEST_SUITE(Retry) { 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); - } + 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{}; @@ -92,13 +92,13 @@ Y_UNIT_TEST_SUITE(Retry) { 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()))); - } + UNIT_ASSERT(true == DoWithRetryOnRetCode(TDoOnSecondOrFail{}, TRetryOptions(1, TDuration::Zero()))); + } Y_UNIT_TEST(RetryOnRetCodeFail) { - UNIT_ASSERT(false == DoWithRetryOnRetCode(TDoOnSecondOrFail{}, TRetryOptions(0, TDuration::Zero()))); - } + UNIT_ASSERT(false == DoWithRetryOnRetCode(TDoOnSecondOrFail{}, TRetryOptions(0, TDuration::Zero()))); + } Y_UNIT_TEST(MakeRetryOptionsFromProto) { NRetry::TRetryOptionsPB protoOptions; protoOptions.SetMaxTries(1); @@ -114,4 +114,4 @@ Y_UNIT_TEST_SUITE(Retry) { UNIT_ASSERT_EQUAL(options.SleepRandomDelta, TDuration::MilliSeconds(4)); UNIT_ASSERT_EQUAL(options.SleepExponentialMultiplier, TDuration::MilliSeconds(5)); } -} +} diff --git a/library/cpp/retry/ut/ya.make b/library/cpp/retry/ut/ya.make index ff8259bfdb..d47f1c5cc3 100644 --- a/library/cpp/retry/ut/ya.make +++ b/library/cpp/retry/ut/ya.make @@ -1,13 +1,13 @@ UNITTEST_FOR(library/cpp/retry) - + OWNER( salmin osado g:yabs-small ) - -SRCS( - retry_ut.cpp -) - -END() + +SRCS( + retry_ut.cpp +) + +END() |