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

Bugfixes: JServices #386

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ccpp-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ jobs:
- name: jana-unit-tests
run: |
echo "--- Running jana-unit-tests ------------------------------"
ctest --test-dir build -R jana-unit-tests
ctest --test-dir build -R jana-unit-tests --rerun-failed --output-on-failure
2 changes: 1 addition & 1 deletion src/libraries/JANA/Components/JOmniFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ class JOmniFactory : public JMultifactory, public jana::components::JHasInputs {
variadic_output_count += 1;
}
}
size_t variadic_output_collection_count = FindVariadicCollectionCount(m_outputs.size(), variadic_output_count, output_collection_names.size(), true);
size_t variadic_output_collection_count = FindVariadicCollectionCount(m_outputs.size(), variadic_output_count, output_collection_names.size(), false);

// Set output collection names and create corresponding helper factories
i = 0;
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/JANA/JApplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,10 @@ void JApplication::Initialize() {

topology_builder->create_topology();
auto execution_engine = m_service_locator->get<JExecutionEngine>();

// Make sure that Init() is called on any remaining JServices
m_service_locator->wire_everything();

m_initialized = true;
// This needs to be at the end so that m_initialized==false while InitPlugin() is being called
}
Expand Down
1 change: 1 addition & 0 deletions src/libraries/JANA/Services/JServiceLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class JServiceLocator {
std::lock_guard<std::mutex> lock(mutex);
auto svc = std::dynamic_pointer_cast<JService>(t);
assert(svc != nullptr);
svc->SetTypeName(JTypeInfo::demangle<T>());
underlying[std::type_index(typeid(T))] = svc;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/JANA/Services/JWiringService.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JWiringService : public JService {
};

private:
Parameter<std::string> m_wirings_input_file {this, "jana:wiring_file", "wiring.toml",
Parameter<std::string> m_wirings_input_file {this, "jana:wiring_file", "",
"Path to TOML file containing wiring definitions"};

Parameter<bool> m_strict_inheritance {this, "jana:wiring_strictness", true,
Expand Down
1 change: 1 addition & 0 deletions src/programs/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(TEST_SOURCES
Components/JFactoryTests.cc
Components/JFactoryGeneratorTests.cc
Components/JMultiFactoryTests.cc
Components/JServiceTests.cc
Components/UnfoldTests.cc
Components/UserExceptionTests.cc

Expand Down
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
82 changes: 82 additions & 0 deletions src/programs/unit_tests/Components/JServiceTests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

#include <catch.hpp>
#include <JANA/JApplication.h>
#include <JANA/JService.h>
#include <JANA/Components/JOmniFactory.h>
#include <JANA/Components/JOmniFactoryGeneratorT.h>

namespace jana::jservicetests {

class DummyService: public JService {
bool init_started = false;
bool init_finished = false;
public:
void Init() override {
LOG_INFO(GetLogger()) << "Starting DummyService::Init()" << LOG_END;
init_started = true;
throw std::runtime_error("Something goes wrong");
init_finished = true;
LOG_INFO(GetLogger()) << "Finishing DummyService::Init()" << LOG_END;
}
};

TEST_CASE("JServiceTests_ExceptionInInit") {
JApplication app;
app.ProvideService(std::make_shared<DummyService>());
try {
app.Initialize();
REQUIRE(1 == 0); // Shouldn't be reachable

auto sut = app.GetService<DummyService>();
REQUIRE(1 == 0); // Definitely shouldn't be reachable
}
catch (JException& e) {
REQUIRE(e.GetMessage() == "Something goes wrong");
REQUIRE(e.type_name == "jana::jservicetests::DummyService");
REQUIRE(e.function_name == "JService::Init");
}
}

struct DummyData {int x;};

struct DummyOmniFactory: public jana::components::JOmniFactory<DummyOmniFactory> {

Service<DummyService> m_svc {this};
Output<DummyData> m_output {this};

void Configure() {
}

void ChangeRun(int32_t /*run_nr*/) {
}

void Execute(int32_t /*run_nr*/, uint64_t /*evt_nr*/) {
m_output().push_back(new DummyData{22});
}
};

TEST_CASE("JServiceTests_ExceptionInInit_Issue381") {
JApplication app;
app.ProvideService(std::make_shared<DummyService>());

auto gen = new components::JOmniFactoryGeneratorT<DummyOmniFactory>();
gen->AddWiring("dummy", {}, {"data"});
app.Add(gen);

try {
app.Initialize();
REQUIRE(1 == 0); // Shouldn't be reachable
auto event = std::make_shared<JEvent>(&app);
auto data = event->Get<DummyData>("data");
REQUIRE(1 == 0); // Definitely shouldn't be reachable
REQUIRE(data.at(0)->x == 22);
}
catch (JException& e) {
REQUIRE(e.GetMessage() == "Something goes wrong");
REQUIRE(e.type_name == "jana::jservicetests::DummyService");
REQUIRE(e.function_name == "JService::Init");
}
}

} // namespace jana::jservicetests

Loading