Skip to content

Commit

Permalink
Refactor torii_service_test (hyperledger-iroha#1853)
Browse files Browse the repository at this point in the history
- Rework deserializeTransaction with for-each, since it calls pipeline
callback twice for some reason
- Use Mocks for tx validator and tx batch validator
- Remove transactions, where useless
- Remove redundant stuff from CommandSyncClient

Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l authored Dec 27, 2018
1 parent 59da405 commit f8a51dd
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 82 deletions.
9 changes: 0 additions & 9 deletions irohad/torii/command_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ namespace torii {
size_t port,
logger::Logger log = logger::log("CommandSyncClient"));

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

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

/**
* requests tx to a torii server and returns response (blocking, sync)
* @param tx
Expand Down Expand Up @@ -63,9 +57,6 @@ namespace torii {
std::vector<iroha::protocol::ToriiResponse> &response) const;

private:
void swap(CommandSyncClient &lhs, CommandSyncClient &rhs);
std::string ip_;
size_t port_;
std::unique_ptr<iroha::protocol::CommandService_v1::Stub> stub_;
logger::Logger log_;
};
Expand Down
30 changes: 1 addition & 29 deletions irohad/torii/impl/command_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,10 @@ namespace torii {
CommandSyncClient::CommandSyncClient(const std::string &ip,
size_t port,
logger::Logger log)
: ip_(ip),
port_(port),
stub_(iroha::network::createClient<iroha::protocol::CommandService_v1>(
: stub_(iroha::network::createClient<iroha::protocol::CommandService_v1>(
ip + ":" + std::to_string(port))),
log_(std::move(log)) {}

CommandSyncClient::CommandSyncClient(const CommandSyncClient &rhs)
: CommandSyncClient(rhs.ip_, rhs.port_, rhs.log_) {}

CommandSyncClient &CommandSyncClient::operator=(CommandSyncClient rhs) {
swap(*this, rhs);
return *this;
}

CommandSyncClient::CommandSyncClient(CommandSyncClient &&rhs) noexcept {
swap(*this, rhs);
}

CommandSyncClient &CommandSyncClient::operator=(
CommandSyncClient &&rhs) noexcept {
swap(*this, rhs);
return *this;
}

grpc::Status CommandSyncClient::Torii(const Transaction &tx) const {
google::protobuf::Empty a;
grpc::ClientContext context;
Expand Down Expand Up @@ -80,12 +60,4 @@ namespace torii {
reader->Finish();
}

void CommandSyncClient::swap(CommandSyncClient &lhs, CommandSyncClient &rhs) {
using std::swap;
swap(lhs.ip_, rhs.ip_);
swap(lhs.port_, rhs.port_);
swap(lhs.stub_, rhs.stub_);
swap(lhs.log_, rhs.log_);
}

} // namespace torii
42 changes: 16 additions & 26 deletions irohad/torii/impl/command_service_transport_grpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,22 @@ namespace torii {
shared_model::interface::types::SharedTxsCollectionType
CommandServiceTransportGrpc::deserializeTransactions(
const iroha::protocol::TxList *request) {
return boost::copy_range<
shared_model::interface::types::SharedTxsCollectionType>(
request->transactions()
| boost::adaptors::transformed(
[&](const auto &tx) { return transaction_factory_->build(tx); })
| boost::adaptors::filtered([&](const auto &result) {
return result.match(
[](const iroha::expected::Value<
std::unique_ptr<shared_model::interface::Transaction>> &) {
return true;
},
[&](const iroha::expected::Error<TransportFactoryType::Error>
&error) {
status_bus_->publish(status_factory_->makeStatelessFail(
error.error.hash,
shared_model::interface::TxStatusFactory::
TransactionError{error.error.error, 0, 0}));
return false;
});
})
| boost::adaptors::transformed([&](auto result) {
return std::move(
boost::get<iroha::expected::ValueOf<decltype(result)>>(
result))
.value;
}));
shared_model::interface::types::SharedTxsCollectionType tx_collection;
for (const auto &tx : request->transactions()) {
transaction_factory_->build(tx).match(
[&tx_collection](
iroha::expected::Value<
std::unique_ptr<shared_model::interface::Transaction>> &v) {
tx_collection.emplace_back(std::move(v).value);
},
[this](iroha::expected::Error<TransportFactoryType::Error> &error) {
status_bus_->publish(status_factory_->makeStatelessFail(
error.error.hash,
shared_model::interface::TxStatusFactory::TransactionError{
error.error.error, 0, 0}));
});
}
return tx_collection;
}

grpc::Status CommandServiceTransportGrpc::ListTorii(
Expand Down
18 changes: 11 additions & 7 deletions test/module/irohad/torii/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@
add_subdirectory(processor)

# command service test
addtest(torii_service_test torii_service_test.cpp)
target_link_libraries(torii_service_test
addtest(torii_transport_command_test torii_transport_command_test.cpp)
target_link_libraries(torii_transport_command_test
torii_service
command_client
query_client
server_runner
processors
consensus_round
on_demand_common
)

addtest(torii_queries_test torii_queries_test.cpp)
Expand All @@ -38,3 +33,12 @@ target_link_libraries(torii_service_query_test
server_runner
query_client
)

addtest(command_sync_client_test
command_sync_client_test.cpp
)
target_link_libraries(command_sync_client_test
command_client
server_runner
endpoint
)
100 changes: 100 additions & 0 deletions test/module/irohad/torii/command_sync_client_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "torii/command_client.hpp"

#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "endpoint_mock.grpc.pb.h"
#include "main/server_runner.hpp"
#include "module/irohad/torii/torii_mocks.hpp"

using testing::_;
using testing::Invoke;
using testing::Return;

class CommandSyncClientTest : public testing::Test {
public:
void SetUp() override {
runner = std::make_unique<ServerRunner>(ip + ":0");
server = std::make_shared<iroha::torii::MockCommandServiceTransport>();
runner->append(server).run().match(
[this](iroha::expected::Value<int> port) { this->port = port.value; },
[](iroha::expected::Error<std::string> err) { FAIL() << err.error; });
}

std::unique_ptr<ServerRunner> runner;
std::shared_ptr<iroha::torii::MockCommandServiceTransport> server;

const std::string ip = "127.0.0.1";
const size_t kHashLength = 32;
int port;
};

/**
* @given command client
* @when Status is called
* @then the same method of the server is called and client successfully return
*/
TEST_F(CommandSyncClientTest, Status) {
iroha::protocol::TxStatusRequest tx_request;
tx_request.set_tx_hash(std::string(kHashLength, '1'));
iroha::protocol::ToriiResponse toriiResponse;

torii::CommandSyncClient client(ip, port);
EXPECT_CALL(*server, Status(_, _, _)).WillOnce(Return(grpc::Status::OK));
auto stat = client.Status(tx_request, toriiResponse);
ASSERT_TRUE(stat.ok());
}

/**
* @given command client
* @when Torii is called
* @then the same method of the server is called and client successfully return
*/
TEST_F(CommandSyncClientTest, Torii) {
iroha::protocol::Transaction tx;
EXPECT_CALL(*server, Torii(_, _, _)).WillOnce(Return(grpc::Status()));
torii::CommandSyncClient client(ip, port);
auto stat = client.Torii(tx);
ASSERT_TRUE(stat.ok());
}

/**
* @given command client
* @when ListTorii is called
* @then the same method of the server is called and client successfully return
*/
TEST_F(CommandSyncClientTest, ListTorii) {
iroha::protocol::TxList tx;
EXPECT_CALL(*server, ListTorii(_, _, _)).WillOnce(Return(grpc::Status()));
torii::CommandSyncClient client(ip, port);
auto stat = client.ListTorii(tx);
ASSERT_TRUE(stat.ok());
}

/**
* @given command client
* @when StatusStream is called
* @then the same method of the server is called and client successfully return
*/
TEST_F(CommandSyncClientTest, StatusStream) {
iroha::protocol::TxStatusRequest tx;
iroha::protocol::ToriiResponse resp;
resp.set_tx_hash(std::string(kHashLength, '1'));
std::vector<iroha::protocol::ToriiResponse> responses;
EXPECT_CALL(*server, StatusStream(_, _, _))
.WillOnce(Invoke([&](auto,
auto,
grpc::ServerWriter<iroha::protocol::ToriiResponse>
*response_writer) {
response_writer->Write(resp);
return grpc::Status();
}));
torii::CommandSyncClient client(ip, port);
client.StatusStream(tx, responses);
ASSERT_EQ(responses.size(), 1);
ASSERT_EQ(responses[0].tx_hash(), resp.tx_hash());
}
42 changes: 42 additions & 0 deletions test/module/irohad/torii/torii_mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@

#include <gmock/gmock.h>

#include "endpoint.grpc.pb.h"
#include "endpoint.pb.h"
#include "interfaces/query_responses/block_query_response.hpp"
#include "interfaces/query_responses/query_response.hpp"
#include "torii/command_service.hpp"
#include "torii/processor/query_processor.hpp"
#include "torii/status_bus.hpp"

Expand All @@ -33,6 +36,45 @@ namespace iroha {
MOCK_METHOD1(publish, void(StatusBus::Objects));
MOCK_METHOD0(statuses, rxcpp::observable<StatusBus::Objects>());
};

class MockCommandServiceTransport
: public iroha::protocol::CommandService_v1::Service {
public:
MOCK_METHOD3(Torii,
grpc::Status(grpc::ServerContext *,
const iroha::protocol::Transaction *,
google::protobuf::Empty *));
MOCK_METHOD3(ListTorii,
grpc::Status(grpc::ServerContext *,
const iroha::protocol::TxList *,
google::protobuf::Empty *));
MOCK_METHOD3(Status,
grpc::Status(grpc::ServerContext *,
const iroha::protocol::TxStatusRequest *,
iroha::protocol::ToriiResponse *));
MOCK_METHOD3(
StatusStream,
grpc::Status(grpc::ServerContext *,
const iroha::protocol::TxStatusRequest *,
grpc::ServerWriter<iroha::protocol::ToriiResponse> *));
};

class MockCommandService : public ::torii::CommandService {
public:
MOCK_METHOD1(handleTransactionBatch,
void(std::shared_ptr<
shared_model::interface::TransactionBatch> batch));
MOCK_METHOD1(
getStatus,
std::shared_ptr<shared_model::interface::TransactionResponse>(
const shared_model::crypto::Hash &request));
MOCK_METHOD1(
getStatusStream,
rxcpp::observable<
std::shared_ptr<shared_model::interface::TransactionResponse>>(
const shared_model::crypto::Hash &hash));
};

} // namespace torii
} // namespace iroha

Expand Down
Loading

0 comments on commit f8a51dd

Please sign in to comment.