From 20839b0ae08763863512932d0b40f0e1152c405d Mon Sep 17 00:00:00 2001 From: Tyler Neely Date: Mon, 4 Jul 2022 15:00:32 +0000 Subject: [PATCH] Add simple test for MgFuture --- src/io/v3/future.hpp | 13 ++++------ src/io/v3/simulator.hpp | 38 +----------------------------- src/io/v3/simulator_handle.hpp | 35 +++++++++++++++++++++++++++ tests/simulation/basic_request.cpp | 19 ++++++++++++++- 4 files changed, 58 insertions(+), 47 deletions(-) create mode 100644 src/io/v3/simulator_handle.hpp diff --git a/src/io/v3/future.hpp b/src/io/v3/future.hpp index 572013941..d2a7ce6e2 100644 --- a/src/io/v3/future.hpp +++ b/src/io/v3/future.hpp @@ -21,7 +21,7 @@ #include "utils/logging.hpp" #include "errors.hpp" -#include "simulator.hpp" +#include "simulator_handle.hpp" template class MgPromise; @@ -42,6 +42,7 @@ class Shared { friend MgFuture; public: + Shared() = default; Shared(Shared &&) = default; Shared &operator=(Shared &&) = default; Shared(const Shared &) = delete; @@ -148,9 +149,11 @@ class MgPromise { MgPromise &operator=(const MgPromise &) = delete; ~MgPromise() { + /* MG_ASSERT(filled_, "MgPromise destroyed before its \ associated MgFuture was filled!"); + */ } // Fill the expected item into the Future. @@ -183,11 +186,3 @@ std::pair, MgPromise> FuturePromisePair(SimulatorHandle simulator future.simulator_handle_ = simulator_handle; return std::make_pair(std::move(future), std::move(promise)); } - -namespace _compile_test { -void _templatization_smoke_test() { - auto [future, promise] = FuturePromisePair(); - promise.Fill(true); - MG_ASSERT(future.Wait() == true); -} -} // namespace _compile_test diff --git a/src/io/v3/simulator.hpp b/src/io/v3/simulator.hpp index 2e0d0c318..2dd6d0582 100644 --- a/src/io/v3/simulator.hpp +++ b/src/io/v3/simulator.hpp @@ -25,34 +25,11 @@ struct SimulatorStats { uint64_t total_requests_; uint64_t total_responses_; uint64_t simulator_ticks_; -} +}; struct SimulatorConfig { uint8_t drop_percent_; uint64_t rng_seed_; -} - -class SimulatorHandle { - public: - void NotifySimulator() { - std::unique_lock lock(mu_); - cv_sim_.notify_all(); - } - - private: - std::mutex mu_; - std::condition_variable cv_sim_; - std::condition_variable cv_srv_; -}; - -class SimulatorTransport { - public: - SimulatorTransport(std::shared_ptr simulator_handle, Address address) - : simulator_handle_(simulator_handle), address_(address) {} - - private: - std::shared_ptr simulator_handle_; - Address address_; }; class Simulator { @@ -64,16 +41,3 @@ class Simulator { private: std::shared_ptr simulator_handle_; }; - -namespace _compile_test { -void use_it() { - auto simulator = Simulator(); - auto addr_1 = Address(); - auto addr_2 = Address(); - auto addr_3 = Address(); - - auto sim_transport_1 = simulator.Register(addr_1, true); - auto sim_transport_2 = simulator.Register(addr_2, true); - auto sim_transport_3 = simulator.Register(addr_3, true); -} -} // namespace _compile_test diff --git a/src/io/v3/simulator_handle.hpp b/src/io/v3/simulator_handle.hpp new file mode 100644 index 000000000..f7360c210 --- /dev/null +++ b/src/io/v3/simulator_handle.hpp @@ -0,0 +1,35 @@ +// 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 + +class SimulatorHandle { + public: + void NotifySimulator() { + std::unique_lock lock(mu_); + cv_sim_.notify_all(); + } + + private: + std::mutex mu_; + std::condition_variable cv_sim_; + std::condition_variable cv_srv_; +}; + +class SimulatorTransport { + public: + SimulatorTransport(std::shared_ptr simulator_handle, Address address) + : simulator_handle_(simulator_handle), address_(address) {} + + private: + std::shared_ptr simulator_handle_; + Address address_; +}; diff --git a/tests/simulation/basic_request.cpp b/tests/simulation/basic_request.cpp index 2e89e38b1..ffa80b545 100644 --- a/tests/simulation/basic_request.cpp +++ b/tests/simulation/basic_request.cpp @@ -11,10 +11,27 @@ //#include +#include + +#include "io/v3/simulator.hpp" #include "io/v3/transport.hpp" #include "utils/logging.hpp" int main() { - MG_ASSERT(true); + auto [future, promise] = FuturePromisePair(); + promise.Fill("yo"); + MG_ASSERT(future.Wait() == "yo"); + + /* + auto simulator = Simulator(); + auto addr_1 = Address(); + auto addr_2 = Address(); + auto addr_3 = Address(); + + auto sim_transport_1 = simulator.Register(addr_1, true); + auto sim_transport_2 = simulator.Register(addr_2, true); + auto sim_transport_3 = simulator.Register(addr_3, true); + */ + return 0; }