Skip to content

Commit

Permalink
Test JFactory::GetObjects caching behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Nov 25, 2024
1 parent 0d3563d commit aa9f6b7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
42 changes: 41 additions & 1 deletion src/programs/unit_tests/Components/JFactoryTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


#include "JANA/Components/JComponentFwd.h"
#include "JANA/JApplicationFwd.h"
#include "JANA/JFactory.h"
#include "catch.hpp"
#include "JFactoryTests.h"
Expand Down Expand Up @@ -566,4 +567,43 @@ TEST_CASE("JFactory_ExceptionHandling") {
}
}


TEST_CASE("JFactory_GetObjects_Caching") {
JApplication app;
app.Add(new JFactoryGeneratorT<JFactoryT<JFactoryTestDummyObject>>());
app.Add(new JFactoryGeneratorT<JFactoryT<DifferentDummyObject>>());
auto source = new JFactoryTestDummySource;
auto event = std::make_shared<JEvent>(&app);
event->SetJEventSource(source);

SECTION("RepeatedGetObjectsAreCached") {
auto dummies = event->Get<JFactoryTestDummyObject>();
REQUIRE(dummies.at(0)->data == 8);
REQUIRE(source->get_objects_count == 1);
REQUIRE(source->get_objects_dummy_count == 1);

dummies = event->Get<JFactoryTestDummyObject>();
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<JFactoryTestDummyObject>();
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<DifferentDummyObject>();
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<DifferentDummyObject>();
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);
}
}
26 changes: 22 additions & 4 deletions src/programs/unit_tests/Components/JFactoryTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -69,6 +74,10 @@ struct JFactoryTestExceptingInInitFactory : public JFactoryT<JFactoryTestDummyOb

struct JFactoryTestDummySource: public JEventSource {

int get_objects_count = 0;
int get_objects_dummy_count = 0;
int get_objects_different_count = 0;

JFactoryTestDummySource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
EnableGetObjects();
Expand All @@ -79,10 +88,19 @@ struct JFactoryTestDummySource: public JEventSource {
};

bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* aFactory) override {
auto factory = dynamic_cast<JFactoryT<JFactoryTestDummyObject>*>(aFactory);
if (factory != nullptr) {
factory->Insert(new JFactoryTestDummyObject(8));
factory->Insert(new JFactoryTestDummyObject(88));
get_objects_count += 1;
auto dummy_factory = dynamic_cast<JFactoryT<JFactoryTestDummyObject>*>(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<JFactoryT<DifferentDummyObject>*>(aFactory);
if (different_factory != nullptr) {
get_objects_different_count += 1;
different_factory->Insert(new DifferentDummyObject(123.));
return true;
}
return false;
}
Expand Down

0 comments on commit aa9f6b7

Please sign in to comment.