Skip to content

Commit 12eda19

Browse files
authoredNov 2, 2019
Add async logger set-up (#47)
* Allow sync and async loggers to be created respectively * Fix and use string for map key * Change to TYPE instead of SYNC for stating sync/async logger * Reuse function instead of using special enum find * Add parsing of global and named thread pools * Add mapping of thread pool to async logger * Implement getting async overflow policy * Split up impl of parsing logger * Add unit tests for basic async loggers * Rename unit_test.cpp to others.cpp * Add logger with sink test cases * Add global thread pool testing * Change all versions to 0.3.0-pre * Add test cases for localized thread pool * Add test case for multiple thread pools * Add to CHANGELOG * Fix GCC 4.9 old syntax issue * Add async into proper testing and put into README
1 parent 0417a98 commit 12eda19

16 files changed

+946
-93
lines changed
 

‎CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
- Add support for async logger. This fixes
6+
([#44](https://github.com/guangie88/spdlog_setup/issues/44))
7+
- This makes logger accepts `type`, which takes in `sync` or `async`.
8+
`sync` is default. Refer to the README for more details.
59
- Change support to `spdlog` `v1.y.z` tag release, currently tested all `v1.0.0`
610
to `v1.3.1` to be working. This fixes
711
([#26](https://github.com/guangie88/spdlog_setup/issues/26)).

‎CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ install(
9999
endif()
100100

101101
# spdlog_setup_unit_test
102+
FILE(GLOB unit_test_cpps src/unit_test/*.cpp)
102103
if(SPDLOG_SETUP_INCLUDE_UNIT_TESTS)
103104
add_executable(spdlog_setup_unit_test
104-
src/unit_test/unit_test.cpp)
105+
${unit_test_cpps})
105106

106107
set_property(TARGET spdlog_setup_unit_test PROPERTY CXX_STANDARD 11)
107108

‎README.md

+25
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ level = "trace"
298298
name = "console"
299299
sinks = ["console_st", "console_mt"]
300300
pattern = "succient"
301+
302+
# Async
303+
304+
[global_thread_pool]
305+
queue_size = 8192
306+
num_threads = 1
307+
308+
[[thread_pool]]
309+
name = "tp"
310+
queue_size = 4096
311+
num_threads = 2
312+
313+
[[logger]]
314+
type = "async"
315+
name = "global_async"
316+
sinks = ["console_mt"]
317+
pattern = "succient"
318+
319+
[[logger]]
320+
type = "async"
321+
name = "local_async"
322+
sinks = ["console_mt"]
323+
pattern = "succient"
324+
thread_pool = "tp"
325+
overflow_policy = "overrun_oldest" # block (default) | overrun_oldest
301326
```
302327

303328
### Tagged-Base Pre-TOML File Configuration

‎config/log_conf_linux.toml

+25
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,28 @@ level = "trace"
125125
name = "console"
126126
sinks = ["console_st", "console_mt"]
127127
pattern = "succient"
128+
129+
# Async
130+
131+
[global_thread_pool]
132+
queue_size = 8192
133+
num_threads = 1
134+
135+
[[thread_pool]]
136+
name = "tp"
137+
queue_size = 4096
138+
num_threads = 2
139+
140+
[[logger]]
141+
type = "async"
142+
name = "global_async"
143+
sinks = ["console_mt"]
144+
pattern = "succient"
145+
146+
[[logger]]
147+
type = "async"
148+
name = "local_async"
149+
sinks = ["console_mt"]
150+
pattern = "succient"
151+
thread_pool = "tp"
152+
overflow_policy = "overrun_oldest" # block (default) | overrun_oldest

‎config/log_conf_win.toml

+39
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@
1313
# check out https://github.com/gabime/spdlog/wiki/3.-Custom-formatting
1414
global_pattern = "[%Y-%m-%dT%T%z] [%L] <%n>: %v"
1515

16+
[global_thread_pool]
17+
queue_size = 8192
18+
num_threads = 1
19+
20+
[[thread_pool]]
21+
name = "tp_1"
22+
queue_size = 4096
23+
num_threads = 2
24+
25+
[[thread_pool]]
26+
name = "tp_2"
27+
queue_size = 2048
28+
num_threads = 4
29+
1630
[[sink]]
1731
name = "console_st"
1832
type = "stdout_sink_st"
@@ -106,3 +120,28 @@ level = "trace"
106120
name = "console"
107121
sinks = ["console_st", "console_mt"]
108122
pattern = "succient"
123+
124+
# Async
125+
126+
[global_thread_pool]
127+
queue_size = 8192
128+
num_threads = 1
129+
130+
[[thread_pool]]
131+
name = "tp"
132+
queue_size = 4096
133+
num_threads = 2
134+
135+
[[logger]]
136+
type = "async"
137+
name = "global_async"
138+
sinks = ["console_mt"]
139+
pattern = "succient"
140+
141+
[[logger]]
142+
type = "async"
143+
name = "local_async"
144+
sinks = ["console_mt"]
145+
pattern = "succient"
146+
thread_pool = "tp"
147+
overflow_policy = "overrun_oldest" # block (default) | overrun_oldest

‎include/spdlog_setup/conf.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void from_file_with_tag_replacement(
110110
cpptoml::parser parser(toml_ss);
111111
const auto config = parser.parse();
112112

113-
details::setup_impl(config);
113+
details::setup(config);
114114
} catch (const setup_error &) {
115115
throw;
116116
} catch (const exception &e) {
@@ -158,7 +158,7 @@ auto from_file_and_override_with_tag_replacement(
158158
details::merge_config_root(merged_config, override_config);
159159
}
160160

161-
details::setup_impl(merged_config);
161+
details::setup(merged_config);
162162
return has_override;
163163
} catch (const setup_error &) {
164164
throw;
@@ -174,7 +174,7 @@ inline void from_file(const std::string &toml_path) {
174174

175175
try {
176176
const auto config = cpptoml::parse_file(toml_path);
177-
details::setup_impl(config);
177+
details::setup(config);
178178
} catch (const exception &e) {
179179
throw setup_error(e.what());
180180
}
@@ -205,7 +205,7 @@ inline auto from_file_and_override(
205205
details::merge_config_root(merged_config, override_config);
206206
}
207207

208-
details::setup_impl(merged_config);
208+
details::setup(merged_config);
209209
return has_override;
210210
} catch (const exception &e) {
211211
throw setup_error(e.what());

‎include/spdlog_setup/details/conf_impl.h

+311-84
Large diffs are not rendered by default.

‎src/unit_test/conf.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#pragma once
8+
9+
#if defined(unix) || defined(__unix__) || defined(__unix)
10+
#ifndef SPDLOG_ENABLE_SYSLOG
11+
#define SPDLOG_ENABLE_SYSLOG
12+
#endif
13+
#endif
14+
15+
#include "spdlog_setup/conf.h"

‎src/unit_test/examples.h

+54
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
17
#pragma once
28

9+
#include "conf.h"
10+
311
#include <cstddef>
412
#include <cstdio>
513
#include <fstream>
@@ -101,6 +109,29 @@ static constexpr auto FULL_CONF = R"x(
101109
name = "console"
102110
sinks = ["console_st", "console_mt"]
103111
pattern = "succient"
112+
113+
[global_thread_pool]
114+
queue_size = 8192
115+
num_threads = 1
116+
117+
[[thread_pool]]
118+
name = "tp"
119+
queue_size = 4096
120+
num_threads = 2
121+
122+
[[logger]]
123+
type = "async"
124+
name = "global_async"
125+
sinks = ["console_mt"]
126+
pattern = "succient"
127+
128+
[[logger]]
129+
type = "async"
130+
name = "local_async"
131+
sinks = ["console_mt"]
132+
pattern = "succient"
133+
thread_pool = "tp"
134+
overflow_policy = "overrun_oldest" # block (default) | overrun_oldest
104135
)x";
105136
#else
106137
static constexpr auto FULL_CONF = R"x(
@@ -204,6 +235,29 @@ static constexpr auto FULL_CONF = R"x(
204235
name = "console"
205236
sinks = ["console_st", "console_mt"]
206237
pattern = "succient"
238+
239+
[global_thread_pool]
240+
queue_size = 8192
241+
num_threads = 1
242+
243+
[[thread_pool]]
244+
name = "tp"
245+
queue_size = 4096
246+
num_threads = 2
247+
248+
[[logger]]
249+
type = "async"
250+
name = "global_async"
251+
sinks = ["console_mt"]
252+
pattern = "succient"
253+
254+
[[logger]]
255+
type = "async"
256+
name = "local_async"
257+
sinks = ["console_mt"]
258+
pattern = "succient"
259+
thread_pool = "tp"
260+
overflow_policy = "overrun_oldest" # block (default) | overrun_oldest
207261
)x";
208262
#endif
209263

‎src/unit_test/loggers.cpp

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#include "catch.hpp"
8+
9+
#include "loggers.h"
10+
#include "sinks.h"
11+
12+
#include <memory>
13+
#include <string>
14+
#include <typeinfo>
15+
#include <unordered_map>
16+
17+
const std::unordered_map<std::string, std::shared_ptr<spdlog::sinks::sink>>
18+
EMPTY_SINKS_MAP;
19+
const std::
20+
unordered_map<std::string, std::shared_ptr<spdlog::details::thread_pool>>
21+
EMPTY_THREAD_POOLS_MAP;
22+
23+
TEST_CASE("Parse default logger", "[parse_simple_default_logger]") {
24+
const auto logger = spdlog_setup::details::setup_logger(
25+
generate_simple_default_logger_table(),
26+
EMPTY_SINKS_MAP,
27+
EMPTY_THREAD_POOLS_MAP);
28+
29+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
30+
REQUIRE(typeid(*logger) == typeid(const spdlog::logger &));
31+
}
32+
33+
TEST_CASE("Parse sync logger", "[parse_simple_sync_logger]") {
34+
const auto logger = spdlog_setup::details::setup_logger(
35+
generate_simple_sync_logger_table(),
36+
EMPTY_SINKS_MAP,
37+
EMPTY_THREAD_POOLS_MAP);
38+
39+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
40+
REQUIRE(typeid(*logger) == typeid(const spdlog::logger &));
41+
}
42+
43+
TEST_CASE("Parse async logger", "[parse_simple_async_logger]") {
44+
const auto logger = spdlog_setup::details::setup_logger(
45+
generate_simple_async_logger_table(),
46+
EMPTY_SINKS_MAP,
47+
EMPTY_THREAD_POOLS_MAP);
48+
49+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
50+
REQUIRE(typeid(*logger) == typeid(const spdlog::async_logger &));
51+
}
52+
53+
TEST_CASE("Parse invalid sync logger", "[parse_invalid_sync_logger]") {
54+
REQUIRE_THROWS_AS(
55+
spdlog_setup::details::setup_logger(
56+
generate_invalid_sync_logger_table(),
57+
EMPTY_SINKS_MAP,
58+
EMPTY_THREAD_POOLS_MAP),
59+
spdlog_setup::setup_error);
60+
}
61+
62+
TEST_CASE(
63+
"Parse async overflow policy block logger",
64+
"[parse_simple_async_overflow_policy_block_logger]") {
65+
const auto logger = spdlog_setup::details::setup_logger(
66+
generate_async_overflow_policy_block_logger_table(),
67+
EMPTY_SINKS_MAP,
68+
EMPTY_THREAD_POOLS_MAP);
69+
70+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
71+
REQUIRE(typeid(*logger) == typeid(const spdlog::async_logger &));
72+
}
73+
74+
TEST_CASE(
75+
"Parse async overflow policy overrun oldest logger",
76+
"[parse_simple_async_overflow_policy_overrun_oldest_logger]") {
77+
const auto logger = spdlog_setup::details::setup_logger(
78+
generate_async_overflow_policy_overrun_oldest_logger_table(),
79+
EMPTY_SINKS_MAP,
80+
EMPTY_THREAD_POOLS_MAP);
81+
82+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
83+
REQUIRE(typeid(*logger) == typeid(const spdlog::async_logger &));
84+
}
85+
86+
TEST_CASE(
87+
"Parse invalid async overflow policy logger",
88+
"[parse_invalid_async_overflow_policy_logger]") {
89+
REQUIRE_THROWS_AS(
90+
spdlog_setup::details::setup_logger(
91+
generate_invalid_async_overflow_policy_logger_table(),
92+
EMPTY_SINKS_MAP,
93+
EMPTY_THREAD_POOLS_MAP),
94+
spdlog_setup::setup_error);
95+
}
96+
97+
TEST_CASE("Parse logger with sink", "[parse_logger_with_sink]") {
98+
auto sink = spdlog_setup::details::setup_sink(generate_stdout_sink_st());
99+
const auto sinks_map =
100+
std::unordered_map<std::string, std::shared_ptr<spdlog::sinks::sink>>{
101+
{TEST_SINK_NAME, std::move(sink)}};
102+
103+
const auto logger = spdlog_setup::details::setup_logger(
104+
generate_simple_logger_with_sink_table(),
105+
sinks_map,
106+
EMPTY_THREAD_POOLS_MAP);
107+
108+
REQUIRE(logger->name() == TEST_LOGGER_NAME);
109+
REQUIRE(logger->sinks()[0] == sinks_map.at(TEST_SINK_NAME));
110+
}
111+
112+
TEST_CASE(
113+
"Parse logger with missing sink", "[parse_logger_with_missing_sink]") {
114+
auto sink = spdlog_setup::details::setup_sink(generate_stdout_sink_st());
115+
const auto sinks_map =
116+
std::unordered_map<std::string, std::shared_ptr<spdlog::sinks::sink>>{
117+
{"xxx", std::move(sink)}};
118+
119+
REQUIRE_THROWS_AS(
120+
spdlog_setup::details::setup_logger(
121+
generate_simple_logger_with_sink_table(),
122+
sinks_map,
123+
EMPTY_THREAD_POOLS_MAP),
124+
spdlog_setup::setup_error);
125+
}

‎src/unit_test/loggers.h

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#pragma once
8+
9+
#include "conf.h"
10+
11+
#include <memory>
12+
#include <string>
13+
#include <utility>
14+
15+
static constexpr auto TEST_LOGGER_NAME = "default";
16+
static constexpr auto TEST_SINK_NAME = "default";
17+
18+
inline auto generate_simple_default_logger_table()
19+
-> std::shared_ptr<cpptoml::table> {
20+
namespace names = spdlog_setup::details::names;
21+
22+
auto logger_table = cpptoml::make_table();
23+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
24+
logger_table->insert(names::SINKS, cpptoml::make_array());
25+
return std::move(logger_table);
26+
}
27+
28+
inline auto generate_simple_sync_logger_table()
29+
-> std::shared_ptr<cpptoml::table> {
30+
namespace names = spdlog_setup::details::names;
31+
32+
auto logger_table = cpptoml::make_table();
33+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
34+
logger_table->insert(names::TYPE, names::SYNC);
35+
logger_table->insert(names::SINKS, cpptoml::make_array());
36+
return std::move(logger_table);
37+
}
38+
39+
inline auto generate_simple_async_logger_table()
40+
-> std::shared_ptr<cpptoml::table> {
41+
namespace names = spdlog_setup::details::names;
42+
43+
auto logger_table = cpptoml::make_table();
44+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
45+
logger_table->insert(names::TYPE, names::ASYNC);
46+
logger_table->insert(names::SINKS, cpptoml::make_array());
47+
return std::move(logger_table);
48+
}
49+
50+
inline auto generate_global_async_logger_table()
51+
-> std::shared_ptr<cpptoml::table> {
52+
namespace names = spdlog_setup::details::names;
53+
54+
auto logger_table = cpptoml::make_table();
55+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
56+
logger_table->insert(names::TYPE, names::ASYNC);
57+
logger_table->insert(names::SINKS, cpptoml::make_array());
58+
return std::move(logger_table);
59+
}
60+
61+
inline auto generate_invalid_sync_logger_table()
62+
-> std::shared_ptr<cpptoml::table> {
63+
namespace names = spdlog_setup::details::names;
64+
65+
auto logger_table = cpptoml::make_table();
66+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
67+
logger_table->insert(names::TYPE, "xxx");
68+
logger_table->insert(names::SINKS, cpptoml::make_array());
69+
return std::move(logger_table);
70+
}
71+
72+
inline auto generate_async_overflow_policy_block_logger_table()
73+
-> std::shared_ptr<cpptoml::table> {
74+
namespace names = spdlog_setup::details::names;
75+
76+
auto logger_table = cpptoml::make_table();
77+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
78+
logger_table->insert(names::TYPE, names::ASYNC);
79+
logger_table->insert(names::OVERFLOW_POLICY, names::BLOCK);
80+
logger_table->insert(names::SINKS, cpptoml::make_array());
81+
return std::move(logger_table);
82+
}
83+
84+
inline auto generate_async_overflow_policy_overrun_oldest_logger_table()
85+
-> std::shared_ptr<cpptoml::table> {
86+
namespace names = spdlog_setup::details::names;
87+
88+
auto logger_table = cpptoml::make_table();
89+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
90+
logger_table->insert(names::TYPE, names::ASYNC);
91+
logger_table->insert(names::OVERFLOW_POLICY, names::OVERRUN_OLDEST);
92+
logger_table->insert(names::SINKS, cpptoml::make_array());
93+
return std::move(logger_table);
94+
}
95+
96+
inline auto generate_invalid_async_overflow_policy_logger_table()
97+
-> std::shared_ptr<cpptoml::table> {
98+
namespace names = spdlog_setup::details::names;
99+
100+
auto logger_table = cpptoml::make_table();
101+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
102+
logger_table->insert(names::TYPE, names::ASYNC);
103+
logger_table->insert(names::OVERFLOW_POLICY, "xxx");
104+
logger_table->insert(names::SINKS, cpptoml::make_array());
105+
return std::move(logger_table);
106+
}
107+
108+
inline auto generate_simple_logger_with_sink_table()
109+
-> std::shared_ptr<cpptoml::table> {
110+
namespace names = spdlog_setup::details::names;
111+
112+
auto logger_table = cpptoml::make_table();
113+
logger_table->insert(names::NAME, std::string(TEST_LOGGER_NAME));
114+
115+
auto sinks = cpptoml::make_array();
116+
sinks->push_back(std::string(TEST_SINK_NAME));
117+
logger_table->insert(names::SINKS, std::move(sinks));
118+
119+
return std::move(logger_table);
120+
}

‎src/unit_test/unit_test.cpp ‎src/unit_test/others.cpp

-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,6 @@
1616
#include "examples.h"
1717

1818
#include "spdlog_setup/conf.h"
19-
#include "spdlog_setup/details/third_party/cpptoml.h"
20-
#if defined(SPDLOG_SETUP_CPPTOML_EXTERNAL)
21-
#include "cpptoml.h"
22-
#endif
2319

2420
#include <fstream>
2521
#include <iostream>

‎src/unit_test/sinks.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#include "catch.hpp"
8+
9+
#include "sinks.h"
10+
11+
#include <typeinfo>
12+
13+
TEST_CASE("Parse stdout sink st", "[parse_generate_stdout_sink_st]") {
14+
const auto sink =
15+
spdlog_setup::details::setup_sink(generate_stdout_sink_st());
16+
REQUIRE(typeid(*sink) == typeid(const spdlog::sinks::stdout_sink_st &));
17+
}

‎src/unit_test/sinks.h

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#pragma once
8+
9+
#include "conf.h"
10+
11+
#include <memory>
12+
#include <string>
13+
#include <unordered_map>
14+
#include <utility>
15+
16+
inline auto generate_stdout_sink_st() -> std::shared_ptr<cpptoml::table> {
17+
namespace names = spdlog_setup::details::names;
18+
19+
auto sink_table = cpptoml::make_table();
20+
sink_table->insert(names::TYPE, std::string("stdout_sink_st"));
21+
return std::move(sink_table);
22+
}

‎src/unit_test/thread_pool.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#include "catch.hpp"
8+
9+
#include "thread_pool.h"
10+
11+
TEST_CASE("Parse global thread pool", "[parse_global_thread_pool]") {
12+
// Cannot test queue size and thread count as they are not exposed publicly
13+
// Can only test that the thread pool was changed to another instance
14+
const auto original = spdlog::thread_pool();
15+
16+
const auto thread_pools = spdlog_setup::details::setup_thread_pools(
17+
generate_global_thread_pool());
18+
const auto modified = spdlog::thread_pool();
19+
20+
REQUIRE(original != modified);
21+
REQUIRE(thread_pools.empty());
22+
}
23+
24+
TEST_CASE("Parse simple thread pool", "[parse_simple_thread_pool]") {
25+
// No way to test non-global thread pool from spdlog sadly
26+
const auto thread_pools = spdlog_setup::details::setup_thread_pools(
27+
generate_simple_thread_pool());
28+
29+
REQUIRE(thread_pools.size() == 1);
30+
}
31+
32+
TEST_CASE("Parse multiple thread pools", "[parse_multiple_thread_pools]") {
33+
const auto thread_pools = spdlog_setup::details::setup_thread_pools(
34+
generate_multiple_thread_pools(10));
35+
36+
REQUIRE(thread_pools.size() == 10);
37+
}
38+
39+
TEST_CASE(
40+
"Parse invalid no name thread pool",
41+
"[parse_invalid_no_name_thread_pool]") {
42+
REQUIRE_THROWS_AS(
43+
spdlog_setup::details::setup_thread_pools(
44+
generate_invalid_no_name_thread_pool()),
45+
spdlog_setup::setup_error);
46+
}
47+
48+
TEST_CASE(
49+
"Parse invalid no queue size thread pool",
50+
"[parse_invalid_no_queue_size_thread_pool]") {
51+
REQUIRE_THROWS_AS(
52+
spdlog_setup::details::setup_thread_pools(
53+
generate_invalid_no_queue_size_thread_pool()),
54+
spdlog_setup::setup_error);
55+
}
56+
57+
TEST_CASE(
58+
"Parse invalid no num threads thread pool",
59+
"[parse_invalid_no_num_threads_thread_pool]") {
60+
REQUIRE_THROWS_AS(
61+
spdlog_setup::details::setup_thread_pools(
62+
generate_invalid_no_num_threads_thread_pool()),
63+
spdlog_setup::setup_error);
64+
}

‎src/unit_test/thread_pool.h

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* Unit tests implementation.
3+
* @author Chen Weiguang
4+
* @version 0.3.0-pre
5+
*/
6+
7+
#pragma once
8+
9+
#include "conf.h"
10+
11+
#include <memory>
12+
#include <string>
13+
#include <utility>
14+
15+
static constexpr auto TEST_THREAD_POOL_NAME = "default";
16+
17+
inline auto generate_global_thread_pool() -> std::shared_ptr<cpptoml::table> {
18+
namespace names = spdlog_setup::details::names;
19+
20+
auto global_thread_inner_pool_table = cpptoml::make_table();
21+
global_thread_inner_pool_table->insert(names::QUEUE_SIZE, size_t(1234));
22+
global_thread_inner_pool_table->insert(names::NUM_THREADS, size_t(9));
23+
24+
auto conf = cpptoml::make_table();
25+
conf->insert(
26+
names::GLOBAL_THREAD_POOL_TABLE,
27+
std::move(global_thread_inner_pool_table));
28+
29+
return std::move(conf);
30+
}
31+
32+
inline auto generate_simple_thread_pool() -> std::shared_ptr<cpptoml::table> {
33+
namespace names = spdlog_setup::details::names;
34+
35+
auto thread_pool_table_array = cpptoml::make_table_array();
36+
37+
auto thread_pool_table = cpptoml::make_table();
38+
thread_pool_table->insert(names::NAME, TEST_THREAD_POOL_NAME);
39+
thread_pool_table->insert(names::QUEUE_SIZE, size_t(1234));
40+
thread_pool_table->insert(names::NUM_THREADS, size_t(9));
41+
thread_pool_table_array->push_back(std::move(thread_pool_table));
42+
43+
auto conf = cpptoml::make_table();
44+
conf->insert(names::THREAD_POOL_TABLE, std::move(thread_pool_table_array));
45+
46+
return std::move(conf);
47+
}
48+
49+
inline auto generate_multiple_thread_pools(const size_t count)
50+
-> std::shared_ptr<cpptoml::table> {
51+
namespace names = spdlog_setup::details::names;
52+
53+
auto thread_pool_table_array = cpptoml::make_table_array();
54+
55+
for (size_t i = 0; i < count; ++i) {
56+
auto thread_pool_table = cpptoml::make_table();
57+
thread_pool_table->insert(
58+
names::NAME, TEST_THREAD_POOL_NAME + std::to_string(i));
59+
thread_pool_table->insert(names::QUEUE_SIZE, size_t(1234));
60+
thread_pool_table->insert(names::NUM_THREADS, size_t(9));
61+
thread_pool_table_array->push_back(std::move(thread_pool_table));
62+
}
63+
64+
auto conf = cpptoml::make_table();
65+
conf->insert(names::THREAD_POOL_TABLE, std::move(thread_pool_table_array));
66+
67+
return std::move(conf);
68+
}
69+
70+
inline auto generate_invalid_no_name_thread_pool()
71+
-> std::shared_ptr<cpptoml::table> {
72+
namespace names = spdlog_setup::details::names;
73+
74+
auto thread_pool_table_array = cpptoml::make_table_array();
75+
76+
auto thread_pool_table = cpptoml::make_table();
77+
thread_pool_table->insert(names::QUEUE_SIZE, size_t(1234));
78+
thread_pool_table->insert(names::NUM_THREADS, size_t(9));
79+
thread_pool_table_array->push_back(std::move(thread_pool_table));
80+
81+
auto conf = cpptoml::make_table();
82+
conf->insert(names::THREAD_POOL_TABLE, std::move(thread_pool_table_array));
83+
84+
return std::move(conf);
85+
}
86+
87+
inline auto generate_invalid_no_queue_size_thread_pool()
88+
-> std::shared_ptr<cpptoml::table> {
89+
namespace names = spdlog_setup::details::names;
90+
91+
auto thread_pool_table_array = cpptoml::make_table_array();
92+
93+
auto thread_pool_table = cpptoml::make_table();
94+
thread_pool_table->insert(names::NAME, TEST_THREAD_POOL_NAME);
95+
thread_pool_table->insert(names::NUM_THREADS, size_t(9));
96+
thread_pool_table_array->push_back(std::move(thread_pool_table));
97+
98+
auto conf = cpptoml::make_table();
99+
conf->insert(names::THREAD_POOL_TABLE, std::move(thread_pool_table_array));
100+
101+
return std::move(conf);
102+
}
103+
104+
inline auto generate_invalid_no_num_threads_thread_pool()
105+
-> std::shared_ptr<cpptoml::table> {
106+
namespace names = spdlog_setup::details::names;
107+
108+
auto thread_pool_table_array = cpptoml::make_table_array();
109+
110+
auto thread_pool_table = cpptoml::make_table();
111+
thread_pool_table->insert(names::NAME, TEST_THREAD_POOL_NAME);
112+
thread_pool_table->insert(names::QUEUE_SIZE, size_t(1234));
113+
thread_pool_table_array->push_back(std::move(thread_pool_table));
114+
115+
auto conf = cpptoml::make_table();
116+
conf->insert(names::THREAD_POOL_TABLE, std::move(thread_pool_table_array));
117+
118+
return std::move(conf);
119+
}

0 commit comments

Comments
 (0)
Please sign in to comment.