Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for min and max backoff time (PR) #510

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sdk/main/src/Client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ Client& Client::setMaxAttempts(uint32_t attempts)
//-----
Client& Client::setMinBackoff(const std::chrono::duration<double>& 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");
}
Expand All @@ -269,7 +270,8 @@ Client& Client::setMinBackoff(const std::chrono::duration<double>& backoff)
//-----
Client& Client::setMaxBackoff(const std::chrono::duration<double>& 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");
}
Expand Down
64 changes: 62 additions & 2 deletions sdk/tests/integration/ClientIntegrationTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@
#include "PublicKey.h"
#include "TransactionReceipt.h"
#include "TransactionResponse.h"
#include "exceptions/UninitializedException.h"

#include <chrono>
#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>
#include <nlohmann/json.hpp>

using json = nlohmann::json;
Expand All @@ -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();
Expand Down Expand Up @@ -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<std::string, AccountId> 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<std::string, AccountId> 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<std::string, AccountId> 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<std::string, AccountId> networkMap;
Client client = Client::forNetwork(networkMap);

// When / Then
EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MIN_BACKOFF));
EXPECT_NO_THROW(client.setMaxBackoff(DEFAULT_MAX_BACKOFF));
}
Loading