diff options
author | elshiko <elshiko@yandex-team.ru> | 2022-02-10 16:49:20 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:20 +0300 |
commit | 455ab915435b70e433ed91eee2ca84f03122d6cb (patch) | |
tree | 4570335e510c5cb009cca313349e2a29b4c62310 /library/cpp/retry | |
parent | 85ad20a00eb9c7d4dcae0debe5172d5f9bc6792f (diff) | |
download | ydb-455ab915435b70e433ed91eee2ca84f03122d6cb.tar.gz |
Restoring authorship annotation for <elshiko@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/retry')
-rw-r--r-- | library/cpp/retry/retry.h | 34 | ||||
-rw-r--r-- | library/cpp/retry/retry_ut.cpp | 80 |
2 files changed, 57 insertions, 57 deletions
diff --git a/library/cpp/retry/retry.h b/library/cpp/retry/retry.h index c47ff5070fe..478eb481972 100644 --- a/library/cpp/retry/retry.h +++ b/library/cpp/retry/retry.h @@ -82,12 +82,12 @@ struct TRetryOptions { }; template <typename TResult, typename TException = yexception> -TMaybe<TResult> DoWithRetry(std::function<TResult()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) { +TMaybe<TResult> DoWithRetry(std::function<TResult()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) { for (ui32 attempt = 0; attempt <= retryOptions.RetryCount; ++attempt) { try { return func(); - } catch (TException& ex) { - onFail(ex); + } catch (TException& ex) { + onFail(ex); if (attempt == retryOptions.RetryCount) { if (throwLast) { throw; @@ -101,27 +101,27 @@ TMaybe<TResult> DoWithRetry(std::function<TResult()> func, std::function<void(co return Nothing(); } -template <typename TResult, typename TException = yexception> -TMaybe<TResult> DoWithRetry(std::function<TResult()> func, TRetryOptions retryOptions, bool throwLast) { - return DoWithRetry<TResult, TException>(func, [](const TException&){}, retryOptions, throwLast); -} - +template <typename TResult, typename TException = yexception> +TMaybe<TResult> DoWithRetry(std::function<TResult()> func, TRetryOptions retryOptions, bool throwLast) { + return DoWithRetry<TResult, TException>(func, [](const TException&){}, retryOptions, throwLast); +} + template <typename TException = yexception> bool DoWithRetry(std::function<void()> func, std::function<void(const TException&)> onFail, TRetryOptions retryOptions, bool throwLast) { - auto f = [&]() { - func(); - return nullptr; - }; - return DoWithRetry<void*, TException>(f, onFail, retryOptions, throwLast).Defined(); -} - -template <typename TException = yexception> + auto f = [&]() { + func(); + return nullptr; + }; + return DoWithRetry<void*, TException>(f, onFail, retryOptions, throwLast).Defined(); +} + +template <typename TException = yexception> bool DoWithRetry(std::function<void()> func, TRetryOptions retryOptions, bool throwLast) { auto f = [&]() { func(); return nullptr; }; - return DoWithRetry<void*, TException>(f, [](const TException&){}, retryOptions, throwLast).Defined(); + return DoWithRetry<void*, TException>(f, [](const TException&){}, retryOptions, throwLast).Defined(); } void DoWithRetry(std::function<void()> func, TRetryOptions retryOptions); diff --git a/library/cpp/retry/retry_ut.cpp b/library/cpp/retry/retry_ut.cpp index 92153e987eb..0d50176bc27 100644 --- a/library/cpp/retry/retry_ut.cpp +++ b/library/cpp/retry/retry_ut.cpp @@ -31,67 +31,67 @@ 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(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(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(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(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(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(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()))); |