Skip to content

Commit

Permalink
Merge pull request hyperledger-iroha#460 from hyperledger/hotfix/tori…
Browse files Browse the repository at this point in the history
…i-client-test

Hotfix/torii test
  • Loading branch information
grimadas authored Jul 24, 2017
2 parents e15bdc1 + fe88fa1 commit 8542ccb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 66 deletions.
18 changes: 7 additions & 11 deletions irohad/torii/command_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ namespace torii {
/**
* requests tx to a torii server and returns response (blocking, sync)
* @param tx
* @return ToriiResponse
* @param response - returns ToriiResponse if succeeded
* @return grpc::Status - returns connection is success or not.
*/
ToriiResponse CommandSyncClient::Torii(const Transaction& tx) {
ToriiResponse response;
grpc::Status CommandSyncClient::Torii(const Transaction& tx, ToriiResponse& response) {

std::unique_ptr<grpc::ClientAsyncResponseReader<iroha::protocol::ToriiResponse>> rpc(
stub_->AsyncTorii(&context_, tx, &completionQueue_)
Expand All @@ -61,13 +61,7 @@ namespace torii {
assert(got_tag == (void *)static_cast<int>(State::ResponseSent));
assert(ok);

if (status_.ok()) {
return response;
}

response.set_code(iroha::protocol::ResponseCode::FAIL);
response.set_message("RPC failed");
return response;
return status_;
}

/**
Expand All @@ -85,15 +79,17 @@ namespace torii {
* requests tx to a torii server and returns response (non-blocking)
* @param tx
* @param callback
* @return grpc::Status
*/
void CommandAsyncClient::Torii(
grpc::Status CommandAsyncClient::Torii(
const Transaction& tx,
const std::function<void(ToriiResponse& response)>& callback)
{
auto call = new ToriiAsyncClientCall;
call->callback = callback;
call->responseReader = stub_->AsyncTorii(&call->context, tx, &completionQueue_);
call->responseReader->Finish(&call->response, &call->status, (void*)call);
return call->status;
}

/**
Expand Down
10 changes: 6 additions & 4 deletions irohad/torii/command_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ namespace torii {
/**
* requests tx to a torii server and returns response (blocking, sync)
* @param tx
* @return ToriiResponse
* @param response - returns ToriiResponse if succeeded
* @return grpc::Status - returns connection is success or not.
*/
iroha::protocol::ToriiResponse Torii(const iroha::protocol::Transaction& tx);
grpc::Status Torii(const iroha::protocol::Transaction& tx,
iroha::protocol::ToriiResponse& response);

private:
grpc::ClientContext context_;
Expand Down Expand Up @@ -68,9 +70,9 @@ namespace torii {
* Async Torii rpc
* @param tx
* @param callback
* @return grpc::Status
*/
void Torii(const iroha::protocol::Transaction& tx,
const Callback& callback);
grpc::Status Torii(const iroha::protocol::Transaction& tx, const Callback& callback);

private:
/**
Expand Down
4 changes: 1 addition & 3 deletions irohad/torii/query_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,11 @@ namespace torii {
* actual implementation of async Find in QueryService
* @param request - Query
* @param response - QueryResponse
* @return grpc::Status - Status::OK if succeeded. TODO(motxx): grpc::CANCELLED is not supported.
*/
static grpc::Status FindAsync(
static void FindAsync(
iroha::protocol::Query const& request, iroha::protocol::QueryResponse& response) {
response.set_code(iroha::protocol::ResponseCode::OK);
response.set_message("Find async response");
return grpc::Status::OK;
}
};

Expand Down
7 changes: 4 additions & 3 deletions irohad/torii/torii_service_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace torii {
CommandServiceCall<prot::Transaction, prot::ToriiResponse>* call) {

CommandService::ToriiAsync(call->request(), call->response());
call->sendResponse(grpc::Status::OK); // TODO(motxx) currently, grpc::Status::CANCELLED is not supported.
call->sendResponse(grpc::Status::OK);

// Spawn a new Call instance to serve an another client.
enqueueRequest<prot::CommandService::AsyncService, prot::Transaction, prot::ToriiResponse>(
Expand All @@ -111,8 +111,9 @@ namespace torii {
void ToriiServiceHandler::QueryFindHandler(
QueryServiceCall<
iroha::protocol::Query, iroha::protocol::QueryResponse>* call) {
auto stat = QueryService::FindAsync(call->request(), call->response());
call->sendResponse(stat);

QueryService::FindAsync(call->request(), call->response());
call->sendResponse(grpc::Status::OK);

// Spawn a new Call instance to serve an another client.
enqueueRequest<prot::QueryService::AsyncService, prot::Query, prot::QueryResponse>(
Expand Down
22 changes: 7 additions & 15 deletions libs/torii_utils/query_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ limitations under the License.
#include <thread>

namespace torii_utils {

const char* FailureMessage = "RPC failed";


using iroha::protocol::Query;
using iroha::protocol::QueryResponse;

Expand All @@ -35,12 +33,12 @@ namespace torii_utils {
}

/**
* requests tx to a torii server and returns response (blocking, sync)
* @param tx
* @return ToriiResponse
* requests query to a torii server and returns response (blocking, sync)
* @param query
* @param response
* @return grpc::Status
*/
QueryResponse QuerySyncClient::Find(const iroha::protocol::Query &query) {
QueryResponse response;
grpc::Status QuerySyncClient::Find(const iroha::protocol::Query &query, QueryResponse &response) {

std::unique_ptr<grpc::ClientAsyncResponseReader<iroha::protocol::QueryResponse>> rpc(
stub_->AsyncFind(&context_, query, &completionQueue_)
Expand All @@ -63,13 +61,7 @@ namespace torii_utils {
assert(got_tag == (void *)static_cast<int>(State::ResponseSent));
assert(ok);

if (status_.ok()) {
return response;
}

response.set_code(iroha::protocol::ResponseCode::FAIL);
response.set_message(FailureMessage);
return response;
return status_;
}

} // namespace torii
9 changes: 4 additions & 5 deletions libs/torii_utils/query_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ limitations under the License.

namespace torii_utils {

extern const char* FailureMessage;

/**
* CommandSyncClient
*/
Expand All @@ -38,10 +36,11 @@ namespace torii_utils {

/**
* requests query to a torii server and returns response (blocking, sync)
* @param query
* @return QueryResponse
* @param query - contains Query what clients request.
* @param response - QueryResponse that contains what clients want to get.
* @return grpc::Status
*/
iroha::protocol::QueryResponse Find(const iroha::protocol::Query& query);
grpc::Status Find(const iroha::protocol::Query &query, iroha::protocol::QueryResponse &response);

private:
grpc::ClientContext context_;
Expand Down
44 changes: 19 additions & 25 deletions test/module/irohad/torii/torii_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,23 @@ class ToriiServiceTest : public testing::Test {
};

TEST_F(ToriiServiceTest, ToriiWhenBlocking) {
EXPECT_GT(static_cast<int>(iroha::protocol::ResponseCode::OK), 0); // to guarantee ASSERT_EQ works TODO(motxx): More reasonable way.

for (size_t i = 0; i < TimesToriiBlocking; ++i) {
std::cout << i << std::endl;
auto response = torii::CommandSyncClient(Ip, Port)
.Torii(iroha::protocol::Transaction {});
ASSERT_EQ(response.code(), iroha::protocol::ResponseCode::OK);
iroha::protocol::ToriiResponse response;
auto stat = torii::CommandSyncClient(Ip, Port)
.Torii(iroha::protocol::Transaction {}, response);
ASSERT_TRUE(stat.ok());
std::cout << "Sync Response\n";
}
}

TEST_F(ToriiServiceTest, ToriiWhenNonBlocking) {
EXPECT_GT(static_cast<int>(iroha::protocol::ResponseCode::OK), 0);

torii::CommandAsyncClient client(Ip, Port);
std::atomic_int count {0};

for (size_t i = 0; i < TimesToriiNonBlocking; ++i) {
std::cout << i << std::endl;
client.Torii(iroha::protocol::Transaction {},
auto stat = client.Torii(iroha::protocol::Transaction {},
[&count](iroha::protocol::ToriiResponse response){
ASSERT_EQ(response.code(), iroha::protocol::ResponseCode::OK);
std::cout << "Async response\n";
Expand All @@ -87,39 +84,36 @@ TEST_F(ToriiServiceTest, ToriiWhenNonBlocking) {
}

TEST_F(ToriiServiceTest, FindWhereQueryServiceSync) {
EXPECT_GT(iroha::protocol::ResponseCode::OK, 0);

auto response = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {});
ASSERT_TRUE(response.code() == iroha::protocol::ResponseCode::OK);
ASSERT_STRNE(response.message().c_str(), torii_utils::FailureMessage);
iroha::protocol::QueryResponse response;
auto stat = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {}, response);
ASSERT_TRUE(stat.ok());
}

TEST_F(ToriiServiceTest, FindManyTimesWhereQueryServiceSync) {
EXPECT_GT(iroha::protocol::ResponseCode::OK, 0);

for (size_t i = 0; i < TimesFind; ++i) {
auto response = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {});
ASSERT_TRUE(response.code() == iroha::protocol::ResponseCode::OK);
ASSERT_STRNE(response.message().c_str(), torii_utils::FailureMessage);
iroha::protocol::QueryResponse response;
auto stat = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {}, response);
ASSERT_TRUE(stat.ok());
}
}

TEST_F(ToriiServiceTest, MixRPCWhereCommandAndQueryService) {
EXPECT_GT(iroha::protocol::ResponseCode::OK, 0);
torii::CommandAsyncClient client(Ip, Port);

for (size_t i = 0; i < TimesFind; ++i) {
auto response = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {});
ASSERT_TRUE(response.code() == iroha::protocol::ResponseCode::OK);
ASSERT_STRNE(response.message().c_str(), torii_utils::FailureMessage);
iroha::protocol::QueryResponse qresp;
grpc::Status stat;
stat = torii_utils::QuerySyncClient(Ip, Port).Find(iroha::protocol::Query {}, qresp);
ASSERT_TRUE(stat.ok());
client.Torii(iroha::protocol::Transaction {},
[](iroha::protocol::ToriiResponse response){
ASSERT_EQ(response.code(), iroha::protocol::ResponseCode::OK);
std::cout << "Async response\n";
});
auto toriiSyncResponse = torii::CommandSyncClient(Ip, Port)
.Torii(iroha::protocol::Transaction {});
ASSERT_EQ(toriiSyncResponse.code(), iroha::protocol::ResponseCode::OK);
iroha::protocol::ToriiResponse response;
stat = torii::CommandSyncClient(Ip, Port)
.Torii(iroha::protocol::Transaction {}, response);
ASSERT_TRUE(stat.ok());
std::cout << "Sync Response\n";
}
}

0 comments on commit 8542ccb

Please sign in to comment.