Skip to content

Commit

Permalink
Migrate test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Apr 28, 2024
1 parent 3548f66 commit 66cc351
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 43 deletions.
14 changes: 9 additions & 5 deletions src/programs/unit_tests/BarrierEventTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,27 @@ class BarrierSource : public JEventSource {

public:

BarrierSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
}

void GetEvent(std::shared_ptr<JEvent> event) override {
Result Emit(JEvent& event) override {
event_count++;

if (event_count >= 100) {
throw RETURN_STATUS::kNO_MORE_EVENTS;
return Result::FailureFinished;
}

LOG << "Emitting event " << event_count << LOG_END;
event->SetEventNumber(event_count);
event.SetEventNumber(event_count);

if (event_count % 10 == 0) {
event->SetSequential(true);
event.SetSequential(true);
}

return Result::Success;
}
};

Expand Down
8 changes: 6 additions & 2 deletions src/programs/unit_tests/ExactlyOnceTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ struct SimpleSource : public JEventSource {
std::atomic_int close_count {0};
std::atomic_int event_count {0};

SimpleSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
open_count += 1;
}

void GetEvent(std::shared_ptr<JEvent>) override {
Result Emit(JEvent&) override {
if (++event_count == 5) {
throw JEventSource::RETURN_STATUS::kNO_MORE_EVENTS;
return Result::FailureFinished;
}
return Result::Success;
}
void Close() override {
close_count += 1;
Expand Down
9 changes: 6 additions & 3 deletions src/programs/unit_tests/GetObjectsTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ struct Obj3 : public JObject { int data; };
struct Obj4 : public JObject { int data; };

class Src : public JEventSource {

void GetEvent(std::shared_ptr<JEvent> event) override {
Src() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}
Result Emit(JEvent& event) override {
auto obj = new Obj1;
obj->data = 21;
event->Insert(obj);
event.Insert(obj);
return Result::Success;
}
bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* fac) override {
if (fac->GetObjectName() == "Obj2") {
Expand Down
9 changes: 2 additions & 7 deletions src/programs/unit_tests/JEventProcessorSequentialTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace jeventprocessorsequentialtests {
// the linker will cheerfully not notice and you will get VERY weird errors.
// Hence, we protect each Catch test with its own namespace.

struct DummySource : public JEventSource {

// By default, this will emit empty events with event numbers 0,1,2...
void GetEvent(std::shared_ptr<JEvent>) override {}
};

struct MyRootProcessor : public JEventProcessorSequentialRoot {
std::vector<std::pair<int, int>> access_log;
Expand Down Expand Up @@ -51,7 +46,7 @@ struct MyRootProcessor : public JEventProcessorSequentialRoot {
TEST_CASE("JEventProcessorSequentialRootTests") {

JApplication app;
app.Add(new DummySource);
app.Add(new JEventSource());
app.SetParameterValue("nthreads", 4);
app.SetParameterValue("jana:nevents", 4);
app.SetParameterValue("jana:event_source_chunksize", 1);
Expand Down Expand Up @@ -138,7 +133,7 @@ struct MySeqProcessor : public JEventProcessorSequential {
TEST_CASE("JEventProcessorSequentialTests") {

JApplication app;
app.Add(new DummySource);
app.Add(new JEventSource());
app.SetParameterValue("nthreads", 4);
app.SetParameterValue("jana:nevents", 4);
app.SetParameterValue("jana:event_source_chunksize", 1);
Expand Down
9 changes: 1 addition & 8 deletions src/programs/unit_tests/JFactoryDefTagsTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ TEST_CASE("MediumDefTags") {
}

namespace deftagstest {
struct DummySource : public JEventSource {
DummySource() {
SetTypeName(NAME_OF_THIS);
};

void GetEvent(std::shared_ptr<JEvent>) override {};
};

struct DummyProcessor : public JEventProcessor {
double E = 0.5;
Expand All @@ -135,7 +128,7 @@ TEST_CASE("LargeDefTags") {
JApplication app;
app.Add(new JFactoryGeneratorT<Fac1>);
app.Add(new JFactoryGeneratorT<Fac2>);
app.Add(new DummySource);
app.Add(new JEventSource);
auto proc = new DummyProcessor;
app.Add(proc);
app.SetParameterValue("jana:nevents", 3);
Expand Down
7 changes: 6 additions & 1 deletion src/programs/unit_tests/JFactoryTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ struct JFactoryTestExceptingInInitFactory : public JFactoryT<JFactoryTestDummyOb

struct JFactoryTestDummySource: public JEventSource {

void GetEvent(std::shared_ptr<JEvent>) override {
JFactoryTestDummySource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

Result Emit(JEvent&) override {
return Result::Success;
};

bool GetObjects(const std::shared_ptr<const JEvent>&, JFactory* aFactory) override {
Expand Down
7 changes: 6 additions & 1 deletion src/programs/unit_tests/ScaleTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,14 @@
namespace scaletest {
struct DummySource : public JEventSource {

void GetEvent(std::shared_ptr<JEvent>) override {
DummySource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

Result Emit(JEvent&) override {
consume_cpu_ms(20);
std::this_thread::sleep_for(std::chrono::nanoseconds(1));
return Result::Success;
}
};

Expand Down
11 changes: 7 additions & 4 deletions src/programs/unit_tests/SubeventTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,18 @@ TEST_CASE("Basic subevent arrow functionality") {
}

struct SimpleSource : public JEventSource {
void GetEvent(std::shared_ptr<JEvent> event) override {
SimpleSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}
Result Emit(JEvent& event) override {
if (GetEventCount() == 10) return Result::FailureFinished;
std::vector<MyInput*> inputs;
inputs.push_back(new MyInput(22,3.6));
inputs.push_back(new MyInput(23,3.5));
inputs.push_back(new MyInput(24,3.4));
inputs.push_back(new MyInput(25,3.3));
event->Insert(inputs);
if (GetEventCount() == 10)
throw JEventSource::RETURN_STATUS::kNO_MORE_EVENTS;
event.Insert(inputs);
return Result::Success;
}
};

Expand Down
24 changes: 18 additions & 6 deletions src/programs/unit_tests/TerminationTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,49 @@
#include "catch.hpp"

struct InterruptedSource : public JEventSource {
InterruptedSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}
void Open() override { GetApplication()->Stop(); }
void GetEvent(std::shared_ptr<JEvent>) override {}
Result Emit(JEvent&) override { return Result::Success; }
};

struct BoundedSource : public JEventSource {

uint64_t event_count = 0;

BoundedSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
}

void GetEvent(std::shared_ptr<JEvent>) override {
Result Emit(JEvent&) override {
if (event_count >= 10) {
throw JEventSource::RETURN_STATUS::kNO_MORE_EVENTS;
return Result::FailureFinished;
}
event_count += 1;
return Result::Success;
}
};

struct UnboundedSource : public JEventSource {

uint64_t event_count = 0;

UnboundedSource() {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
}

void GetEvent(std::shared_ptr<JEvent> event) override {
Result Emit(JEvent& event) override {
event_count += 1;
event->SetEventNumber(event_count);
event.SetEventNumber(event_count);
std::this_thread::sleep_for(std::chrono::milliseconds(200));
// jout << "Emitting " << event_count << jendl;
return Result::Success;
}
};

Expand Down
9 changes: 6 additions & 3 deletions src/programs/unit_tests/TimeoutTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ struct SourceWithTimeout : public JEventSource {

: timeout_on_event_nr(timeout_on_event_nr)
, first_event_delay_ms(first_delay_ms)
{ }
{
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
}

void GetEvent(std::shared_ptr<JEvent>) override {
Result Emit(JEvent&) override {
event_count += 1;
std::cout << "Processing event # " << event_count << std::endl;
std::flush(std::cout);
Expand All @@ -35,14 +37,15 @@ struct SourceWithTimeout : public JEventSource {
}

if (event_count == 100) {
throw RETURN_STATUS::kNO_MORE_EVENTS;
return Result::FailureFinished;
}

if (event_count == timeout_on_event_nr) {
while (true) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}; // Endless loop
}
return Result::Success;
}
};

Expand Down
9 changes: 6 additions & 3 deletions src/programs/unit_tests/UserExceptionTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,26 @@ struct FlakySource : public JEventSource {
int event_count = 0;

FlakySource(bool open_excepts, bool getevent_excepts)
: open_excepts(open_excepts), getevent_excepts(getevent_excepts) {}
: open_excepts(open_excepts), getevent_excepts(getevent_excepts) {
SetCallbackStyle(CallbackStyle::ExpertMode);
}

void Open() override {
if (open_excepts) {
throw JException("Unable to open source!");
}
}

void GetEvent(std::shared_ptr<JEvent>) override {
Result Emit(JEvent&) override {

if (++event_count > 10) {
throw JEventSource::RETURN_STATUS::kNO_MORE_EVENTS;
return Result::FailureFinished;
}

if (getevent_excepts) {
throw JException("Unable to getEvent!");
}
return Result::Success;
}
};

Expand Down

0 comments on commit 66cc351

Please sign in to comment.