Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more btests to replace "unit" tests #417

Merged
merged 9 commits into from
Jul 14, 2024
7 changes: 0 additions & 7 deletions libbroker/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ if ( BROKER_DISABLE_TESTS )
endif ()

set(BROKER_TEST_SRC
# broker/core.test.cc
# broker/integration.test.cc
# broker/internal/data_generator.test.cc
# broker/internal/generator_file_writer.test.cc
# broker/internal/json_type_mapper.test.cc
Expand All @@ -173,21 +171,16 @@ set(BROKER_TEST_SRC
broker/format/bin.test.cc
broker/format/json.test.cc
broker/internal/channel.test.cc
broker/internal/core_actor.test.cc
broker/internal/json.test.cc
broker/internal/metric_collector.test.cc
broker/internal/metric_exporter.test.cc
broker/internal/wire_format.test.cc
broker/master.test.cc
broker/peering.test.cc
broker/publisher.test.cc
broker/radix_tree.test.cc
broker/shutdown.test.cc
broker/status.test.cc
broker/status_subscriber.test.cc
broker/store.test.cc
broker/store_event.test.cc
broker/subscriber.test.cc
broker/telemetry/histogram.test.cc
broker/topic.test.cc
broker/variant.test.cc
Expand Down
6 changes: 3 additions & 3 deletions libbroker/broker/alm/multipath.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ using namespace broker;

namespace {

struct fixture : base_fixture {
struct fixture : public ids_fixture {
void stringify(const multipath& path, std::string& result) {
if (!path.head().id()) {
result += "()";
Expand Down Expand Up @@ -131,13 +131,13 @@ TEST(multipaths are serializable) {
caf::binary_serializer::container_type buf;
MESSAGE("serializer the path into a buffer");
{
caf::binary_serializer sink{sys, buf};
caf::binary_serializer sink{nullptr, buf};
CHECK(sink.apply(path));
}
multipath copy;
MESSAGE("deserializers a copy from the path from the buffer");
{
caf::binary_deserializer source{sys, buf};
caf::binary_deserializer source{nullptr, buf};
CHECK(source.apply(copy));
}
MESSAGE("after a serialization roundtrip, the path is equal to its copy");
Expand Down
2 changes: 1 addition & 1 deletion libbroker/broker/alm/routing_table.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using namespace broker::literals;

namespace {

struct fixture : base_fixture {
struct fixture : ids_fixture {
endpoint_id A;

endpoint_id B;
Expand Down
2 changes: 1 addition & 1 deletion libbroker/broker/backend.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class meta_backend : public detail::abstract_backend {
std::vector<std::string> paths_;
};

struct fixture : base_fixture {
struct fixture {
fixture() {
auto opts = backend_options{{"path", detail::make_temp_file_name()}};
backend = std::make_unique<meta_backend>(std::move(opts));
Expand Down
30 changes: 19 additions & 11 deletions libbroker/broker/broker-test.test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,40 @@ std::string_view id_strings[] = {

} // namespace

base_fixture::base_fixture()
: ep(make_config()),
sys(internal::endpoint_access{&ep}.sys()),
self(sys),
sched(dynamic_cast<scheduler_type&>(sys.scheduler())) {
ids_fixture::ids_fixture() {
for (char id = 'A'; id <= 'Z'; ++id) {
auto index = id - 'A';
str_ids[id] = id_strings[index];
convert(std::string{id_strings[index]}, ids[id]);
}
}

base_fixture::~base_fixture() {
run();
// Our core might do some messaging in its dtor, hence we need to make sure
// messages are handled when enqueued to avoid blocking.
sched.inline_all_enqueues();
ids_fixture::~ids_fixture() {
// Only defined because the id_fixture is a base class.
}

char base_fixture::id_by_value(const broker::endpoint_id& value) {
char ids_fixture::id_by_value(const broker::endpoint_id& value) {
for (const auto& [key, val] : ids)
if (val == value)
return key;
FAIL("value not found: " << value);
}

base_fixture::base_fixture()
: ep(make_config()),
sys(internal::endpoint_access{&ep}.sys()),
self(sys),
sched(dynamic_cast<scheduler_type&>(sys.scheduler())) {
// nop
}

base_fixture::~base_fixture() {
run();
// Our core might do some messaging in its dtor, hence we need to make sure
// messages are handled when enqueued to avoid blocking.
sched.inline_all_enqueues();
}

configuration base_fixture::make_config() {
broker_options options;
options.disable_ssl = true;
Expand Down
26 changes: 17 additions & 9 deletions libbroker/broker/broker-test.test.hh
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,25 @@ private:
}
};

class ids_fixture {
public:
ids_fixture();

virtual ~ids_fixture();

char id_by_value(const broker::endpoint_id& value);

// A couple of predefined endpoint IDs for testing purposes. Filled from A-Z.
std::map<char, broker::endpoint_id> ids;

// String representation of all `ids`.
std::map<char, std::string> str_ids;
};

/// A fixture that hosts an endpoint configured with `test_coordinator` as
/// scheduler as well as a `scoped_actor`.
class base_fixture : public time_aware_fixture<base_fixture> {
class base_fixture : public time_aware_fixture<base_fixture>,
public ids_fixture {
public:
struct endpoint_state {
broker::endpoint_id id;
Expand All @@ -198,20 +214,12 @@ public:
caf::scoped_actor self;
scheduler_type& sched;

// A couple of predefined endpoint IDs for testing purposes. Filled from A-Z.
std::map<char, broker::endpoint_id> ids;

// String representation of all `ids`.
std::map<char, std::string> str_ids;

using super::run;

void run();

void consume_message();

char id_by_value(const broker::endpoint_id& value);

/// Dereferences `hdl` and downcasts it to `T`.
template <class T = caf::scheduled_actor, class Handle = caf::actor>
static T& deref(const Handle& hdl) {
Expand Down
Loading
Loading