SRS-control 0.1.4
 
Loading...
Searching...
No Matches
TestOutputs.cpp
Go to the documentation of this file.
1#include <filesystem>
2#include <thread>
3
4#include <gtest/gtest.h>
5
6#include <srs/SRSEmulator.hpp>
9
10namespace
11{
12 class Runner
13 {
14 public:
15 Runner() = default;
16
17 [[nodiscard]] auto get_error_msg() const -> const auto& { return error_msg_; }
18 [[nodiscard]] auto get_event_nums() const -> const auto& { return event_nums_; }
19
20 void run(const std::vector<std::string>& output_filenames)
21 {
22
23 try
24 {
25 auto app = srs::App{};
26
27 app.set_fec_data_receiv_port(0);
28 app.set_output_filenames(output_filenames);
29
30 app.init();
31 app.read_data(false);
32 auto* data_reader = app.get_data_reader();
33 auto port_num = data_reader->get_local_port_number();
34 auto emulator = srs::test::SRSEmulator{ "test_data.bin", port_num, app };
35
36 auto analysis_thread = std::jthread(
37 [&app, this]()
38 {
39 app.start_workflow(false);
40 event_nums_ = app.get_workflow_handler().get_processed_hit_number();
41 });
42
43 // std::this_thread::sleep_for(std::chrono::seconds(1));
44 emulator.start_send_data();
45 error_msg_ = app.get_error_string();
46 }
47 catch (std::exception& err)
48 {
49 spdlog::critical("Exception occurred: {}", err.what());
50 }
51 }
52
53 private:
54 std::string error_msg_;
55 uint64_t event_nums_ = 0;
56 };
57
58 namespace fs = std::filesystem;
59
60 auto check_if_file_exist(const std::string& filename) -> bool
61 {
62 auto file_path = fs::path{ filename };
63 return fs::exists(file_path);
64 }
65
66} // namespace
67
68TEST(integration_test_outputfiles, binary_output)
69{
70 const auto filename = std::string{ "test_output.bin" };
71
72 auto runner = Runner{};
73 ASSERT_NO_THROW(runner.run(std::vector{ filename }));
74 EXPECT_EQ(runner.get_error_msg(), "");
75 EXPECT_EQ(runner.get_event_nums(), 0);
76
77 auto res = check_if_file_exist(filename);
78 ASSERT_TRUE(res);
79}
80
81TEST(integration_test_outputfiles, root_output)
82{
83 const auto filename = std::string{ "test_output.root" };
84
85 auto runner = Runner{};
86 ASSERT_NO_THROW(runner.run(std::vector{ filename }));
87 EXPECT_EQ(runner.get_error_msg(), "");
88 EXPECT_GT(runner.get_event_nums(), 0);
89
90 auto res = check_if_file_exist(filename);
91#ifdef HAS_ROOT
92 ASSERT_TRUE(res);
93#else
94 ASSERT_FALSE(res);
95#endif
96}
97
98TEST(integration_test_outputfiles, proto_binary_output)
99{
100 const auto filename = std::string{ "test_output.binpb" };
101
102 auto runner = Runner{};
103 ASSERT_NO_THROW(runner.run(std::vector{ filename }));
104 EXPECT_EQ(runner.get_error_msg(), "");
105 EXPECT_GT(runner.get_event_nums(), 0);
106
107 auto res = check_if_file_exist(filename);
108 ASSERT_TRUE(res);
109}
110
111TEST(integration_test_outputfiles, json_output)
112{
113 const auto filename = std::string{ "test_output.json" };
114
115 auto runner = Runner{};
116 ASSERT_NO_THROW(runner.run(std::vector{ filename }));
117 EXPECT_EQ(runner.get_error_msg(), "");
118 EXPECT_GT(runner.get_event_nums(), 0);
119
120 auto res = check_if_file_exist(filename);
121#ifdef HAS_ROOT
122 ASSERT_TRUE(res);
123#else
124 ASSERT_FALSE(res);
125#endif
126}
127
128TEST(integration_test_outputfiles, all_outputs)
129{
130 const auto filenames = std::vector<std::string>{
131 "test_output1.bin", "test_output2.bin", "test_output1.json", "test_output1.root", "test_output1.binpb"
132 };
133
134 auto runner = Runner{};
135 ASSERT_NO_THROW(runner.run(filenames));
136 EXPECT_EQ(runner.get_error_msg(), "");
137 EXPECT_GT(runner.get_event_nums(), 0);
138
139 for (const auto& filename : filenames)
140 {
141 auto res = check_if_file_exist(filename);
142 if (filename == "test_output1.root")
143 {
144#ifdef HAS_ROOT
145 ASSERT_TRUE(res);
146#else
147 ASSERT_FALSE(res);
148#endif
149 }
150 else
151 {
152 ASSERT_TRUE(res);
153 }
154 }
155}
TEST(integration_test_outputfiles, binary_output)