Add simple test for MgFuture

This commit is contained in:
Tyler Neely 2022-07-04 15:00:32 +00:00
parent 6cec9acbb9
commit 20839b0ae0
4 changed files with 58 additions and 47 deletions

View File

@ -21,7 +21,7 @@
#include "utils/logging.hpp"
#include "errors.hpp"
#include "simulator.hpp"
#include "simulator_handle.hpp"
template <typename T>
class MgPromise;
@ -42,6 +42,7 @@ class Shared {
friend MgFuture<T>;
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<MgFuture<T>, MgPromise<T>> 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<bool>();
promise.Fill(true);
MG_ASSERT(future.Wait() == true);
}
} // namespace _compile_test

View File

@ -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<std::mutex> 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<SimulatorHandle> simulator_handle, Address address)
: simulator_handle_(simulator_handle), address_(address) {}
private:
std::shared_ptr<SimulatorHandle> simulator_handle_;
Address address_;
};
class Simulator {
@ -64,16 +41,3 @@ class Simulator {
private:
std::shared_ptr<SimulatorHandle> 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

View File

@ -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<std::mutex> 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<SimulatorHandle> simulator_handle, Address address)
: simulator_handle_(simulator_handle), address_(address) {}
private:
std::shared_ptr<SimulatorHandle> simulator_handle_;
Address address_;
};

View File

@ -11,10 +11,27 @@
//#include <gtest/gtest.h>
#include <string>
#include "io/v3/simulator.hpp"
#include "io/v3/transport.hpp"
#include "utils/logging.hpp"
int main() {
MG_ASSERT(true);
auto [future, promise] = FuturePromisePair<std::string>();
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;
}