diff --git a/src/io/thrift/thrift_handle.hpp b/src/io/thrift/thrift_handle.hpp new file mode 100644 index 000000000..b9ed0585e --- /dev/null +++ b/src/io/thrift/thrift_handle.hpp @@ -0,0 +1,29 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#pragma once + +namespace memgraph::io::thrift { + +class ThriftHandle { + public: + template + void SubmitRequest(Address to_address, Address from_address, uint64_t request_id, Request &&request, Duration timeout, + ResponsePromise &&promise); + + template + requires(sizeof...(Ms) > 0) RequestResult Receive(const Address &receiver, Duration timeout); + + template + void Send(Address to_address, Address from_address, uint64_t request_id, M message); +}; + +} // namespace memgraph::io::thrift diff --git a/src/io/thrift/thrift_transport.hpp b/src/io/thrift/thrift_transport.hpp new file mode 100644 index 000000000..36398d132 --- /dev/null +++ b/src/io/thrift/thrift_transport.hpp @@ -0,0 +1,70 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#pragma once + +#include +#include +#include +#include + +#include "io/address.hpp" +#include "io/thrift/thrift_handle.hpp" +#include "io/time.hpp" +#include "io/transport.hpp" + +namespace memgraph::io::thrift { + +using memgraph::io::Duration; +using memgraph::io::Time; + +class ThriftTransport { + std::shared_ptr simulator_handle_; + const Address address_; + std::random_device rng_; + + public: + ThriftTransport(std::shared_ptr simulator_handle, Address address) + : simulator_handle_(simulator_handle), address_(address) {} + + template + ResponseFuture Request(Address address, uint64_t request_id, Request request, Duration timeout) { + auto [future, promise] = memgraph::io::FuturePromisePairWithNotifier>(); + + simulator_handle_->SubmitRequest(address, address_, request_id, std::move(request), timeout, std::move(promise)); + + return std::move(future); + } + + template + requires(sizeof...(Ms) > 0) RequestResult Receive(Duration timeout) { + return simulator_handle_->template Receive(address_, timeout); + } + + template + void Send(Address address, uint64_t request_id, M message) { + return simulator_handle_->template Send(address, address_, request_id, message); + } + + Time Now() const { + auto nano_time = std::chrono::system_clock::now(); + auto micros = std::chrono::duration_cast(nano_time); + return Time::now(); + } + + bool ShouldShutDown() const { return false; } + + template , class Return = uint64_t> + Return Rand(D distrib) { + return distrib(rng_); + } +}; +}; // namespace memgraph::io::thrift diff --git a/tests/unit/CMakeLists.txt b/tests/unit/CMakeLists.txt index 9de4860ef..c4c957b8b 100644 --- a/tests/unit/CMakeLists.txt +++ b/tests/unit/CMakeLists.txt @@ -391,12 +391,10 @@ add_custom_target(test_lcp ALL DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/test_lcp) add_test(test_lcp ${CMAKE_CURRENT_BINARY_DIR}/test_lcp) add_dependencies(memgraph__unit test_lcp) -# Test websocket -find_package(Boost REQUIRED) - -add_unit_test(websocket.cpp) -target_link_libraries(${test_prefix}websocket mg-communication Boost::headers) - # Test future add_unit_test(future.cpp) -target_link_libraries(${test_prefix}future mg-io) \ No newline at end of file +target_link_libraries(${test_prefix}future mg-io) + +# Test Thrift transport echo +add_unit_test(thrift_transport_echo.cpp) +target_link_libraries(${test_prefix}thrift_transport_echo mg-io) diff --git a/tests/unit/thrift_transport_echo.cpp b/tests/unit/thrift_transport_echo.cpp new file mode 100644 index 000000000..dda0f9551 --- /dev/null +++ b/tests/unit/thrift_transport_echo.cpp @@ -0,0 +1,21 @@ +// Copyright 2022 Memgraph Ltd. +// +// Use of this software is governed by the Business Source License +// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source +// License, and you may not use this file except in compliance with the Business Source License. +// +// As of the Change Date specified in that file, in accordance with +// the Business Source License, use of this software will be governed +// by the Apache License, Version 2.0, included in the file +// licenses/APL.txt. + +#include +#include + +#include "gtest/gtest.h" + +#include "io/thrift/thrift_transport.hpp" + +using namespace memgraph::io; + +TEST(ThriftTransport, Echo) {}