From 09e73bbbe655333894525e08d2c919e9a5ceb01a Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Tue, 9 Oct 2018 13:25:07 +0200 Subject: [PATCH 01/10] Fix byteswriter_tests for MSVC compiler MSVC represent 12UL as 4 bytes literal not as 8 bytes, but 12ULL is (unsigned long long) so it is 8 bytes in all compilers --- .gitignore | 1 + core/test/bytes/byteswriter_tests.cpp | 8 -------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index ce1a87741b..024cc508c3 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ core/lib/secp256k1/tmp/ node_modules api/djinni/support-lib/*.mk api/djinni/support-lib/*.Makefile +.vscode/ diff --git a/core/test/bytes/byteswriter_tests.cpp b/core/test/bytes/byteswriter_tests.cpp index be44cdefa9..7ac27b1d42 100644 --- a/core/test/bytes/byteswriter_tests.cpp +++ b/core/test/bytes/byteswriter_tests.cpp @@ -37,21 +37,13 @@ using namespace ledger::core; TEST(BytesWriter, WriteBeValues) { EXPECT_EQ(BytesWriter().writeBeValue(12U).toByteArray(), std::vector({0x00, 0x00, 0x00, 12})); EXPECT_EQ(BytesWriter().writeBeValue((uint8_t)12).toByteArray(), std::vector({12})); -#ifdef __MINGW32__ EXPECT_EQ(BytesWriter().writeBeValue(12ULL).toByteArray(), std::vector({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 12})); -#else - EXPECT_EQ(BytesWriter().writeBeValue(12UL).toByteArray(), std::vector({0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 12})); -#endif } TEST(BytesWriter, WriteLeValues) { EXPECT_EQ(BytesWriter().writeLeValue(12U).toByteArray(), std::vector({12, 0x00, 0x00, 00})); EXPECT_EQ(BytesWriter().writeLeValue((uint8_t)12).toByteArray(), std::vector({12})); -#ifdef __MINGW32__ EXPECT_EQ(BytesWriter().writeLeValue(12ULL).toByteArray(), std::vector({12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})); -#else - EXPECT_EQ(BytesWriter().writeLeValue(12UL).toByteArray(), std::vector({12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})); -#endif } TEST(BytesWriter, WriteVarInt) { From 30e3ddae812fc40b1716cc3b68677fac062b5f86 Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Tue, 9 Oct 2018 15:06:34 +0200 Subject: [PATCH 02/10] Remove priv_key from appveyor Add this environment variable manually to the UI https://www.appveyor.com/docs/how-to/private-git-sub-modules/ --- appveyor.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 9cb9ce24b1..da6ab88000 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,8 +9,6 @@ environment: nodejs_version: "9" appveyor_rdp_password: secure: jb1LsDmcxCww7tA38S3xSw== - priv_key: - secure: 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 pub_key: secure: B+4FNFeW6sbRsl7XJYnHUYptAx/2AvV7XySuhhEMdvcloUz0YXxgX/EguKV2Bvm5GaBKYLlq4G2Lw7udm4l68I9g3x+2pZvWiMBUhTRaTGVqi8PXXN+RON62G77yR9Lt/zUR+ljVjuJmRqQBU3NkzOJAAfZ/RDOCAEcWkA3cEE64IkfpqOTHBPW0PpwZcPGFF/l4MGdP8r5XIeu/WwPZHfQjGnDlME0VgkRWflA/Pjt0GcyDSLURXQBk+5JJhn9FjGPCKD+db4xQkW7zhep/su5urEf0HYTmVHRiKMZNfBPGSnL/Lmr6343qc6YF78lyX2U9saoJrf+tuxqAHtUq0KhkHltbNeIUaxvcC7TcH+kEm/Uxl/al8iRiMWE86b8z/wiFQI1xA+oejrFQivbyZVJ+cOifoZ7HhYwP+niOnUG+ZzT5WFLejnsnd4Lwdi5XzWaKKooIPtZ9qcyk15SFzVBayEeeljNk/gByrT6U+25LUqO6ekXrZ+A86Tmjkhp67B5+JNxis+k3sCZ63ASP6LhL9NvrVNky1ZGB8WAZ/hs= matrix: From ab882a75845582df8412531994169f57c7453aab Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Tue, 9 Oct 2018 17:12:16 +0200 Subject: [PATCH 03/10] Fix DateUtils test on Windows MSVC impl of ctime have _mkgmtime that do the job. --- core/src/utils/DateUtils.cpp | 9 ++++----- core/test/utils/date_parser_tests.cpp | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/core/src/utils/DateUtils.cpp b/core/src/utils/DateUtils.cpp index 83e0925e88..795a88e5df 100644 --- a/core/src/utils/DateUtils.cpp +++ b/core/src/utils/DateUtils.cpp @@ -36,12 +36,11 @@ #if defined(_WIN32) || defined(_WIN64) - #if defined(_MSC_VER) && _MSC_VER <= 1900 - long shift = 0; - int result = _get_timezone(&shift); - #define timezone shift + #if defined(_MSC_VER) + time_t timegm(struct tm* tm) { return _mkgmtime(tm); } + #else + time_t timegm(struct tm* tm) { return mktime(tm) - timezone; } #endif -time_t timegm(struct tm* tm) { return mktime(tm) - timezone; } #endif std::chrono::system_clock::time_point ledger::core::DateUtils::fromJSON(const std::string &str) { diff --git a/core/test/utils/date_parser_tests.cpp b/core/test/utils/date_parser_tests.cpp index 7388590e92..8eca221eb2 100644 --- a/core/test/utils/date_parser_tests.cpp +++ b/core/test/utils/date_parser_tests.cpp @@ -36,12 +36,12 @@ using namespace ledger::core; TEST(DateParser, ParseJSONDate) { auto date = "2012-04-23T18:25:43.511Z"; - EXPECT_EQ(DateUtils::fromJSON(date).time_since_epoch().count(), 1335205543000000); + EXPECT_EQ(std::chrono::duration_cast(DateUtils::fromJSON(date).time_since_epoch()).count(), 1335205543000000); } TEST(DateParser, ParseJSONDate2) { auto date = "2017-03-27T09:10:22Z"; - EXPECT_EQ(DateUtils::fromJSON(date).time_since_epoch().count(), 1490605822000000); + EXPECT_EQ(std::chrono::duration_cast(DateUtils::fromJSON(date).time_since_epoch()).count(), 1490605822000000); } TEST(DateParser, ToJSONDate) { From bcf082b1ce311f87fc6c4728150e2929cd608f33 Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Wed, 10 Oct 2018 12:31:14 +0200 Subject: [PATCH 04/10] Fix wrong file size calculation in RotatingEncryptableSink + tests for Windows Test was written unix line endings --- core/src/debug/RotatingEncryptableSink.cpp | 13 ++++++------- core/src/debug/RotatingEncryptableSink.hpp | 2 +- core/test/debug/logger_test.cpp | 16 ++++++++-------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/core/src/debug/RotatingEncryptableSink.cpp b/core/src/debug/RotatingEncryptableSink.cpp index 133a63ce77..e7eb7fdd59 100644 --- a/core/src/debug/RotatingEncryptableSink.cpp +++ b/core/src/debug/RotatingEncryptableSink.cpp @@ -60,11 +60,10 @@ namespace ledger { void RotatingEncryptableSink::log(const spdlog::details::log_msg &msg) { auto context = _context; - std::string message = msg.formatted.data(); - auto size = msg.formatted.size(); + std::string message = msg.formatted.str(); auto self = shared_from_this(); - context->execute(make_runnable([self, message, size] () { - self->_sink_it(message, size); + context->execute(make_runnable([self, message] () { + self->_sink_it(message); })); } @@ -75,13 +74,13 @@ namespace ledger { })); } - void RotatingEncryptableSink::_sink_it(std::string msg, std::size_t size) { + void RotatingEncryptableSink::_sink_it(std::string msg) { // TODO: implement encryption - _current_size += size; + _current_size += msg.size(); if (_current_size > _max_size) { _rotate(); - _current_size = size; + _current_size = msg.size(); } spdlog::details::log_msg spdlog_msg; diff --git a/core/src/debug/RotatingEncryptableSink.hpp b/core/src/debug/RotatingEncryptableSink.hpp index 78d515bb27..32120a2313 100644 --- a/core/src/debug/RotatingEncryptableSink.hpp +++ b/core/src/debug/RotatingEncryptableSink.hpp @@ -58,7 +58,7 @@ namespace ledger { virtual void flush() override; protected: - void _sink_it(std::string msg, std::size_t size); + void _sink_it(std::string msg); private: static spdlog::filename_t calc_filename( diff --git a/core/test/debug/logger_test.cpp b/core/test/debug/logger_test.cpp index 31ce90eb2a..e038e04ea0 100644 --- a/core/test/debug/logger_test.cpp +++ b/core/test/debug/logger_test.cpp @@ -29,16 +29,16 @@ * */ -#include #include #include #include #include -#include -#include #include -#include +#include #include +#include +#include +#include #include #include #include @@ -47,13 +47,13 @@ TEST(LoggerTest, LogAndOverflow) { auto dispatcher = std::make_shared(); auto logPrinter = std::make_shared(dispatcher->getMainExecutionContext()); auto resolver = std::make_shared(); - + auto logLineExample = std::string("2017-03-02T10:07:06Z+01:00 D: This is a log XXX") + spdlog::details::os::default_eol; std::shared_ptr logger = ledger::core::logger::create("test_logs", std::experimental::optional(), dispatcher->getSerialExecutionContext("logger"), resolver, logPrinter, - (std::string("2017-03-02T10:07:06Z+01:00 D: This is a log \n").size() + 3) * 199 + logLineExample.size() * 199 ); dispatcher->getMainExecutionContext()->execute(make_runnable([=] () { for (auto i = 0; i < 200; i++) { @@ -79,13 +79,13 @@ TEST(LoggerTest, LogNoOverflow) { auto dispatcher = std::make_shared(); auto logPrinter = std::make_shared(dispatcher->getMainExecutionContext()); auto resolver = std::make_shared(); - + auto logLineExample = std::string("2017-03-02T10:07:06Z+01:00 D: This is a log XXX") + spdlog::details::os::default_eol; std::shared_ptr logger = ledger::core::logger::create("test_logs_1", std::experimental::optional(), dispatcher->getSerialExecutionContext("logger"), resolver, logPrinter, - (std::string("2017-03-02T10:07:06Z+01:00 D: This is a log \n").size() + 3) * 200 + logLineExample.size() * 200 ); dispatcher->getMainExecutionContext()->execute(make_runnable([=] () { for (auto i = 0; i < 200; i++) { From ae88ea262cf5eb2a52415a1ba29582b4cc99b77b Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Wed, 10 Oct 2018 16:22:16 +0200 Subject: [PATCH 05/10] Fix database test Two instances of QCoreApplication was created during creating QtThreadDispatcher. Now using dispatcher created by fixture object. --- core/test/database/BaseFixture.h | 2 +- core/test/database/query_builder_tests.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/core/test/database/BaseFixture.h b/core/test/database/BaseFixture.h index 0ef5e10f60..0af07a2d5d 100644 --- a/core/test/database/BaseFixture.h +++ b/core/test/database/BaseFixture.h @@ -94,7 +94,7 @@ class BaseFixture : public ::testing::Test { int32_t index, const api::AccountCreationInfo &info ); - std::shared_ptr createBitcoinLikeAccount(const std::shared_ptr& wallet, + static std::shared_ptr createBitcoinLikeAccount(const std::shared_ptr& wallet, int32_t index, const api::ExtendedKeyAccountCreationInfo& info ); diff --git a/core/test/database/query_builder_tests.cpp b/core/test/database/query_builder_tests.cpp index 5559766989..3d6c235312 100644 --- a/core/test/database/query_builder_tests.cpp +++ b/core/test/database/query_builder_tests.cpp @@ -52,17 +52,18 @@ #include "BaseFixture.h" -class QueryBuilderTest : public BaseFixture { +// class QueryBuilderTest : public BaseFixture { -}; +// }; -TEST_F(QueryBuilderTest, SimpleOperationQuery) { - auto pool = newDefaultPool(); +TEST(QueryBuilderTest, SimpleOperationQuery) { + QTemporaryDir tempDir; + auto resolver = std::make_shared(tempDir.path().toStdString()); { auto wallet = wait(pool->createWallet("my_wallet", "bitcoin", api::DynamicObject::newInstance())); auto nextIndex = wait(wallet->getNextAccountIndex()); EXPECT_EQ(nextIndex, 0); - auto account = createBitcoinLikeAccount(wallet, 0, P2PKH_MEDIUM_XPUB_INFO); + auto account = BaseFixture::createBitcoinLikeAccount(wallet, 0, P2PKH_MEDIUM_XPUB_INFO); std::vector transactions = { *JSONUtils::parse(TX_1), *JSONUtils::parse(TX_2), From 9723d07cbad27ef6a4aa190ed66b93e23c521d78 Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Wed, 10 Oct 2018 18:08:35 +0200 Subject: [PATCH 06/10] Speed up MapLike.lift Exceptions are slow --- core/src/collections/MapLike.hpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/core/src/collections/MapLike.hpp b/core/src/collections/MapLike.hpp index 5e289e40d9..021a13b138 100644 --- a/core/src/collections/MapLike.hpp +++ b/core/src/collections/MapLike.hpp @@ -83,9 +83,11 @@ namespace ledger { } Option lift(const K &key) const { - return Try::from([this, &key] () { - return at(key); - }).toOption(); + auto it = _container.find(key); + if (it == _container.end()) { + return Option::NONE; + } + return Option(it->second); } bool contains(const K &key) const { From 6b55925d727b965d0c68ee81389f8bac3b36b1ff Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Wed, 10 Oct 2018 18:10:16 +0200 Subject: [PATCH 07/10] Remove unnecessary check --- core/src/collections/DynamicObject.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/collections/DynamicObject.cpp b/core/src/collections/DynamicObject.cpp index 982f2c8879..097ef76c04 100644 --- a/core/src/collections/DynamicObject.cpp +++ b/core/src/collections/DynamicObject.cpp @@ -97,7 +97,7 @@ namespace ledger { optional DynamicObject::getString(const std::string &key) { auto v = _values.lift(key); - if (_values.empty() || !v.hasValue() || v.getValue().type != api::DynamicType::STRING) + if (!v.hasValue() || v.getValue().type != api::DynamicType::STRING) return optional(); return optional(v.getValue().string); } From dacf11cf39bfd09fdcf67ee6b3d3e2eed913526f Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Fri, 12 Oct 2018 16:55:38 +0200 Subject: [PATCH 08/10] Remake WebSocket tests, workaround on HTTP tests Host discovery on mangoose htttp client read host file and if there are no entries try to call DNS, except for localhost in Windows 10 by default there are no localhost in host file. Remake WebSocket tests to use mocks and don't test all the code (futures, dispatching + some testing implementations) --- .../lib/libledger-test/MongooseHttpClient.cpp | 2 +- core/test/net/CMakeLists.txt | 2 + core/test/net/http_client_tests.cpp | 12 +- core/test/net/websocket_client_tests.cpp | 198 +++++++++++++----- 4 files changed, 149 insertions(+), 65 deletions(-) diff --git a/core/test/lib/libledger-test/MongooseHttpClient.cpp b/core/test/lib/libledger-test/MongooseHttpClient.cpp index a2a9a765a5..411d8e177f 100644 --- a/core/test/lib/libledger-test/MongooseHttpClient.cpp +++ b/core/test/lib/libledger-test/MongooseHttpClient.cpp @@ -159,7 +159,7 @@ void MongooseHttpClient::execute(const std::shared_ptrgetUrl().c_str(), headers.str().c_str(), c_body); // Pass headers and body data - + assert(nc != nullptr); nc->user_data = new std::pair, std::shared_ptr>(request, self); mg_set_protocol_http_websocket(nc); })); diff --git a/core/test/net/CMakeLists.txt b/core/test/net/CMakeLists.txt index 69d86d222a..5f388534bd 100644 --- a/core/test/net/CMakeLists.txt +++ b/core/test/net/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.0) include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR}) +include_directories(${gmock_SOURCE_DIR}/include ${gmock_SOURCE_DIR}) include_directories(${CMAKE_BINARY_DIR}/include) if (APPLE) add_definitions(-DGTEST_USE_OWN_TR1_TUPLE) @@ -11,6 +12,7 @@ include_directories(../lib/libledger-test/) add_executable(ledger-core-net-tests main.cpp http_client_tests.cpp websocket_client_tests.cpp) target_link_libraries(ledger-core-net-tests gtest gtest_main) +target_link_libraries(ledger-core-net-tests gmock) target_link_libraries(ledger-core-net-tests ledger-core) target_link_libraries(ledger-core-net-tests ledger-test) target_link_libraries(ledger-core-net-tests ledger-qt-host) diff --git a/core/test/net/http_client_tests.cpp b/core/test/net/http_client_tests.cpp index 4e51ec45e2..dd39e8d3a7 100644 --- a/core/test/net/http_client_tests.cpp +++ b/core/test/net/http_client_tests.cpp @@ -60,7 +60,7 @@ TEST(HttpClient, GET) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); @@ -94,7 +94,7 @@ TEST(HttpClient, GETJson) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); @@ -127,7 +127,7 @@ TEST(HttpClient, GETJsonError) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); @@ -161,7 +161,7 @@ TEST(HttpClient, POST) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); @@ -237,7 +237,7 @@ TEST(HttpClient, GETWithSax) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); @@ -317,7 +317,7 @@ TEST(HttpClient, GETWithSaxError) { auto dispatcher = std::make_shared(); auto client = std::make_shared(dispatcher->getSerialExecutionContext("client")); auto worker = dispatcher->getSerialExecutionContext("worker"); - ledger::core::HttpClient http("http://localhost:8000", client, worker); + ledger::core::HttpClient http("http://127.0.0.1:8000", client, worker); { auto server = std::make_shared(dispatcher->getSerialExecutionContext("server")); diff --git a/core/test/net/websocket_client_tests.cpp b/core/test/net/websocket_client_tests.cpp index 6fdbebe59c..4390752696 100644 --- a/core/test/net/websocket_client_tests.cpp +++ b/core/test/net/websocket_client_tests.cpp @@ -30,7 +30,7 @@ */ #include -#include +#include #include #include #include @@ -40,72 +40,154 @@ using namespace ledger::core; using namespace ledger::qt; +using namespace testing; -TEST(WebSocket, ConnectWebSocket) { - auto dispatcher = std::make_shared(); - auto engine = std::make_shared(nullptr); - auto client = std::make_shared(engine); - auto worker = dispatcher->getSerialExecutionContext("worker"); - auto handler = [] (WebSocketEventType event, const std::shared_ptr& connection, - const Option& message, Option code) { +class MockApiWebSocketClient : public api::WebSocketClient { +public: + MOCK_METHOD2(connect, void (const std::string & url, const std::shared_ptr & connection)); + MOCK_METHOD2(send, void (const std::shared_ptr & connection, const std::string & data)); + MOCK_METHOD1(disconnect, void (const std::shared_ptr & connection)); +}; - }; +class MockHandler { +public: + MOCK_METHOD4(Handle, void (WebSocketEventType, + const std::shared_ptr& connection, + const Option& message, + Option)); +}; - client->connect("wss://echo.websocket.org", handler).onComplete(worker, [&] (const TryPtr& result) { - EXPECT_TRUE(result.isSuccess()); - dispatcher->stop(); - }); +class WebSocketClientTest : public Test { +public: + WebSocketClientTest() + : engine(std::make_shared()) + , handler([&h = mockHandler](WebSocketEventType a, + const std::shared_ptr& b, + const Option& c, + Option d) { h.Handle(a,b,c,d); }) {}; +public: + void TearDown() override { + // there are circular dependencies between WebSocketClient and WebSocketConnection + // so without connection.close() there is a leak + Mock::AllowLeak(engine.get()); + } - dispatcher->waitUntilStopped(); + const std::string url = "uri://some_address"; + const std::string message = "Hello from websocket"; + MockHandler mockHandler; + std::shared_ptr engine; + WebSocketEventHandler handler; + const int32_t connectionID = 42; +}; + +TEST_F(WebSocketClientTest, InjectionIsDone) { + //Injection happened once + EXPECT_CALL(*engine, connect(StrEq(url), _)).Times(1); + // callback was not called + EXPECT_CALL(mockHandler, Handle(_, _, _, _)).Times(0); + + WebSocketClient client(engine); + auto connectionFtr = client.connect(url, handler); + EXPECT_FALSE(connectionFtr.isCompleted()); +} + +TEST_F(WebSocketClientTest, ConnectSuccedsSync) { + EXPECT_CALL(*engine, connect(_, _)) + .WillOnce(Invoke([connID = connectionID](Unused, const std::shared_ptr& connImpl) { + connImpl->onConnect(connID); + })); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CONNECT, + _, + Option::NONE, + Option::NONE)).Times(1); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CLOSE, _, _, _)).Times(0); + + WebSocketClient client(engine); + auto connectionFtr = client.connect(url, handler); + ASSERT_TRUE(connectionFtr.isCompleted()); + auto connectionTry = connectionFtr.getValue().getValue(); + ASSERT_TRUE(connectionTry.isSuccess()); + EXPECT_EQ(connectionTry.getValue()->getApiConnection()->getConnectionId(), connectionID); } -TEST(WebSocket, ConnectToInvalidHost) { - auto dispatcher = std::make_shared(); - auto engine = std::make_shared(nullptr); - auto client = std::make_shared(engine); - auto worker = dispatcher->getSerialExecutionContext("worker"); - auto handler = [] (WebSocketEventType event, const std::shared_ptr& connection, - const Option& message, Option code) { +TEST_F(WebSocketClientTest, ConnectFailed) { + const std::string errorMessge("Can't connect"); + api::ErrorCode errorCode = api::ErrorCode::UNABLE_TO_CONNECT_TO_HOST; + EXPECT_CALL(*engine, connect(_, _)) + .WillOnce(Invoke([errorCode, errorMessge](Unused, const std::shared_ptr& connImpl) { + connImpl->onError(errorCode, errorMessge); + })); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CONNECT, _, _, _)).Times(0); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CLOSE, + _, + _, + Option(errorCode))).Times(1); - }; + WebSocketClient client(engine); + auto connectionFtr = client.connect(url, handler); + ASSERT_TRUE(connectionFtr.isCompleted()); + auto connectionTry = connectionFtr.getValue().getValue(); + ASSERT_TRUE(connectionTry.isFailure()); + EXPECT_EQ(connectionTry.getFailure().what(), errorMessge); + EXPECT_EQ(connectionTry.getFailure().getErrorCode(), errorCode); +} - client->connect("wss://echo.websocket.org/does/not/exist", handler).onComplete(worker, [&] (const TryPtr& result) { - EXPECT_TRUE(result.isFailure()); - dispatcher->stop(); - }); +TEST_F(WebSocketClientTest, SendOK) { + EXPECT_CALL(*engine, connect(_, _)) + .WillOnce(Invoke([connID = connectionID](Unused, const std::shared_ptr& connImpl) { + connImpl->onConnect(connID); + })); + EXPECT_CALL(*engine, send(_, StrEq(message))).Times(1); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CONNECT, + _, + Option::NONE, + Option::NONE)).Times(1); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CLOSE, _, _, _)).Times(0); - dispatcher->waitUntilStopped(); + WebSocketClient client(engine); + auto connection = client.connect(url, handler).getValue().getValue().getValue(); + connection->send(message); } -TEST(WebSocket, EchoAndDisconnect) { - auto dispatcher = std::make_shared(); - auto engine = std::make_shared(); - auto client = std::make_shared(engine); - auto worker = dispatcher->getSerialExecutionContext("worker"); - auto main = dispatcher->getMainExecutionContext(); - bool hasReceived = false; - auto handler = [&] (WebSocketEventType event, const std::shared_ptr& connection, - const Option& message, Option code) mutable { - switch (event) { - case WebSocketEventType::CONNECT:break; - case WebSocketEventType::RECEIVE: - EXPECT_EQ("echo from Ledger", message.getValue()); - hasReceived = true; - connection->close(); - break; - case WebSocketEventType::CLOSE: - dispatcher->stop(); - break; - } - }; - - client->connect("wss://echo.websocket.org", handler).onComplete(main, [&] (const TryPtr& result) { - EXPECT_TRUE(result.isSuccess()); - if (result.isSuccess()) { - result.getValue()->send("echo from Ledger"); - } - }); - - dispatcher->waitUntilStopped(); - EXPECT_TRUE(hasReceived); +TEST_F(WebSocketClientTest, SendError) { + const std::string errorMessage("Can't reach the host"); + api::ErrorCode errorCode = api::ErrorCode::NO_INTERNET_CONNECTIVITY; + EXPECT_CALL(*engine, connect(_, _)) + .WillOnce(Invoke([connID = connectionID](Unused, const std::shared_ptr& connImpl) { + connImpl->onConnect(connID); + })); + EXPECT_CALL(*engine, send(_, StrEq(message))). + WillOnce(Invoke([errorCode, errorMessage](const std::shared_ptr& connImpl, const std::string& msg) { + connImpl->onError(errorCode, errorMessage); + })); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CONNECT, + _, + Option::NONE, + Option::NONE)).Times(1); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CLOSE, _, _, _)).Times(1); + + WebSocketClient client(engine); + auto connection = client.connect(url, handler).getValue().getValue().getValue(); + connection->send(message); +} + +TEST_F(WebSocketClientTest, EchoTest) { + EXPECT_CALL(*engine, connect(_, _)) + .WillOnce(Invoke([connID = connectionID](Unused, const std::shared_ptr& connImpl) { + connImpl->onConnect(connID); + })); + EXPECT_CALL(*engine, send(_, StrEq(message))). + WillOnce(Invoke([](const std::shared_ptr& connImpl, const std::string& msg) { + connImpl->onMessage(msg); + })); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CONNECT, + _, + Option::NONE, + Option::NONE)).Times(1); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::CLOSE, _, _, _)).Times(0); + EXPECT_CALL(mockHandler, Handle(WebSocketEventType::RECEIVE, _, Option(message), Option::NONE)).Times(1); + + WebSocketClient client(engine); + auto connection = client.connect(url, handler).getValue().getValue().getValue(); + connection->send(message); } \ No newline at end of file From 8879837b79b7d8c196f6f68980d769d481bd8ead Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Fri, 26 Oct 2018 13:53:46 +0200 Subject: [PATCH 09/10] Revert "Fix database test" This reverts commit ae88ea262cf5eb2a52415a1ba29582b4cc99b77b. --- core/test/database/BaseFixture.h | 2 +- core/test/database/query_builder_tests.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/core/test/database/BaseFixture.h b/core/test/database/BaseFixture.h index 0af07a2d5d..0ef5e10f60 100644 --- a/core/test/database/BaseFixture.h +++ b/core/test/database/BaseFixture.h @@ -94,7 +94,7 @@ class BaseFixture : public ::testing::Test { int32_t index, const api::AccountCreationInfo &info ); - static std::shared_ptr createBitcoinLikeAccount(const std::shared_ptr& wallet, + std::shared_ptr createBitcoinLikeAccount(const std::shared_ptr& wallet, int32_t index, const api::ExtendedKeyAccountCreationInfo& info ); diff --git a/core/test/database/query_builder_tests.cpp b/core/test/database/query_builder_tests.cpp index 3d6c235312..5559766989 100644 --- a/core/test/database/query_builder_tests.cpp +++ b/core/test/database/query_builder_tests.cpp @@ -52,18 +52,17 @@ #include "BaseFixture.h" -// class QueryBuilderTest : public BaseFixture { +class QueryBuilderTest : public BaseFixture { -// }; +}; -TEST(QueryBuilderTest, SimpleOperationQuery) { - QTemporaryDir tempDir; - auto resolver = std::make_shared(tempDir.path().toStdString()); +TEST_F(QueryBuilderTest, SimpleOperationQuery) { + auto pool = newDefaultPool(); { auto wallet = wait(pool->createWallet("my_wallet", "bitcoin", api::DynamicObject::newInstance())); auto nextIndex = wait(wallet->getNextAccountIndex()); EXPECT_EQ(nextIndex, 0); - auto account = BaseFixture::createBitcoinLikeAccount(wallet, 0, P2PKH_MEDIUM_XPUB_INFO); + auto account = createBitcoinLikeAccount(wallet, 0, P2PKH_MEDIUM_XPUB_INFO); std::vector transactions = { *JSONUtils::parse(TX_1), *JSONUtils::parse(TX_2), From aa16b72844d4feff513d222c6842e0ee9d03b604 Mon Sep 17 00:00:00 2001 From: Andrii Korol Date: Mon, 29 Oct 2018 10:18:10 +0100 Subject: [PATCH 10/10] Bump up the version of libcore to 1.1.4 --- CMakeLists.txt | 2 +- appveyor.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37beaac197..dbb31d686b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ include(UseBackportedModules) # The project version number. set(VERSION_MAJOR 1 CACHE STRING "Project major version number.") set(VERSION_MINOR 1 CACHE STRING "Project minor version number.") -set(VERSION_PATCH 3 CACHE STRING "Project patch version number.") +set(VERSION_PATCH 4 CACHE STRING "Project patch version number.") mark_as_advanced(VERSION_MAJOR VERSION_MINOR VERSION_PATCH) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY build) diff --git a/appveyor.yml b/appveyor.yml index da6ab88000..b7cc3445c1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -5,7 +5,7 @@ branches: image: - Visual Studio 2015 environment: - LIB_VERSION: 1.1.3 #Hardcode the LIB_VERSION : should be retrieved by building libcore node module and run tests/lib_version.js + LIB_VERSION: 1.1.4 #Hardcode the LIB_VERSION : should be retrieved by building libcore node module and run tests/lib_version.js nodejs_version: "9" appveyor_rdp_password: secure: jb1LsDmcxCww7tA38S3xSw==