Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelsavannah committed Sep 19, 2018
1 parent 357de25 commit 2cd44d4
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion chaos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ add_library(chaos
chaos.cpp
)

target_link_libraries(chaos)
target_link_libraries(chaos options)

add_subdirectory(test)
2 changes: 1 addition & 1 deletion chaos/chaos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ chaos::chaos(std::shared_ptr<bzn::asio::io_context_base> io_context, const bzn::
void
chaos::start()
{
if (!this->options.get_simple_options().get<bool>(bzn::option_names::CHAOS_ENABLED))
if (this->options.get_simple_options().get<bool>(bzn::option_names::CHAOS_ENABLED))
{
std::call_once(
this->start_once, [this]()
Expand Down
3 changes: 3 additions & 0 deletions chaos/test/chaos_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ class chaos_test : public Test
.WillRepeatedly(Invoke(
[&](auto handler){this->node_crash_handler = handler;}
));

this->options.get_mutable_simple_options().set(bzn::option_names::CHAOS_ENABLED, "true");
}

void build_chaos()
{
this->chaos = std::make_shared<bzn::chaos>(this->mock_io_context, this->options);
this->chaos->start();
}

};
Expand Down
6 changes: 6 additions & 0 deletions options/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ options::get_simple_options() const
return this->raw_opts;
}

simple_options&
options::get_mutable_simple_options()
{
return this->raw_opts;
}

boost::asio::ip::tcp::endpoint
options::get_listener() const
{
Expand Down
2 changes: 2 additions & 0 deletions options/options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace bzn
public:
const simple_options& get_simple_options() const override;

simple_options& get_mutable_simple_options() override;

bool parse_command_line(int argc, const char* argv[]);

boost::asio::ip::tcp::endpoint get_listener() const override;
Expand Down
5 changes: 5 additions & 0 deletions options/options_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ namespace bzn
*/
virtual const simple_options& get_simple_options() const = 0;

/**
* @return the raw_options container for accessing simple options
*/
virtual simple_options& get_mutable_simple_options() = 0;

/**
* Get the address and port for the node to listen on
* @return endpoint
Expand Down
16 changes: 15 additions & 1 deletion options/simple_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ namespace
simple_options::simple_options()
: options_root("Core configuration")
{
this->build_options();
}

bool
simple_options::parse(int argc, const char* argv[])
{
this->build_options();
return this->handle_command_line_options(argc, argv) && this->handle_config_file_options() && this->validate_options();
}

Expand Down Expand Up @@ -321,3 +321,17 @@ simple_options::has(const std::string& option_name) const
{
return this->vm.count(option_name) > 0;
}

void
simple_options::set(const std::string& option_name, const std::string& option_value)
{
po::basic_option<char> opt;
opt.string_key = option_name;
opt.value.push_back(option_value);

boost::program_options::parsed_options parsed(&(this->options_root));
parsed.options.push_back(opt);

this->vm.erase(option_name);
po::store(parsed, this->vm);
}
5 changes: 5 additions & 0 deletions options/simple_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ namespace bzn
}
}

/*
* Assign a value to an option at runtime
*/
void set(const std::string& option_name, const std::string& option_value);

/*
* Do we have a value for an option (either explicit or default)
*/
Expand Down
12 changes: 12 additions & 0 deletions options/test/options_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,16 @@ TEST_F(options_file_test, test_that_uuid_and_pubkey_conflict)
bzn::options options;
this->save_options_file(compose_config_data(DEFAULT_CONFIG_CONTENT, "\"public_key_file\": \"somefile\""));
EXPECT_FALSE(options.parse_command_line(1, NO_ARGS));
}

TEST_F(options_file_test, test_set_option_at_runtime)
{
bzn::options options;
this->save_options_file(DEFAULT_CONFIG_DATA);
EXPECT_TRUE(options.parse_command_line(1, NO_ARGS));

EXPECT_TRUE(options.get_simple_options().get<bool>(bzn::option_names::DEBUG_LOGGING));
options.get_mutable_simple_options().set(bzn::option_names::DEBUG_LOGGING, "false");
EXPECT_FALSE(options.get_simple_options().get<bool>(bzn::option_names::DEBUG_LOGGING));

}

0 comments on commit 2cd44d4

Please sign in to comment.