diff --git a/src/programs/unit_tests/Components/JFactoryTests.cc b/src/programs/unit_tests/Components/JFactoryTests.cc index 07ada4b21..7149dfe9b 100644 --- a/src/programs/unit_tests/Components/JFactoryTests.cc +++ b/src/programs/unit_tests/Components/JFactoryTests.cc @@ -4,6 +4,7 @@ #include "JANA/Components/JComponentFwd.h" +#include "JANA/JApplicationFwd.h" #include "JANA/JFactory.h" #include "catch.hpp" #include "JFactoryTests.h" @@ -566,4 +567,43 @@ TEST_CASE("JFactory_ExceptionHandling") { } } - +TEST_CASE("JFactory_GetObjects_Caching") { + JApplication app; + app.Add(new JFactoryGeneratorT>()); + app.Add(new JFactoryGeneratorT>()); + auto source = new JFactoryTestDummySource; + auto event = std::make_shared(&app); + event->SetJEventSource(source); + + SECTION("RepeatedGetObjectsAreCached") { + auto dummies = event->Get(); + REQUIRE(dummies.at(0)->data == 8); + REQUIRE(source->get_objects_count == 1); + REQUIRE(source->get_objects_dummy_count == 1); + + dummies = event->Get(); + REQUIRE(dummies.at(0)->data == 8); + REQUIRE(source->get_objects_count == 1); + REQUIRE(source->get_objects_dummy_count == 1); + } + + SECTION("DifferentGetObjectsAreNotCached") { + auto dummies = event->Get(); + REQUIRE(dummies.at(0)->data == 8); + REQUIRE(source->get_objects_count == 1); + REQUIRE(source->get_objects_dummy_count == 1); + REQUIRE(source->get_objects_different_count == 0); + + auto different = event->Get(); + REQUIRE(different.at(0)->E == 123.0); + REQUIRE(source->get_objects_count == 2); + REQUIRE(source->get_objects_dummy_count == 1); + REQUIRE(source->get_objects_different_count == 1); + + different = event->Get(); + REQUIRE(different.at(0)->E == 123.0); + REQUIRE(source->get_objects_count == 2); + REQUIRE(source->get_objects_dummy_count == 1); + REQUIRE(source->get_objects_different_count == 1); + } +} diff --git a/src/programs/unit_tests/Components/JFactoryTests.h b/src/programs/unit_tests/Components/JFactoryTests.h index e700b86c4..e2cbc0188 100644 --- a/src/programs/unit_tests/Components/JFactoryTests.h +++ b/src/programs/unit_tests/Components/JFactoryTests.h @@ -23,6 +23,11 @@ struct JFactoryTestDummyObject : public JObject { } }; +struct DifferentDummyObject : public JObject { + double E; + DifferentDummyObject(double E) : E(E) {} +}; + /// DummyFactory is a trivial JFactory which reports how often its member functions get called @@ -69,6 +74,10 @@ struct JFactoryTestExceptingInInitFactory : public JFactoryT&, JFactory* aFactory) override { - auto factory = dynamic_cast*>(aFactory); - if (factory != nullptr) { - factory->Insert(new JFactoryTestDummyObject(8)); - factory->Insert(new JFactoryTestDummyObject(88)); + get_objects_count += 1; + auto dummy_factory = dynamic_cast*>(aFactory); + if (dummy_factory != nullptr) { + get_objects_dummy_count += 1; + dummy_factory->Insert(new JFactoryTestDummyObject(8)); + dummy_factory->Insert(new JFactoryTestDummyObject(88)); + return true; + } + auto different_factory = dynamic_cast*>(aFactory); + if (different_factory != nullptr) { + get_objects_different_count += 1; + different_factory->Insert(new DifferentDummyObject(123.)); + return true; } return false; }