Skip to content

Commit 11839c7

Browse files
authored
Improve CsvWriter and InlinedVector tests
1 parent 70954a0 commit 11839c7

File tree

3 files changed

+178
-8
lines changed

3 files changed

+178
-8
lines changed

include/quill/CsvWriter.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ class CsvWriter
163163
* Constructs a CsvWriter object that writes to multiple sinks.
164164
*
165165
* @param unique_name A unique name for this CsvWriter instance.
166-
* @param sinks An initializer list of sinks to output the data to.
166+
* @param sinks A list of sinks to output the data to.
167167
* @param should_write_header Whether to write the header at the beginning of the CSV file.
168168
*/
169-
CsvWriter(std::string const& unique_name, std::initializer_list<std::shared_ptr<Sink>> sinks,
169+
CsvWriter(std::string const& unique_name, std::vector<std::shared_ptr<Sink>> sinks,
170170
bool should_write_header = true)
171171
{
172172
_logger = frontend_t::create_or_get_logger(_logger_name_prefix + unique_name, sinks,
@@ -181,10 +181,7 @@ class CsvWriter
181181
/**
182182
* Destructor for CsvWriter. Flushes the log and removes the logger.
183183
*/
184-
~CsvWriter()
185-
{
186-
frontend_t::remove_logger(_logger);
187-
}
184+
~CsvWriter() { frontend_t::remove_logger_blocking(_logger); }
188185

189186
/**
190187
* Appends a row to the CSV file. This function is also thread safe.

test/integration_tests/CsvWritingTest.cpp

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ TEST_CASE("csv_writing")
2121
static constexpr char const* filename_2 = "orders_rotating.csv";
2222
static constexpr char const* filename_2_1 = "orders_rotating.1.csv";
2323
static constexpr char const* filename_2_2 = "orders_rotating.2.csv";
24+
static constexpr char const* filename_3 = "orders_3.csv";
25+
static constexpr char const* filename_4 = "orders_4.csv";
26+
static constexpr char const* filename_5 = "orders_5.csv";
27+
static constexpr char const* filename_6 = "orders_6.csv";
2428

2529
// Start the backend thread
2630
quill::BackendOptions backend_options;
@@ -58,6 +62,71 @@ TEST_CASE("csv_writing")
5862
}
5963
}
6064

65+
{
66+
auto file_sink = quill::Frontend::create_or_get_sink<quill::FileSink>(
67+
filename_3,
68+
[]()
69+
{
70+
quill::FileSinkConfig cfg;
71+
cfg.set_open_mode('w');
72+
cfg.set_filename_append_option(quill::FilenameAppendOption::None);
73+
return cfg;
74+
}());
75+
76+
quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writter{filename_3, std::move(file_sink)};
77+
csv_writter.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
78+
csv_writter.append_row(132121123, "META", 300, 478.32321, "SELL");
79+
csv_writter.append_row(14212123, "AAPL", 120, 210.42321, "BUY");
80+
}
81+
82+
{
83+
// append mode
84+
{
85+
quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writter{filename_4, 'w'};
86+
csv_writter.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
87+
csv_writter.append_row(132121123, "META", 300, 478.32321, "SELL");
88+
csv_writter.append_row(14212123, "AAPL", 120, 210.42321, "BUY");
89+
}
90+
91+
{
92+
quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writter{filename_4, 'a'};
93+
csv_writter.append_row(13212123, "AAPL", 200, 210.32321, "BUY");
94+
csv_writter.append_row(132121123, "META", 400, 478.32321, "SELL");
95+
csv_writter.append_row(14212123, "AAPL", 220, 210.42321, "BUY");
96+
}
97+
}
98+
99+
{
100+
auto file_sink_5 = quill::Frontend::create_or_get_sink<quill::FileSink>(
101+
filename_5,
102+
[]()
103+
{
104+
quill::FileSinkConfig cfg;
105+
cfg.set_open_mode('w');
106+
cfg.set_filename_append_option(quill::FilenameAppendOption::None);
107+
return cfg;
108+
}());
109+
110+
auto file_sink_6 = quill::Frontend::create_or_get_sink<quill::FileSink>(
111+
filename_6,
112+
[]()
113+
{
114+
quill::FileSinkConfig cfg;
115+
cfg.set_open_mode('w');
116+
cfg.set_filename_append_option(quill::FilenameAppendOption::None);
117+
return cfg;
118+
}());
119+
120+
std::vector<std::shared_ptr<quill::Sink>> sinks;
121+
sinks.push_back(std::move(file_sink_5));
122+
sinks.push_back(std::move(file_sink_6));
123+
124+
quill::CsvWriter<OrderCsvSchema, quill::FrontendOptions> csv_writter{filename_5, std::move(sinks)};
125+
csv_writter.append_row(13212123, "AAPL", 100, 210.32321, "BUY");
126+
csv_writter.append_row(132121123, "META", 300, 478.32321, "SELL");
127+
csv_writter.append_row(14212123, "AAPL", 120, 210.42321, "BUY");
128+
}
129+
61130
// Wait until the backend thread stops for test stability
62131
Backend::stop();
63132

@@ -98,9 +167,60 @@ TEST_CASE("csv_writing")
98167
REQUIRE_EQ(file_contents_2[0], "order_id,symbol,quantity,price,side");
99168
}
100169

170+
{
171+
// Read file and check
172+
std::vector<std::string> const file_contents = quill::testing::file_contents(filename_3);
173+
REQUIRE_EQ(file_contents.size(), 4);
174+
175+
REQUIRE(quill::testing::file_contains(file_contents, "order_id,symbol,quantity,price,side"));
176+
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,100,210.32,BUY"));
177+
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,300,478.32,SELL"));
178+
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,120,210.42,BUY"));
179+
}
180+
181+
{
182+
// Read file and check
183+
std::vector<std::string> const file_contents = quill::testing::file_contents(filename_4);
184+
REQUIRE_EQ(file_contents.size(), 7);
185+
186+
REQUIRE(quill::testing::file_contains(file_contents, "order_id,symbol,quantity,price,side"));
187+
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,100,210.32,BUY"));
188+
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,300,478.32,SELL"));
189+
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,120,210.42,BUY"));
190+
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,200,210.32,BUY"));
191+
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,400,478.32,SELL"));
192+
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,220,210.42,BUY"));
193+
}
194+
195+
{
196+
// Read file and check
197+
std::vector<std::string> const file_contents = quill::testing::file_contents(filename_5);
198+
REQUIRE_EQ(file_contents.size(), 4);
199+
200+
REQUIRE(quill::testing::file_contains(file_contents, "order_id,symbol,quantity,price,side"));
201+
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,100,210.32,BUY"));
202+
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,300,478.32,SELL"));
203+
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,120,210.42,BUY"));
204+
}
205+
206+
{
207+
// Read file and check
208+
std::vector<std::string> const file_contents = quill::testing::file_contents(filename_6);
209+
REQUIRE_EQ(file_contents.size(), 4);
210+
211+
REQUIRE(quill::testing::file_contains(file_contents, "order_id,symbol,quantity,price,side"));
212+
REQUIRE(quill::testing::file_contains(file_contents, "13212123,AAPL,100,210.32,BUY"));
213+
REQUIRE(quill::testing::file_contains(file_contents, "132121123,META,300,478.32,SELL"));
214+
REQUIRE(quill::testing::file_contains(file_contents, "14212123,AAPL,120,210.42,BUY"));
215+
}
216+
101217
testing::remove_file(filename);
102218
testing::remove_file(filename_1);
103219
testing::remove_file(filename_2);
104220
testing::remove_file(filename_2_1);
105221
testing::remove_file(filename_2_2);
222+
testing::remove_file(filename_3);
223+
testing::remove_file(filename_4);
224+
testing::remove_file(filename_5);
225+
testing::remove_file(filename_6);
106226
}

test/unit_tests/InlinedVectorTest.cpp

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ TEST_CASE("stress_test")
103103
}
104104
}
105105

106-
TEST_CASE("assign_valid_index")
106+
TEST_CASE("assign_access_index")
107107
{
108108
// Create an InlinedVector with uint32_t and a small capacity
109109
InlinedVector<uint32_t, 4> vec;
@@ -121,13 +121,66 @@ TEST_CASE("assign_valid_index")
121121
REQUIRE_EQ(vec[1], 99);
122122
REQUIRE_EQ(vec[2], 30);
123123

124-
// Assign a new value to the last element (index 2)
124+
// Assign a new value to index 2
125125
vec.assign(2, 199);
126126

127127
// Verify that the value has been updated
128128
REQUIRE_EQ(vec[0], 10);
129129
REQUIRE_EQ(vec[1], 99);
130130
REQUIRE_EQ(vec[2], 199);
131+
132+
{
133+
bool throws{false};
134+
135+
try
136+
{
137+
// we only pushed_back 3 elements, trying to access the 4th will fail
138+
uint32_t const elem = vec[3];
139+
(void)elem;
140+
}
141+
catch (std::exception const&)
142+
{
143+
throws = true;
144+
}
145+
146+
REQUIRE(throws);
147+
}
148+
149+
// Add one more
150+
vec.push_back(130);
151+
152+
// Verify that the values
153+
REQUIRE_EQ(vec[0], 10);
154+
REQUIRE_EQ(vec[1], 99);
155+
REQUIRE_EQ(vec[2], 199);
156+
REQUIRE_EQ(vec[3], 130);
157+
158+
// Add one more - vector will switch to heap
159+
vec.push_back(230);
160+
161+
// Verify that the values
162+
REQUIRE_EQ(vec[0], 10);
163+
REQUIRE_EQ(vec[1], 99);
164+
REQUIRE_EQ(vec[2], 199);
165+
REQUIRE_EQ(vec[3], 130);
166+
REQUIRE_EQ(vec[4], 230);
167+
168+
{
169+
bool throws{false};
170+
171+
try
172+
{
173+
// we only pushed_back 5 elements, trying to access the 6th will fail
174+
uint32_t const elem = vec[5];
175+
(void)elem;
176+
}
177+
catch (std::exception const&)
178+
{
179+
throws = true;
180+
}
181+
182+
REQUIRE(throws);
183+
}
131184
}
132185

133186
TEST_SUITE_END();

0 commit comments

Comments
 (0)