Skip to content

Commit

Permalink
Add move ctor and move assignment operator
Browse files Browse the repository at this point in the history
Write tests for clients

Signed-off-by: dumitru <[email protected]>
  • Loading branch information
x3medima17 committed Feb 2, 2018
1 parent 5be928f commit e461c26
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 11 deletions.
12 changes: 12 additions & 0 deletions irohad/torii/command_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,16 @@ namespace torii {
return stub_->Status(&context, request, &response);
}

CommandSyncClient::CommandSyncClient(CommandSyncClient &&rhs)
: ip_(std::move(rhs.ip_)),
port_(rhs.port_),
stub_(std::move(rhs.stub_)) {}

CommandSyncClient& CommandSyncClient::operator=(CommandSyncClient &&rhs) {
this->ip_ = std::move(rhs.ip_);
this->port_ = rhs.port_;
this->stub_ = std::move(rhs.stub_);
return *this;
}

} // namespace torii
5 changes: 4 additions & 1 deletion irohad/torii/command_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ namespace torii {
class CommandSyncClient {
public:
CommandSyncClient(const std::string& ip, size_t port);

CommandSyncClient(const CommandSyncClient&);
CommandSyncClient& operator=(const CommandSyncClient&);

CommandSyncClient(CommandSyncClient&&);
CommandSyncClient&operator=(CommandSyncClient&&);

/**
* requests tx to a torii server and returns response (blocking, sync)
* @param tx
Expand All @@ -52,7 +56,6 @@ namespace torii {
std::string ip_;
size_t port_;
std::unique_ptr<iroha::protocol::CommandService::Stub> stub_;

};


Expand Down
15 changes: 13 additions & 2 deletions irohad/torii/query_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ limitations under the License.
#include <block.pb.h>
#include <grpc++/grpc++.h>
#include <thread>
#include <torii/torii_service_handler.hpp>
#include <torii/query_client.hpp>

#include "torii/query_client.hpp"
#include "torii/torii_service_handler.hpp"

namespace torii_utils {

Expand All @@ -41,6 +42,16 @@ namespace torii_utils {
return *this;
}

QuerySyncClient::QuerySyncClient(QuerySyncClient &&rhs)
: ip_(std::move(rhs.ip_)), port_(rhs.port_), stub_(std::move(rhs.stub_)) {}

QuerySyncClient& QuerySyncClient::operator=(QuerySyncClient &&rhs) {
this->ip_ = std::move(rhs.ip_);
this->port_ = rhs.port_;
this->stub_ = std::move(rhs.stub_);
return *this;
}

/**
* requests query to a torii server and returns response (blocking, sync)
* @param query
Expand Down
6 changes: 5 additions & 1 deletion irohad/torii/query_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ namespace torii_utils {
class QuerySyncClient {
public:
QuerySyncClient(const std::string &ip, size_t port);

QuerySyncClient(const QuerySyncClient&);
QuerySyncClient& operator=(const QuerySyncClient&);

QuerySyncClient(QuerySyncClient&&);
QuerySyncClient&operator=(QuerySyncClient&&);

/**
* requests query to a torii server and returns response (blocking, sync)
* @param query - contains Query what clients request.
Expand All @@ -48,7 +53,6 @@ namespace torii_utils {
size_t port_;
std::unique_ptr<iroha::protocol::QueryService::Stub> stub_;
};

/**
* QueryAsyncClient
Expand Down
34 changes: 28 additions & 6 deletions test/module/irohad/torii/torii_queries_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,33 @@ class ToriiQueriesTest : public testing::Test {
};

/**
* Test for error response
* Test Query Client
*/
TEST_F(ToriiQueriesTest, QueryClient) {
iroha::protocol::QueryResponse response;
auto query = iroha::protocol::Query();

query.mutable_payload()->set_creator_account_id("accountA");
query.mutable_payload()->mutable_get_account()->set_account_id("accountB");
query.mutable_signature()->set_pubkey(pubkey_test);
query.mutable_signature()->set_signature(signature_test);

auto client1 = torii_utils::QuerySyncClient(Ip, Port);
//Copy ctor
torii_utils::QuerySyncClient client2(client1);
//copy assignment
auto client3 = client2;
//move ctor
torii_utils::QuerySyncClient client4(std::move(client3));
//move assignment
auto client5 = std::move(client4);
auto stat = client5.Find(query, response);
ASSERT_TRUE(stat.ok());
}

/**
* Test for error response
*/
TEST_F(ToriiQueriesTest, FindWhenResponseInvalid) {
EXPECT_CALL(*statelessValidatorMock,
validate(A<const iroha::model::Query &>()))
Expand Down Expand Up @@ -525,10 +549,8 @@ TEST_F(ToriiQueriesTest, FindManyTimesWhereQueryServiceSync) {
validate(A<const iroha::model::Query &>()))
.WillOnce(Return(false));

//Testing copy constructor and copy assignment operator
auto client1 = torii_utils::QuerySyncClient(Ip, Port);
auto client2 = client1;
auto client3 = torii_utils::QuerySyncClient(client2);
auto client = torii_utils::QuerySyncClient(Ip, Port);

for (size_t i = 0; i < TimesFind; ++i) {
iroha::protocol::QueryResponse response;
auto query = iroha::protocol::Query();
Expand All @@ -539,7 +561,7 @@ TEST_F(ToriiQueriesTest, FindManyTimesWhereQueryServiceSync) {
query.mutable_signature()->set_pubkey(pubkey_test);
query.mutable_signature()->set_signature(signature_test);

auto stat = client3.Find(query, response);
auto stat = client.Find(query, response);
ASSERT_TRUE(stat.ok());
// Must return Error Response
ASSERT_EQ(response.error_response().reason(),
Expand Down
25 changes: 24 additions & 1 deletion test/module/irohad/torii/torii_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ class ToriiServiceTest : public testing::Test {
std::shared_ptr<MockStatelessValidator> statelessValidatorMock;
};


/**
* @given chain of CommandClient copies and moves
* @when status is asked
* @then grpc returns ok
*/
TEST_F(ToriiServiceTest, CommandClient) {

iroha::protocol::TxStatusRequest tx_request;
iroha::protocol::ToriiResponse toriiResponse;

auto client1 = torii::CommandSyncClient(Ip, Port);
//Copy ctor
torii::CommandSyncClient client2(client1);
//copy assignment
auto client3 = client2;
//move ctor
torii::CommandSyncClient client4(std::move(client3));
//move assignment
auto client5 = std::move(client4);
auto stat = client5.Status(tx_request, toriiResponse);

ASSERT_TRUE(stat.ok());
}
/**
* @given torii service and number of transactions
* @when retrieving their status
Expand Down Expand Up @@ -323,4 +347,3 @@ TEST_F(ToriiServiceTest, CheckHash) {
ASSERT_EQ(toriiResponse.tx_hash(), hash);
}
}

0 comments on commit e461c26

Please sign in to comment.