#include #include #include namespace { struct test_bus final : kapi::devices::bus { using kapi::devices::bus::bus; }; struct test_device final : kapi::devices::device { using kapi::devices::device::device; }; } // namespace SCENARIO("the device registry holds devices weakly and prunes stale entries lazily", "[devices][registry]") { GIVEN("a device attached to a bus, and therefore registered") { auto owner = kstd::make_shared("registry_test_owner"); owner->add_child(kstd::make_shared("registry_test_device")); THEN("it is discoverable via find_devices") { auto found = kapi::devices::device_registry::get().find("registry_test_device"); REQUIRE(found); REQUIRE(found->name() == "registry_test_device"); } THEN("find_device reports no match for an uknown name") { REQUIRE_FALSE(kapi::devices::device_registry::get().find("does_not_exist")); } WHEN("every strong reference to the device is dropped") { owner.reset(); THEN("find_device no longer resolves it") { REQUIRE_FALSE(kapi::devices::device_registry::get().find("registry_test_device")); } } } GIVEN("a device name whose previous registration has since expired") { { auto owner = kstd::make_shared("registry_reuse_owner_1"); owner->add_child(kstd::make_shared("registry_reuse_device")); } WHEN("a new device is registered under the very same name") { auto new_device = kstd::make_shared("registry_reuse_device"); auto registered = kapi::devices::device_registry::get().add(new_device); THEN("registration succeeds") { REQUIRE(registered); REQUIRE(kapi::devices::device_registry::get().find("registry_reuse_device") == new_device); } } } }