Skip to content

Commit 4a97074

Browse files
bors[bot]gerboland
andcommitted
Merge #796
796: Fulfil promise for restart error condition r=townsend2010 a=gerboland Fixes #764. We were omitting to set a promised future, which caused the client to hang. I've added a basic test to catch this behaviour for this command. Later PR will try to generalise it for the other commands. Co-authored-by: Gerry Boland <[email protected]>
2 parents 7e4c7d1 + 573861b commit 4a97074

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

src/daemon/daemon.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,18 +1493,22 @@ try // clang-format on
14931493
const auto& instances = instances_and_status.first; // use structured bindings instead in C++17
14941494
auto& status = instances_and_status.second; // idem
14951495

1496-
if (status.ok())
1496+
if (!status.ok())
14971497
{
1498-
status = cmd_vms(instances,
1499-
std::bind(&Daemon::reboot_vm, this, std::placeholders::_1)); // 1st pass to reboot all targets
1498+
return status_promise->set_value(status);
1499+
}
15001500

1501-
if (status.ok())
1502-
{
1503-
auto future_watcher = create_future_watcher();
1504-
future_watcher->setFuture(QtConcurrent::run(this, &Daemon::async_wait_for_ready_all<RestartReply>, server,
1505-
instances, status_promise));
1506-
}
1501+
status = cmd_vms(instances,
1502+
std::bind(&Daemon::reboot_vm, this, std::placeholders::_1)); // 1st pass to reboot all targets
1503+
1504+
if (!status.ok())
1505+
{
1506+
return status_promise->set_value(status);
15071507
}
1508+
1509+
auto future_watcher = create_future_watcher();
1510+
future_watcher->setFuture(
1511+
QtConcurrent::run(this, &Daemon::async_wait_for_ready_all<RestartReply>, server, instances, status_promise));
15081512
}
15091513
catch (const std::exception& e)
15101514
{
@@ -1756,7 +1760,7 @@ void mp::Daemon::persist_instances()
17561760
mp::write_json(instance_records_json, data_dir.filePath(instance_db_name));
17571761
}
17581762

1759-
void mp::Daemon::start_mount(VirtualMachine *vm, const std::string& name, const std::string& source_path,
1763+
void mp::Daemon::start_mount(VirtualMachine* vm, const std::string& name, const std::string& source_path,
17601764
const std::string& target_path, const std::unordered_map<int, int>& gid_map,
17611765
const std::unordered_map<int, int>& uid_map)
17621766
{
@@ -2058,7 +2062,7 @@ grpc::Status mp::Daemon::cmd_vms(const std::vector<std::string>& tgts, std::func
20582062
return grpc::Status::OK;
20592063
}
20602064

2061-
void mp::Daemon::install_sshfs(VirtualMachine *vm, const std::string& name)
2065+
void mp::Daemon::install_sshfs(VirtualMachine* vm, const std::string& name)
20622066
{
20632067
auto& key_provider = *config->ssh_key_provider;
20642068

tests/test_daemon.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ using namespace testing;
5858

5959
namespace
6060
{
61+
template<typename R>
62+
bool is_ready(std::future<R> const& f)
63+
{ return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }
64+
6165
struct MockDaemon : public mp::Daemon
6266
{
6367
using mp::Daemon::Daemon;
@@ -336,6 +340,21 @@ TEST_F(Daemon, provides_version)
336340
EXPECT_THAT(stream.str(), HasSubstr(mp::version_string));
337341
}
338342

343+
TEST_F(Daemon, failed_restart_command_returns_fulfilled_promise)
344+
{
345+
mp::Daemon daemon{config_builder.build()};
346+
347+
auto nonexistant_instance = new mp::InstanceNames; // on heap as *Request takes ownership
348+
nonexistant_instance->add_instance_name("nonexistant");
349+
mp::RestartRequest request;
350+
request.set_allocated_instance_names(nonexistant_instance);
351+
std::promise<grpc::Status> status_promise;
352+
353+
daemon.restart(&request, nullptr, &status_promise);
354+
EXPECT_TRUE(is_ready(status_promise.get_future()));
355+
}
356+
357+
339358
namespace
340359
{
341360
struct DaemonCreateLaunchTestSuite : public Daemon, public WithParamInterface<std::string>

0 commit comments

Comments
 (0)