From 44345ce4c91f4b7d52a929ae4962963482ddc5cd Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 01:48:54 +0000 Subject: [PATCH 01/11] Init unit test suite for all unit test apps Move the init_unit_test_suite(...) function to a hpp file and include it in every unit test app: - app_test - chain_test - cli_test - es_test - performance_test --- tests/app/main.cpp | 5 +-- tests/cli/main.cpp | 5 +-- tests/common/init_unit_test_suite.hpp | 42 +++++++++++++++++++++++++ tests/elasticsearch/main.cpp | 3 +- tests/performance/performance_tests.cpp | 8 +---- tests/tests/main.cpp | 20 +----------- 6 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 tests/common/init_unit_test_suite.hpp diff --git a/tests/app/main.cpp b/tests/app/main.cpp index a0d1b9b168..6afc8969a8 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -42,11 +42,12 @@ #include "../../libraries/app/application_impl.hxx" -#define BOOST_TEST_MODULE Test Application -#include +#include "../common/init_unit_test_suite.hpp" #include "../common/genesis_file_util.hpp" +uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; // needed for compiling, not used + using namespace graphene; namespace bpo = boost::program_options; diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index cdaa707e3b..c0f3396ee5 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -60,8 +60,9 @@ #include -#define BOOST_TEST_MODULE Test Application -#include +#include "../common/init_unit_test_suite.hpp" + +uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; // needed for compiling, not used /***** * Global Initialization for Windows diff --git a/tests/common/init_unit_test_suite.hpp b/tests/common/init_unit_test_suite.hpp new file mode 100644 index 0000000000..0c3e9f7644 --- /dev/null +++ b/tests/common/init_unit_test_suite.hpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +#include +#include +#include +#include + +extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP; + +boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { + const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); + std::srand( seed ); + std::cout << "Random number generator seeded to " << seed << std::endl; + const char* genesis_timestamp_str = getenv("GRAPHENE_TESTING_GENESIS_TIMESTAMP"); + if( genesis_timestamp_str != nullptr ) + { + GRAPHENE_TESTING_GENESIS_TIMESTAMP = std::stoul( genesis_timestamp_str ); + } + std::cout << "GRAPHENE_TESTING_GENESIS_TIMESTAMP is " << GRAPHENE_TESTING_GENESIS_TIMESTAMP << std::endl; + return nullptr; +} diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 2935a047d8..2b1836a31b 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -31,8 +31,7 @@ #include "../common/database_fixture.hpp" -#define BOOST_TEST_MODULE Elastic Search Database Tests -#include +#include "../common/init_unit_test_suite.hpp" #ifdef NDEBUG #define ES_WAIT_TIME (fc::milliseconds(1000)) diff --git a/tests/performance/performance_tests.cpp b/tests/performance/performance_tests.cpp index 152f672189..35d0a1b7cc 100644 --- a/tests/performance/performance_tests.cpp +++ b/tests/performance/performance_tests.cpp @@ -22,13 +22,7 @@ * THE SOFTWARE. */ -#include - -boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { - std::srand(time(NULL)); - std::cout << "Random number generator seeded to " << time(NULL) << std::endl; - return nullptr; -} +#include "../common/init_unit_test_suite.hpp" #include diff --git a/tests/tests/main.cpp b/tests/tests/main.cpp index 0c3e9f7644..48ba3b7e4b 100644 --- a/tests/tests/main.cpp +++ b/tests/tests/main.cpp @@ -21,22 +21,4 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ -#include -#include -#include -#include - -extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP; - -boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { - const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); - std::srand( seed ); - std::cout << "Random number generator seeded to " << seed << std::endl; - const char* genesis_timestamp_str = getenv("GRAPHENE_TESTING_GENESIS_TIMESTAMP"); - if( genesis_timestamp_str != nullptr ) - { - GRAPHENE_TESTING_GENESIS_TIMESTAMP = std::stoul( genesis_timestamp_str ); - } - std::cout << "GRAPHENE_TESTING_GENESIS_TIMESTAMP is " << GRAPHENE_TESTING_GENESIS_TIMESTAMP << std::endl; - return nullptr; -} +#include "../common/init_unit_test_suite.hpp" From 9de407b788023f8df96361e5007ac90660edbf85 Mon Sep 17 00:00:00 2001 From: Abit Date: Sat, 6 Mar 2021 04:13:08 +0100 Subject: [PATCH 02/11] Wait longer in es_test, and fix crashes --- tests/elasticsearch/main.cpp | 76 ++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 38 deletions(-) diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 2b1836a31b..96efef7358 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -34,9 +34,9 @@ #include "../common/init_unit_test_suite.hpp" #ifdef NDEBUG - #define ES_WAIT_TIME (fc::milliseconds(1000)) + #define ES_WAIT_TIME (fc::milliseconds(2000)) #else - #define ES_WAIT_TIME (fc::milliseconds(3000)) + #define ES_WAIT_TIME (fc::milliseconds(5000)) #endif using namespace graphene::chain; @@ -250,7 +250,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 0, 4, 9) = { 5, 3, 1, 0 } auto histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(9)); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); @@ -258,7 +258,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 0, 4, 6) = { 5, 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(6)); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); @@ -266,7 +266,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 0, 4, 5) = { 5, 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(5)); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); @@ -274,33 +274,33 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 0, 4, 4) = { 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(4)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 0u); // f(A, 0, 4, 3) = { 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(3)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 0u); // f(A, 0, 4, 2) = { 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(2)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 0u); // f(A, 0, 4, 1) = { 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(1)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 0u); // f(A, 0, 4, 0) = { 5, 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type()); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); @@ -308,30 +308,30 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 1, 5, 9) = { 5, 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(9)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); // f(A, 1, 5, 6) = { 5, 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(6)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); // f(A, 1, 5, 5) = { 5, 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(5)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); // f(A, 1, 5, 4) = { 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(4)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); // f(A, 1, 5, 3) = { 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(3)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); // f(A, 1, 5, 2) = { } @@ -344,67 +344,67 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 1, 5, 0) = { 5, 3 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(1), 5, operation_history_id_type(0)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); // f(A, 0, 3, 9) = { 5, 3, 1 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(9)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(A, 0, 3, 6) = { 5, 3, 1 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(6)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(A, 0, 3, 5) = { 5, 3, 1 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(5)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(A, 0, 3, 4) = { 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(4)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 0u); // f(A, 0, 3, 3) = { 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(3)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 0u); // f(A, 0, 3, 2) = { 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(2)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 0u); // f(A, 0, 3, 1) = { 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type(1)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 1u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 0u); // f(A, 0, 3, 0) = { 5, 3, 1 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 3, operation_history_id_type()); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(B, 0, 4, 9) = { 6, 4, 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(9)); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 2u); @@ -412,7 +412,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(B, 0, 4, 6) = { 6, 4, 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(6)); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 2u); @@ -420,38 +420,38 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(B, 0, 4, 5) = { 4, 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(5)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 4u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 2u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(B, 0, 4, 4) = { 4, 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(4)); - BOOST_CHECK_EQUAL(histories.size(), 3u); + BOOST_REQUIRE_EQUAL(histories.size(), 3u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 4u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 2u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 1u); // f(B, 0, 4, 3) = { 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(3)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 2u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); // f(B, 0, 4, 2) = { 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(2)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 2u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 1u); // f(B, 0, 4, 1) = { 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type(1)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 1u); // f(B, 0, 4, 0) = { 6, 4, 2, 1 } histories = hist_api.get_account_history("dan", operation_history_id_type(), 4, operation_history_id_type()); - BOOST_CHECK_EQUAL(histories.size(), 4u); + BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 2u); @@ -459,24 +459,24 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(B, 2, 4, 9) = { 6, 4 } histories = hist_api.get_account_history("dan", operation_history_id_type(2), 4, operation_history_id_type(9)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); // f(B, 2, 4, 6) = { 6, 4 } histories = hist_api.get_account_history("dan", operation_history_id_type(2), 4, operation_history_id_type(6)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); // f(B, 2, 4, 5) = { 4 } histories = hist_api.get_account_history("dan", operation_history_id_type(2), 4, operation_history_id_type(5)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 4u); // f(B, 2, 4, 4) = { 4 } histories = hist_api.get_account_history("dan", operation_history_id_type(2), 4, operation_history_id_type(4)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 4u); // f(B, 2, 4, 3) = { } @@ -493,7 +493,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(B, 2, 4, 0) = { 6, 4 } histories = hist_api.get_account_history("dan", operation_history_id_type(2), 4, operation_history_id_type(0)); - BOOST_CHECK_EQUAL(histories.size(), 2u); + BOOST_REQUIRE_EQUAL(histories.size(), 2u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 6u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 4u); @@ -524,7 +524,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { // f(A, 0, 10, 0) = { 7, 5, 3, 1, 0 } histories = hist_api.get_account_history("1.2.0", operation_history_id_type(0), 10, operation_history_id_type(0)); - BOOST_CHECK_EQUAL(histories.size(), 5u); + BOOST_REQUIRE_EQUAL(histories.size(), 5u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 7u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[2].id.instance(), 3u); From be285dc28c6452228b8698b0e7005ceb592bdcba Mon Sep 17 00:00:00 2001 From: Abit Date: Sat, 6 Mar 2021 04:44:17 +0100 Subject: [PATCH 03/11] Require successful deletion in es_test --- tests/elasticsearch/main.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 96efef7358..58ea00c07c 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -61,6 +61,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { auto delete_account_history = graphene::utilities::deleteAll(es); fc::usleep(ES_WAIT_TIME); // this is because index.refresh_interval, nothing to worry + BOOST_REQUIRE(delete_account_history); // require successful deletion if(delete_account_history) { // all records deleted //account_id_type() do 3 ops @@ -152,6 +153,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) { generate_block(); fc::usleep(ES_WAIT_TIME); + BOOST_REQUIRE(delete_objects); // require successful deletion if(delete_objects) { // all records deleted // asset and bitasset @@ -200,9 +202,11 @@ BOOST_AUTO_TEST_CASE(elasticsearch_suite) { es.elasticsearch_url = "http://localhost:9200/"; es.index_prefix = es_index_prefix; auto delete_account_history = graphene::utilities::deleteAll(es); + BOOST_REQUIRE(delete_account_history); // require successful deletion fc::usleep(ES_WAIT_TIME); es.index_prefix = es_obj_index_prefix; auto delete_objects = graphene::utilities::deleteAll(es); + BOOST_REQUIRE(delete_objects); // require successful deletion fc::usleep(ES_WAIT_TIME); if(delete_account_history && delete_objects) { // all records deleted @@ -227,6 +231,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { es.index_prefix = es_index_prefix; auto delete_account_history = graphene::utilities::deleteAll(es); + BOOST_REQUIRE(delete_account_history); // require successful deletion generate_block(); fc::usleep(ES_WAIT_TIME); From 99e1453e16c66970dd3100b4c328ad16557d59f2 Mon Sep 17 00:00:00 2001 From: Abit Date: Sat, 6 Mar 2021 05:06:13 +0100 Subject: [PATCH 04/11] Wait even longer for the first writes in es_test --- tests/elasticsearch/main.cpp | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 58ea00c07c..901021b018 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -34,9 +34,11 @@ #include "../common/init_unit_test_suite.hpp" #ifdef NDEBUG - #define ES_WAIT_TIME (fc::milliseconds(2000)) + #define ES_FIRST_WAIT_TIME (fc::milliseconds(3000)) + #define ES_WAIT_TIME (fc::milliseconds(1000)) #else - #define ES_WAIT_TIME (fc::milliseconds(5000)) + #define ES_FIRST_WAIT_TIME (fc::milliseconds(6000)) + #define ES_WAIT_TIME (fc::milliseconds(3000)) #endif using namespace graphene::chain; @@ -55,11 +57,9 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { es.curl = curl; es.elasticsearch_url = "http://localhost:9200/"; es.index_prefix = es_index_prefix; - //es.auth = "elastic:changeme"; // delete all first auto delete_account_history = graphene::utilities::deleteAll(es); - fc::usleep(ES_WAIT_TIME); // this is because index.refresh_interval, nothing to worry BOOST_REQUIRE(delete_account_history); // require successful deletion if(delete_account_history) { // all records deleted @@ -70,11 +70,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { auto bob = create_account("bob"); generate_block(); - fc::usleep(ES_WAIT_TIME); - - // for later use - //int asset_create_op_id = operation::tag::value; - //int account_create_op_id = operation::tag::value; + fc::usleep(ES_FIRST_WAIT_TIME); // this is because index.refresh_interval, nothing to worry string query = "{ \"query\" : { \"bool\" : { \"must\" : [{\"match_all\": {}}] } } }"; es.endpoint = es.index_prefix + "*/data/_count"; @@ -145,13 +141,11 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) { es.curl = curl; es.elasticsearch_url = "http://localhost:9200/"; es.index_prefix = es_obj_index_prefix; - //es.auth = "elastic:changeme"; // delete all first auto delete_objects = graphene::utilities::deleteAll(es); generate_block(); - fc::usleep(ES_WAIT_TIME); BOOST_REQUIRE(delete_objects); // require successful deletion if(delete_objects) { // all records deleted @@ -159,7 +153,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) { // asset and bitasset create_bitasset("USD", account_id_type()); generate_block(); - fc::usleep(ES_WAIT_TIME); + fc::usleep(ES_FIRST_WAIT_TIME); string query = "{ \"query\" : { \"bool\" : { \"must\" : [{\"match_all\": {}}] } } }"; es.endpoint = es.index_prefix + "*/data/_count"; @@ -203,11 +197,13 @@ BOOST_AUTO_TEST_CASE(elasticsearch_suite) { es.index_prefix = es_index_prefix; auto delete_account_history = graphene::utilities::deleteAll(es); BOOST_REQUIRE(delete_account_history); // require successful deletion - fc::usleep(ES_WAIT_TIME); - es.index_prefix = es_obj_index_prefix; - auto delete_objects = graphene::utilities::deleteAll(es); + + graphene::utilities::ES es_obj; + es_obj.curl = curl; + es_obj.elasticsearch_url = "http://localhost:9200/"; + es_obj.index_prefix = es_obj_index_prefix; + auto delete_objects = graphene::utilities::deleteAll(es_obj); BOOST_REQUIRE(delete_objects); // require successful deletion - fc::usleep(ES_WAIT_TIME); if(delete_account_history && delete_objects) { // all records deleted @@ -234,7 +230,6 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { BOOST_REQUIRE(delete_account_history); // require successful deletion generate_block(); - fc::usleep(ES_WAIT_TIME); if(delete_account_history) { @@ -247,7 +242,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { create_bitasset("OIL", dan.id); // create op 6 generate_block(); - fc::usleep(ES_WAIT_TIME); + fc::usleep(ES_FIRST_WAIT_TIME); graphene::app::history_api hist_api(app); app.enable_plugin("elasticsearch"); From 8304df47427a6e0621fb5537e852b40948789659 Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 14:57:48 +0000 Subject: [PATCH 05/11] Tweak ES settings before test in Github Actions --- .github/workflows/build-and-test.ubuntu-debug.yml | 4 ++++ .github/workflows/build-and-test.ubuntu-release.yml | 4 ++++ .github/workflows/sonar-scan.yml | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/.github/workflows/build-and-test.ubuntu-debug.yml b/.github/workflows/build-and-test.ubuntu-debug.yml index 92eca92eb7..31826748e3 100644 --- a/.github/workflows/build-and-test.ubuntu-debug.yml +++ b/.github/workflows/build-and-test.ubuntu-debug.yml @@ -94,6 +94,10 @@ jobs: run: | _build/tests/app_test -l test_suite df -h + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ + -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ + -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite diff --git a/.github/workflows/build-and-test.ubuntu-release.yml b/.github/workflows/build-and-test.ubuntu-release.yml index 9f30bf55ba..cb5480b70f 100644 --- a/.github/workflows/build-and-test.ubuntu-release.yml +++ b/.github/workflows/build-and-test.ubuntu-release.yml @@ -73,6 +73,10 @@ jobs: - name: Unit-Tests run: | _build/tests/app_test -l test_suite + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ + -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ + -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index f1afc20f3c..d63213559d 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -118,6 +118,10 @@ jobs: run: | _build/tests/app_test -l test_suite df -h + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ + -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ + -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite From 665c170ed72e3a5d09c139da59837307b673c0a9 Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 15:07:16 +0000 Subject: [PATCH 06/11] Move wait_for() from app_test/main to a hpp file --- tests/app/main.cpp | 13 +------------ tests/common/utils.hpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 tests/common/utils.hpp diff --git a/tests/app/main.cpp b/tests/app/main.cpp index 6afc8969a8..0d052b15d8 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -43,8 +43,8 @@ #include "../../libraries/app/application_impl.hxx" #include "../common/init_unit_test_suite.hpp" - #include "../common/genesis_file_util.hpp" +#include "../common/utils.hpp" uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; // needed for compiling, not used @@ -54,17 +54,6 @@ namespace bpo = boost::program_options; namespace fc { extern std::unordered_map &get_logger_map(); extern std::unordered_map &get_appender_map(); - - /** Waits for F() to return true before max_duration has passed. - */ - template - static void wait_for( const fc::microseconds max_duration, const Functor&& f ) - { - const auto start = fc::time_point::now(); - while( !f() && fc::time_point::now() < start + max_duration ) - fc::usleep(fc::milliseconds(100)); - BOOST_REQUIRE( f() ); - } } BOOST_AUTO_TEST_CASE(load_configuration_options_test_config_logging_files_created) diff --git a/tests/common/utils.hpp b/tests/common/utils.hpp new file mode 100644 index 0000000000..b590331f64 --- /dev/null +++ b/tests/common/utils.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2015 Cryptonomex, Inc., and contributors. + * + * The MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include +#include + +namespace fc { + + /** Waits for F() to return true before max_duration has passed. + */ + template + static void wait_for( const fc::microseconds max_duration, const Functor&& f ) + { + const auto start = fc::time_point::now(); + while( !f() && fc::time_point::now() < start + max_duration ) + fc::usleep(fc::milliseconds(100)); + BOOST_REQUIRE( f() ); + } +} From 78d86ced220b2a1ad37355f94d8c9b4d84d5a9de Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 16:10:34 +0000 Subject: [PATCH 07/11] Get ES URL from env (if exist) in es_test --- tests/app/main.cpp | 2 - tests/cli/main.cpp | 2 - tests/common/database_fixture.cpp | 54 ++++++++++++++++++--------- tests/common/init_unit_test_suite.hpp | 12 +++++- tests/elasticsearch/main.cpp | 12 +++--- 5 files changed, 54 insertions(+), 28 deletions(-) diff --git a/tests/app/main.cpp b/tests/app/main.cpp index 0d052b15d8..ba4b944819 100644 --- a/tests/app/main.cpp +++ b/tests/app/main.cpp @@ -46,8 +46,6 @@ #include "../common/genesis_file_util.hpp" #include "../common/utils.hpp" -uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; // needed for compiling, not used - using namespace graphene; namespace bpo = boost::program_options; diff --git a/tests/cli/main.cpp b/tests/cli/main.cpp index c0f3396ee5..91c0af7cc6 100644 --- a/tests/cli/main.cpp +++ b/tests/cli/main.cpp @@ -62,8 +62,6 @@ #include "../common/init_unit_test_suite.hpp" -uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; // needed for compiling, not used - /***** * Global Initialization for Windows * ( sets up Winsock stuf ) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index c70ed4cca1..afe12c365e 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -54,7 +54,8 @@ using namespace graphene::chain::test; -uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; +extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP; +extern std::string GRAPHENE_TESTING_ES_URL; namespace graphene { namespace chain { @@ -292,14 +293,22 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) current_test_name == "elasticsearch_history_api") { auto esplugin = app.register_plugin(true); - options.insert(std::make_pair("elasticsearch-node-url", boost::program_options::variable_value(string("http://localhost:9200/"), false))); - options.insert(std::make_pair("elasticsearch-bulk-replay", boost::program_options::variable_value(uint32_t(2), false))); - options.insert(std::make_pair("elasticsearch-bulk-sync", boost::program_options::variable_value(uint32_t(2), false))); - options.insert(std::make_pair("elasticsearch-start-es-after-block", boost::program_options::variable_value(uint32_t(0), false))); - options.insert(std::make_pair("elasticsearch-visitor", boost::program_options::variable_value(false, false))); - options.insert(std::make_pair("elasticsearch-operation-object", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("elasticsearch-operation-string", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("elasticsearch-mode", boost::program_options::variable_value(uint16_t(2), false))); + options.insert(std::make_pair("elasticsearch-node-url", + boost::program_options::variable_value(GRAPHENE_TESTING_ES_URL, false))); + options.insert(std::make_pair("elasticsearch-bulk-replay", + boost::program_options::variable_value(uint32_t(2), false))); + options.insert(std::make_pair("elasticsearch-bulk-sync", + boost::program_options::variable_value(uint32_t(2), false))); + options.insert(std::make_pair("elasticsearch-start-es-after-block", + boost::program_options::variable_value(uint32_t(0), false))); + options.insert(std::make_pair("elasticsearch-visitor", + boost::program_options::variable_value(false, false))); + options.insert(std::make_pair("elasticsearch-operation-object", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("elasticsearch-operation-string", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("elasticsearch-mode", + boost::program_options::variable_value(uint16_t(2), false))); es_index_prefix = string("bitshares-") + fc::to_string(uint64_t(rand())) + "-"; BOOST_TEST_MESSAGE( string("ES index prefix is ") + es_index_prefix ); @@ -319,15 +328,24 @@ database_fixture::database_fixture(const fc::time_point_sec &initial_timestamp) if(current_test_name == "elasticsearch_objects" || current_test_name == "elasticsearch_suite") { auto esobjects_plugin = app.register_plugin(true); - options.insert(std::make_pair("es-objects-elasticsearch-url", boost::program_options::variable_value(string("http://localhost:9200/"), false))); - options.insert(std::make_pair("es-objects-bulk-replay", boost::program_options::variable_value(uint32_t(2), false))); - options.insert(std::make_pair("es-objects-bulk-sync", boost::program_options::variable_value(uint32_t(2), false))); - options.insert(std::make_pair("es-objects-proposals", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("es-objects-accounts", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("es-objects-assets", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("es-objects-balances", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("es-objects-limit-orders", boost::program_options::variable_value(true, false))); - options.insert(std::make_pair("es-objects-asset-bitasset", boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-elasticsearch-url", + boost::program_options::variable_value(GRAPHENE_TESTING_ES_URL, false))); + options.insert(std::make_pair("es-objects-bulk-replay", + boost::program_options::variable_value(uint32_t(2), false))); + options.insert(std::make_pair("es-objects-bulk-sync", + boost::program_options::variable_value(uint32_t(2), false))); + options.insert(std::make_pair("es-objects-proposals", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-accounts", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-assets", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-balances", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-limit-orders", + boost::program_options::variable_value(true, false))); + options.insert(std::make_pair("es-objects-asset-bitasset", + boost::program_options::variable_value(true, false))); es_obj_index_prefix = string("objects-") + fc::to_string(uint64_t(rand())) + "-"; BOOST_TEST_MESSAGE( string("ES_OBJ index prefix is ") + es_obj_index_prefix ); diff --git a/tests/common/init_unit_test_suite.hpp b/tests/common/init_unit_test_suite.hpp index 0c3e9f7644..3732ba25c7 100644 --- a/tests/common/init_unit_test_suite.hpp +++ b/tests/common/init_unit_test_suite.hpp @@ -25,8 +25,10 @@ #include #include #include +#include -extern uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP; +uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; +std::string GRAPHENE_TESTING_ES_URL = "http://127.0.0.1:9200"; boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); @@ -38,5 +40,13 @@ boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { GRAPHENE_TESTING_GENESIS_TIMESTAMP = std::stoul( genesis_timestamp_str ); } std::cout << "GRAPHENE_TESTING_GENESIS_TIMESTAMP is " << GRAPHENE_TESTING_GENESIS_TIMESTAMP << std::endl; + const char* env_es_url = getenv("GRAPHENE_TESTING_ES_URL"); + if( env_es_url != nullptr ) + { + std::string tmp_es_url( env_es_url ); + if( tmp_es_url.substr(0, 7) == "http://" || tmp_es_url.substr(0, 8) == "https://" ) + GRAPHENE_TESTING_ES_URL = tmp_es_url; + } + std::cout << "GRAPHENE_TESTING_ES_URL is " << GRAPHENE_TESTING_ES_URL << std::endl; return nullptr; } diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index 901021b018..c35bd870da 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -45,6 +45,8 @@ using namespace graphene::chain; using namespace graphene::chain::test; using namespace graphene::app; +extern std::string GRAPHENE_TESTING_ES_URL; + BOOST_FIXTURE_TEST_SUITE( elasticsearch_tests, database_fixture ) BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { @@ -55,7 +57,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { graphene::utilities::ES es; es.curl = curl; - es.elasticsearch_url = "http://localhost:9200/"; + es.elasticsearch_url = GRAPHENE_TESTING_ES_URL; es.index_prefix = es_index_prefix; // delete all first @@ -139,7 +141,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) { graphene::utilities::ES es; es.curl = curl; - es.elasticsearch_url = "http://localhost:9200/"; + es.elasticsearch_url = GRAPHENE_TESTING_ES_URL; es.index_prefix = es_obj_index_prefix; // delete all first @@ -193,14 +195,14 @@ BOOST_AUTO_TEST_CASE(elasticsearch_suite) { graphene::utilities::ES es; es.curl = curl; - es.elasticsearch_url = "http://localhost:9200/"; + es.elasticsearch_url = GRAPHENE_TESTING_ES_URL; es.index_prefix = es_index_prefix; auto delete_account_history = graphene::utilities::deleteAll(es); BOOST_REQUIRE(delete_account_history); // require successful deletion graphene::utilities::ES es_obj; es_obj.curl = curl; - es_obj.elasticsearch_url = "http://localhost:9200/"; + es_obj.elasticsearch_url = GRAPHENE_TESTING_ES_URL; es_obj.index_prefix = es_obj_index_prefix; auto delete_objects = graphene::utilities::deleteAll(es_obj); BOOST_REQUIRE(delete_objects); // require successful deletion @@ -223,7 +225,7 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { graphene::utilities::ES es; es.curl = curl; - es.elasticsearch_url = "http://localhost:9200/"; + es.elasticsearch_url = GRAPHENE_TESTING_ES_URL; es.index_prefix = es_index_prefix; auto delete_account_history = graphene::utilities::deleteAll(es); From c24a4275a06f34b96574ace379af2e0e0f0dbb8f Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 16:29:04 +0000 Subject: [PATCH 08/11] Replace sleep() with wait_for() in es_test --- tests/elasticsearch/main.cpp | 85 +++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/tests/elasticsearch/main.cpp b/tests/elasticsearch/main.cpp index c35bd870da..eb254c559c 100644 --- a/tests/elasticsearch/main.cpp +++ b/tests/elasticsearch/main.cpp @@ -32,14 +32,9 @@ #include "../common/database_fixture.hpp" #include "../common/init_unit_test_suite.hpp" +#include "../common/utils.hpp" -#ifdef NDEBUG - #define ES_FIRST_WAIT_TIME (fc::milliseconds(3000)) - #define ES_WAIT_TIME (fc::milliseconds(1000)) -#else - #define ES_FIRST_WAIT_TIME (fc::milliseconds(6000)) - #define ES_WAIT_TIME (fc::milliseconds(3000)) -#endif +#define ES_WAIT_TIME (fc::milliseconds(10000)) using namespace graphene::chain; using namespace graphene::chain::test; @@ -72,16 +67,21 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { auto bob = create_account("bob"); generate_block(); - fc::usleep(ES_FIRST_WAIT_TIME); // this is because index.refresh_interval, nothing to worry string query = "{ \"query\" : { \"bool\" : { \"must\" : [{\"match_all\": {}}] } } }"; es.endpoint = es.index_prefix + "*/data/_count"; es.query = query; - auto res = graphene::utilities::simpleQuery(es); - variant j = fc::json::from_string(res); - auto total = j["count"].as_string(); - BOOST_CHECK_EQUAL(total, "5"); + string res; + variant j; + string total; + + fc::wait_for( ES_WAIT_TIME, [&]() { + res = graphene::utilities::simpleQuery(es); + j = fc::json::from_string(res); + total = j["count"].as_string(); + return (total == "5"); + }); es.endpoint = es.index_prefix + "*/data/_search"; res = graphene::utilities::simpleQuery(es); @@ -93,14 +93,14 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { auto willie = create_account("willie"); generate_block(); - fc::usleep(ES_WAIT_TIME); // index.refresh_interval - es.endpoint = es.index_prefix + "*/data/_count"; - res = graphene::utilities::simpleQuery(es); - j = fc::json::from_string(res); - total = j["count"].as_string(); - BOOST_CHECK_EQUAL(total, "7"); + fc::wait_for( ES_WAIT_TIME, [&]() { + res = graphene::utilities::simpleQuery(es); + j = fc::json::from_string(res); + total = j["count"].as_string(); + return (total == "7"); + }); // do some transfers in 1 block transfer(account_id_type()(db), bob, asset(100)); @@ -108,13 +108,13 @@ BOOST_AUTO_TEST_CASE(elasticsearch_account_history) { transfer(account_id_type()(db), bob, asset(300)); generate_block(); - fc::usleep(ES_WAIT_TIME); // index.refresh_interval - - res = graphene::utilities::simpleQuery(es); - j = fc::json::from_string(res); - total = j["count"].as_string(); - BOOST_CHECK_EQUAL(total, "13"); + fc::wait_for( ES_WAIT_TIME, [&]() { + res = graphene::utilities::simpleQuery(es); + j = fc::json::from_string(res); + total = j["count"].as_string(); + return (total == "13"); + }); // check the visitor data auto block_date = db.head_block_time(); @@ -146,25 +146,30 @@ BOOST_AUTO_TEST_CASE(elasticsearch_objects) { // delete all first auto delete_objects = graphene::utilities::deleteAll(es); + BOOST_REQUIRE(delete_objects); // require successful deletion generate_block(); - BOOST_REQUIRE(delete_objects); // require successful deletion if(delete_objects) { // all records deleted // asset and bitasset create_bitasset("USD", account_id_type()); generate_block(); - fc::usleep(ES_FIRST_WAIT_TIME); string query = "{ \"query\" : { \"bool\" : { \"must\" : [{\"match_all\": {}}] } } }"; es.endpoint = es.index_prefix + "*/data/_count"; es.query = query; - auto res = graphene::utilities::simpleQuery(es); - variant j = fc::json::from_string(res); - auto total = j["count"].as_string(); - BOOST_CHECK_EQUAL(total, "2"); + string res; + variant j; + string total; + + fc::wait_for( ES_WAIT_TIME, [&]() { + res = graphene::utilities::simpleQuery(es); + j = fc::json::from_string(res); + total = j["count"].as_string(); + return (total == "2"); + }); es.endpoint = es.index_prefix + "asset/data/_search"; res = graphene::utilities::simpleQuery(es); @@ -244,14 +249,19 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { create_bitasset("OIL", dan.id); // create op 6 generate_block(); - fc::usleep(ES_FIRST_WAIT_TIME); graphene::app::history_api hist_api(app); app.enable_plugin("elasticsearch"); // f(A, 0, 4, 9) = { 5, 3, 1, 0 } - auto histories = hist_api.get_account_history("1.2.0", operation_history_id_type(), 4, operation_history_id_type(9)); - + auto histories = hist_api.get_account_history( + "1.2.0", operation_history_id_type(), 4, operation_history_id_type(9)); + + fc::wait_for( ES_WAIT_TIME, [&]() { + histories = hist_api.get_account_history( + "1.2.0", operation_history_id_type(), 4, operation_history_id_type(9)); + return (histories.size() == 4u); + }); BOOST_REQUIRE_EQUAL(histories.size(), 4u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 5u); BOOST_CHECK_EQUAL(histories[1].id.instance(), 3u); @@ -513,11 +523,14 @@ BOOST_AUTO_TEST_CASE(elasticsearch_history_api) { create_account("alice"); generate_block(); - fc::usleep(ES_WAIT_TIME); // f(C, 0, 4, 10) = { 7 } - histories = hist_api.get_account_history("alice", operation_history_id_type(0), 4, operation_history_id_type(10)); - BOOST_CHECK_EQUAL(histories.size(), 1u); + fc::wait_for( ES_WAIT_TIME, [&]() { + histories = hist_api.get_account_history( + "alice", operation_history_id_type(0), 4, operation_history_id_type(10)); + return (histories.size() == 1u); + }); + BOOST_REQUIRE_EQUAL(histories.size(), 1u); BOOST_CHECK_EQUAL(histories[0].id.instance(), 7u); // f(C, 8, 4, 10) = { } From 7772a01395a808940282eefe383037e8b20210fd Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 17:09:18 +0000 Subject: [PATCH 09/11] Cleanup ES data after testing --- tests/common/database_fixture.cpp | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/common/database_fixture.cpp b/tests/common/database_fixture.cpp index afe12c365e..a5f3e7b40d 100644 --- a/tests/common/database_fixture.cpp +++ b/tests/common/database_fixture.cpp @@ -419,6 +419,41 @@ database_fixture::~database_fixture() } catch (...) { BOOST_FAIL( "Uncaught exception in ~database_fixture" ); } + + // cleanup data in ES + if( !es_index_prefix.empty() || !es_obj_index_prefix.empty() ) + { + CURL *curl; // curl handler + curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); + + graphene::utilities::ES es; + es.curl = curl; + es.elasticsearch_url = GRAPHENE_TESTING_ES_URL; + + if( !es_index_prefix.empty() ) + { + es.index_prefix = es_index_prefix; + // delete all + try { + graphene::utilities::deleteAll(es); + } catch (...) { + // nothing to do + } + } + + if( !es_obj_index_prefix.empty() ) + { + es.index_prefix = es_obj_index_prefix; + // delete all + try { + graphene::utilities::deleteAll(es); + } catch (...) { + // nothing to do + } + } + } + } void database_fixture::vote_for_committee_and_witnesses(uint16_t num_committee, uint16_t num_witness) From c54db3ad3b92e43c1674e56536f5ece2130f300e Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 17:34:29 +0000 Subject: [PATCH 10/11] Fix ES URL in tests, and remove unnecessary tweak --- .github/workflows/build-and-test.ubuntu-debug.yml | 2 -- .github/workflows/build-and-test.ubuntu-release.yml | 2 -- .github/workflows/sonar-scan.yml | 2 -- tests/common/init_unit_test_suite.hpp | 2 +- 4 files changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/build-and-test.ubuntu-debug.yml b/.github/workflows/build-and-test.ubuntu-debug.yml index 31826748e3..d7caf3c271 100644 --- a/.github/workflows/build-and-test.ubuntu-debug.yml +++ b/.github/workflows/build-and-test.ubuntu-debug.yml @@ -96,8 +96,6 @@ jobs: df -h curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' - curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ - -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite diff --git a/.github/workflows/build-and-test.ubuntu-release.yml b/.github/workflows/build-and-test.ubuntu-release.yml index cb5480b70f..8213775750 100644 --- a/.github/workflows/build-and-test.ubuntu-release.yml +++ b/.github/workflows/build-and-test.ubuntu-release.yml @@ -75,8 +75,6 @@ jobs: _build/tests/app_test -l test_suite curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' - curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ - -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index d63213559d..9803b0030b 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -120,8 +120,6 @@ jobs: df -h curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' - curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_all/_settings \ - -d '{"index.blocks.read_only_allow_delete": null}' _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite diff --git a/tests/common/init_unit_test_suite.hpp b/tests/common/init_unit_test_suite.hpp index 3732ba25c7..472d1e7f14 100644 --- a/tests/common/init_unit_test_suite.hpp +++ b/tests/common/init_unit_test_suite.hpp @@ -28,7 +28,7 @@ #include uint32_t GRAPHENE_TESTING_GENESIS_TIMESTAMP = 1431700000; -std::string GRAPHENE_TESTING_ES_URL = "http://127.0.0.1:9200"; +std::string GRAPHENE_TESTING_ES_URL = "http://127.0.0.1:9200/"; boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { const auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); From d8c91310f84a07ee89dc960e6526c7034d0b3e97 Mon Sep 17 00:00:00 2001 From: abitmore Date: Sat, 6 Mar 2021 17:39:18 +0000 Subject: [PATCH 11/11] Output a new line before es_test in Github Actions --- .github/workflows/build-and-test.ubuntu-debug.yml | 1 + .github/workflows/build-and-test.ubuntu-release.yml | 1 + .github/workflows/sonar-scan.yml | 1 + 3 files changed, 3 insertions(+) diff --git a/.github/workflows/build-and-test.ubuntu-debug.yml b/.github/workflows/build-and-test.ubuntu-debug.yml index d7caf3c271..3ca8a0d347 100644 --- a/.github/workflows/build-and-test.ubuntu-debug.yml +++ b/.github/workflows/build-and-test.ubuntu-debug.yml @@ -96,6 +96,7 @@ jobs: df -h curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + echo _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite diff --git a/.github/workflows/build-and-test.ubuntu-release.yml b/.github/workflows/build-and-test.ubuntu-release.yml index 8213775750..5f5be7fd12 100644 --- a/.github/workflows/build-and-test.ubuntu-release.yml +++ b/.github/workflows/build-and-test.ubuntu-release.yml @@ -75,6 +75,7 @@ jobs: _build/tests/app_test -l test_suite curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + echo _build/tests/es_test -l test_suite libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite _build/tests/cli_test -l test_suite diff --git a/.github/workflows/sonar-scan.yml b/.github/workflows/sonar-scan.yml index 9803b0030b..f9e07e8c0c 100644 --- a/.github/workflows/sonar-scan.yml +++ b/.github/workflows/sonar-scan.yml @@ -120,6 +120,7 @@ jobs: df -h curl -XPUT -H "Content-Type: application/json" http://localhost:9200/_cluster/settings \ -d '{ "transient": { "cluster.routing.allocation.disk.threshold_enabled": false } }' + echo _build/tests/es_test -l test_suite df -h libraries/fc/tests/run-parallel-tests.sh _build/tests/chain_test -l test_suite