diff --git a/sdk/main/src/Client.cc b/sdk/main/src/Client.cc index 7be5995fa..9639c0748 100644 --- a/sdk/main/src/Client.cc +++ b/sdk/main/src/Client.cc @@ -257,7 +257,8 @@ Client& Client::setMaxAttempts(uint32_t attempts) //----- Client& Client::setMinBackoff(const std::chrono::duration& backoff) { - if ((mImpl->mMaxBackoff && backoff > *mImpl->mMaxBackoff) || (!mImpl->mMaxBackoff && backoff > DEFAULT_MAX_BACKOFF)) + if ((mImpl->mMaxBackoff && backoff > *mImpl->mMaxBackoff) || (!mImpl->mMaxBackoff && backoff > DEFAULT_MAX_BACKOFF) || + (!mImpl->mMaxBackoff && backoff < std::chrono::milliseconds(0))) { throw std::invalid_argument("Minimum backoff would be larger than maximum backoff"); } @@ -269,7 +270,8 @@ Client& Client::setMinBackoff(const std::chrono::duration& backoff) //----- Client& Client::setMaxBackoff(const std::chrono::duration& backoff) { - if ((mImpl->mMinBackoff && backoff < *mImpl->mMinBackoff) || (!mImpl->mMinBackoff && backoff < DEFAULT_MIN_BACKOFF)) + if ((mImpl->mMinBackoff && backoff < *mImpl->mMinBackoff) || (!mImpl->mMinBackoff && backoff < DEFAULT_MIN_BACKOFF) || + (!mImpl->mMinBackoff && backoff > DEFAULT_MAX_BACKOFF)) { throw std::invalid_argument("Maximum backoff would be smaller than minimum backoff"); } diff --git a/sdk/tests/integration/ClientIntegrationTest.cc b/sdk/tests/integration/ClientIntegrationTest.cc index ace3f6edf..4112822b3 100644 --- a/sdk/tests/integration/ClientIntegrationTest.cc +++ b/sdk/tests/integration/ClientIntegrationTest.cc @@ -25,12 +25,11 @@ #include "PublicKey.h" #include "TransactionReceipt.h" #include "TransactionResponse.h" -#include "exceptions/UninitializedException.h" +#include #include #include #include -#include #include using json = nlohmann::json; @@ -48,12 +47,22 @@ class ClientIntegrationTest : public ::testing::Test [[nodiscard]] inline const AccountId& getAccountId() const { return mAccountId; } [[nodiscard]] inline const std::string getPathToJSON() const { return mFilePath.string(); } + [[nodiscard]] inline const std::chrono::milliseconds getNegativeBackoffTime() const { return mNegativeBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getZeroBackoffTime() const { return mZeroBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getBelowMinBackoffTime() const { return mBelowMinBackoffTime; } + [[nodiscard]] inline const std::chrono::milliseconds getAboveMaxBackoffTime() const { return mAboveMaxBackoffTime; } + private: const std::string_view mJsonNetworkTag = "network"; const std::string_view mJsonOperatorTag = "operator"; const std::string_view mJsonAccountIdTag = "accountId"; const std::string_view mJsonPrivateKeyTag = "privateKey"; + const std::chrono::milliseconds mNegativeBackoffTime = std::chrono::milliseconds(-1); + const std::chrono::milliseconds mZeroBackoffTime = std::chrono::milliseconds(0); + const std::chrono::milliseconds mBelowMinBackoffTime = DEFAULT_MIN_BACKOFF - std::chrono::milliseconds(1); + const std::chrono::milliseconds mAboveMaxBackoffTime = DEFAULT_MAX_BACKOFF + std::chrono::milliseconds(1); + const std::string_view mAccountIdStr = "0.0.3"; const AccountId mAccountId = AccountId::fromString("0.0.3"); const std::filesystem::path mFilePath = (std::filesystem::current_path() / "local_node.json").string(); @@ -113,3 +122,54 @@ TEST_F(ClientIntegrationTest, ConnectToLocalNode) EXPECT_NE(client.getOperatorPublicKey(), nullptr); EXPECT_FALSE(newAccountId.toString().empty()); } + +//----- +TEST_F(ClientIntegrationTest, SetInvalidMinBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_THROW(client.setMinBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMinBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT +} + +//----- +TEST_F(ClientIntegrationTest, SetValidMinBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_NO_THROW(client.setMinBackoff(getZeroBackoffTime())); + EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MIN_BACKOFF)); + EXPECT_NO_THROW(client.setMinBackoff(DEFAULT_MAX_BACKOFF)); +} + +//----- +TEST_F(ClientIntegrationTest, SetInvalidMaxBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_THROW(client.setMaxBackoff(getNegativeBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getZeroBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getBelowMinBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT + EXPECT_THROW(client.setMaxBackoff(getAboveMaxBackoffTime()), std::invalid_argument); // INVALID_ARGUMENT +} + +//----- +TEST_F(ClientIntegrationTest, SetValidMaxBackoff) +{ + // Given + std::unordered_map networkMap; + Client client = Client::forNetwork(networkMap); + + // When / Then + EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MIN_BACKOFF)); + EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MAX_BACKOFF)); +} \ No newline at end of file