Skip to content

Commit

Permalink
Improve comments for torii service test
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l authored and lebdron committed Dec 19, 2017
1 parent 93de28f commit a2341d1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 26 deletions.
31 changes: 16 additions & 15 deletions irohad/torii/impl/command_service.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
/*
Copyright 2017 Soramitsu Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "torii/command_service.hpp"
#include "common/types.hpp"
Expand Down
51 changes: 40 additions & 11 deletions test/module/irohad/torii/torii_service_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class ToriiServiceTest : public testing::Test {
std::shared_ptr<MockStatelessValidator> statelessValidatorMock;
};

/**
* @given torii service and number of transactions
* @when retrieving their status
* @then ensure those are not received
*/
TEST_F(ToriiServiceTest, StatusWhenTxWasNotReceivedBlocking) {
std::vector<iroha::model::Transaction> txs;
std::vector<std::string> tx_hashes;
Expand Down Expand Up @@ -173,12 +178,17 @@ TEST_F(ToriiServiceTest, StatusWhenTxWasNotReceivedBlocking) {
}

/**
* That test simulates the behavior of the Torii.
* Instead of real implementation fake PCS is used.
* In fake PCS instead of being subscribed to ordering service and simulator
* we create our own rxcpp::subjects and subscribe on them.
* During the test we supply that subjects with transactions or commits
* just like ordering service or simulator would do
* That test simulates the real behavior of the blocking Torii.
*
* @given torii service with mocked CommunicationService
* that is subscribed on custom rxcpp::subjects
* @when sending some number of transactions to the Torii
* @then ensure it perform as real one:
* - Torii returns ok status
* - Proper txs responses in Status are STATELESS_VALIDATION_SUCCESS,
* then STATEFUL_VALIDATION_SUCCESS and COMMITTED (order matters)
* - Tx that not in a block returns STATELESS_VALIDATION_SUCCESS and
then STATEFUL_VALIDATION_FAILED
*/
TEST_F(ToriiServiceTest, StatusWhenBlocking) {
EXPECT_CALL(*statelessValidatorMock,
Expand Down Expand Up @@ -270,9 +280,20 @@ TEST_F(ToriiServiceTest, StatusWhenBlocking) {
iroha::protocol::TxStatus::STATEFUL_VALIDATION_FAILED);
}

/**
* Note that this test assume that Torii is non-blocking so it waits
* for some time if it required thus on failing may hang
*
* @given torii service with mocked CommunicationService
* that is subscribed on custom rxcpp::subjects
* @when sending some number of transactions to the Torii
* @then txs responses in Status are STATELESS_VALIDATION_SUCCESS,
* then STATEFUL_VALIDATION_SUCCESS and COMMITTED (order matters)
*/
TEST_F(ToriiServiceTest, StatusWhenNonBlocking) {
torii::CommandAsyncClient client(Ip, Port);
std::atomic_int torii_count{0}; // counts how many times torii was invoked
// counts how many times torii was invoked
std::atomic_int torii_count{0};

EXPECT_CALL(*statelessValidatorMock,
validate(A<const iroha::model::Transaction &>()))
Expand Down Expand Up @@ -303,8 +324,8 @@ TEST_F(ToriiServiceTest, StatusWhenNonBlocking) {
;
ASSERT_EQ(torii_count, TimesToriiNonBlocking);

std::atomic_int status_counter{
0}; // counts how many times statuses of txs were invoked
// counts how many times statuses of txs were invoked
std::atomic_int status_counter{0};

// create proposal from these transactions
iroha::model::Proposal proposal(txs);
Expand Down Expand Up @@ -372,13 +393,19 @@ TEST_F(ToriiServiceTest, StatusWhenNonBlocking) {
ASSERT_EQ(status_counter, TimesToriiNonBlocking);
}

/**
* @given torii service and some number of transactions with hashes
* @when sending request on this txs
* @then ensure that response has the same hash as sent tx
*/
TEST_F(ToriiServiceTest, CheckHash) {
std::vector<iroha::model::Transaction> txs;
// given
std::vector<std::string> tx_hashes;
const int tx_num = 10;

iroha::model::converters::PbTransactionFactory tx_factory;

// create transactions, but do not send them
for (size_t i = 0; i < tx_num; ++i) {
auto new_tx = iroha::protocol::Transaction();
auto payload = new_tx.mutable_payload();
Expand All @@ -388,12 +415,14 @@ TEST_F(ToriiServiceTest, CheckHash) {
tx_hashes.push_back(iroha::hash(*tx).to_string());
}

// get statuses of transactions
for (auto &hash : tx_hashes) {
iroha::protocol::TxStatusRequest tx_request;
tx_request.set_tx_hash(hash);
iroha::protocol::ToriiResponse toriiResponse;
// when
torii::CommandSyncClient(Ip, Port).Status(tx_request, toriiResponse);

// then
ASSERT_EQ(toriiResponse.tx_hash(), hash);
}
}

0 comments on commit a2341d1

Please sign in to comment.