Database System 0.1.0
Advanced C++20 Database System with Multi-Backend Support
Loading...
Searching...
No Matches
test_thread_adapter.cpp File Reference

Unit tests for thread_adapter (Phase 4) More...

#include "../../database/integrated/adapters/thread_adapter.h"
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
Include dependency graph for test_thread_adapter.cpp:

Go to the source code of this file.

Macros

#define TEST(name)
 
#define RUN_TEST(name)
 
#define ASSERT_TRUE(condition)
 
#define ASSERT_FALSE(condition)
 

Functions

void test_configuration_construction ()
 
void test_adapter_construction ()
 
void test_api_availability_submit ()
 
void test_api_availability_priority ()
 
void test_api_availability_stats ()
 
void test_multiple_instances ()
 
void test_move_semantics ()
 
void test_destructor_safety ()
 
void test_shutdown_without_init ()
 
void test_double_initialization ()
 
int main ()
 

Variables

int tests_passed = 0
 
int tests_failed = 0
 

Detailed Description

Unit tests for thread_adapter (Phase 4)

These are lightweight API tests that verify the adapter interface without requiring deep integration with thread_system.

For full integration testing, run the integration test suite instead.

Definition in file test_thread_adapter.cpp.

Macro Definition Documentation

◆ ASSERT_FALSE

#define ASSERT_FALSE ( condition)
Value:
ASSERT_TRUE(!(condition))
#define ASSERT_TRUE(condition)

Definition at line 47 of file test_thread_adapter.cpp.

◆ ASSERT_TRUE

#define ASSERT_TRUE ( condition)
Value:
if (!(condition)) { \
throw std::runtime_error("Assertion failed: " #condition); \
}

Definition at line 42 of file test_thread_adapter.cpp.

42#define ASSERT_TRUE(condition) \
43 if (!(condition)) { \
44 throw std::runtime_error("Assertion failed: " #condition); \
45 }

Referenced by test_api_availability_priority(), test_api_availability_submit(), and test_configuration_construction().

◆ RUN_TEST

#define RUN_TEST ( name)
Value:
do { \
std::cout << "Running test: " << #name << " ... "; \
try { \
test_##name(); \
std::cout << "PASSED\n"; \
} catch (const std::exception& e) { \
std::cout << "FAILED: " << e.what() << "\n"; \
} \
} while(0)
int tests_passed
int tests_failed

Definition at line 29 of file test_thread_adapter.cpp.

29#define RUN_TEST(name) \
30 do { \
31 std::cout << "Running test: " << #name << " ... "; \
32 try { \
33 test_##name(); \
34 std::cout << "PASSED\n"; \
35 tests_passed++; \
36 } catch (const std::exception& e) { \
37 std::cout << "FAILED: " << e.what() << "\n"; \
38 tests_failed++; \
39 } \
40 } while(0)

Referenced by main().

◆ TEST

#define TEST ( name)
Value:
void test_##name()

Definition at line 28 of file test_thread_adapter.cpp.

Function Documentation

◆ main()

int main ( )

Definition at line 242 of file test_thread_adapter.cpp.

242 {
243 std::cout << "=== Thread Adapter API Tests (Phase 4) ===\n";
244 std::cout << "Note: These tests verify API availability and basic functionality.\n";
245 std::cout << "Some tests may be skipped if thread_system is not available.\n\n";
246
247 RUN_TEST(configuration_construction);
248 RUN_TEST(adapter_construction);
249 RUN_TEST(api_availability_submit);
250 RUN_TEST(api_availability_priority);
251 RUN_TEST(api_availability_stats);
252 RUN_TEST(multiple_instances);
253 RUN_TEST(move_semantics);
254 RUN_TEST(destructor_safety);
255 RUN_TEST(shutdown_without_init);
256 RUN_TEST(double_initialization);
257
258 std::cout << "\n=== Test Summary ===\n";
259 std::cout << "Passed: " << tests_passed << "\n";
260 std::cout << "Failed: " << tests_failed << "\n";
261
262 if (tests_failed == 0) {
263 std::cout << "=== All tests passed! ✓ ===\n";
264 return 0;
265 } else {
266 std::cout << "=== Some tests failed! ✗ ===\n";
267 return 1;
268 }
269}
#define RUN_TEST(name)

References RUN_TEST, tests_failed, and tests_passed.

◆ test_adapter_construction()

void test_adapter_construction ( )

Definition at line 67 of file test_thread_adapter.cpp.

67 {
68 db_thread_config config;
69 config.thread_count = 2;
70
71 // Should be able to construct adapter
72 thread_adapter adapter(config);
73
74 // Construction should succeed
75}
Thread pool adapter for async database operations.
Thread pool configuration for async operations.
std::size_t thread_count
Number of worker threads (0 = auto-detect from hardware)

References database::integrated::db_thread_config::thread_count.

◆ test_api_availability_priority()

void test_api_availability_priority ( )

Definition at line 107 of file test_thread_adapter.cpp.

107 {
108 db_thread_config config;
109 config.thread_count = 1;
110 // Note: enable_priority_scheduling flag exists but priority is not implemented in backend pattern
111
112 thread_adapter adapter(config);
113
114 auto init_result = adapter.initialize();
115
116 if (init_result.is_ok()) {
117 std::atomic<bool> executed{false};
118
119 // Backend pattern removed priority support for simplification
120 // Use regular submit instead
121 auto future = adapter.submit([&executed]() {
122 executed = true;
123 });
124
125 future.get();
126 ASSERT_TRUE(executed);
127
128 adapter.shutdown();
129 }
130}

References ASSERT_TRUE, database::integrated::adapters::thread_adapter::initialize(), database::integrated::adapters::thread_adapter::shutdown(), database::integrated::adapters::thread_adapter::submit(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

◆ test_api_availability_stats()

void test_api_availability_stats ( )

Definition at line 133 of file test_thread_adapter.cpp.

133 {
134 db_thread_config config;
135 config.thread_count = 4;
136
137 thread_adapter adapter(config);
138
139 // Should be able to query stats even without initialization
140 auto worker_count = adapter.worker_count();
141 auto queue_size = adapter.queue_size();
142
143 // Values may be 0 if not initialized, but API should work
144 (void)worker_count;
145 (void)queue_size;
146}

References database::integrated::adapters::thread_adapter::queue_size(), database::integrated::db_thread_config::thread_count, and database::integrated::adapters::thread_adapter::worker_count().

Here is the call graph for this function:

◆ test_api_availability_submit()

void test_api_availability_submit ( )

Definition at line 78 of file test_thread_adapter.cpp.

78 {
79 db_thread_config config;
80 config.thread_count = 2;
81
82 thread_adapter adapter(config);
83
84 // Try to initialize (may fail without thread_system, that's ok)
85 auto init_result = adapter.initialize();
86
87 // If initialization succeeded, test submit
88 if (init_result.is_ok()) {
89 std::atomic<bool> executed{false};
90
91 auto future = adapter.submit([&executed]() {
92 executed = true;
93 return 42;
94 });
95
96 // Wait for completion
97 auto value = future.get();
98 ASSERT_TRUE(value == 42);
99 ASSERT_TRUE(executed);
100
101 adapter.shutdown();
102 }
103 // If init failed, that's acceptable for unit test
104}

References ASSERT_TRUE, database::integrated::adapters::thread_adapter::initialize(), database::integrated::adapters::thread_adapter::shutdown(), database::integrated::adapters::thread_adapter::submit(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

◆ test_configuration_construction()

void test_configuration_construction ( )

Definition at line 54 of file test_thread_adapter.cpp.

54 {
55 db_thread_config config;
56 config.thread_count = 4;
57 config.max_queue_size = 100;
58 config.pool_type = thread_pool_type::standard;
59 config.enable_priority_scheduling = false;
60
61 // Should be able to create config
62 ASSERT_TRUE(config.thread_count == 4);
63 ASSERT_TRUE(config.max_queue_size == 100);
64}
thread_pool_type pool_type
Thread pool implementation type.
bool enable_priority_scheduling
Enable priority-based task scheduling.
std::size_t max_queue_size
Maximum queued tasks (0 = unlimited)

References ASSERT_TRUE, database::integrated::db_thread_config::enable_priority_scheduling, database::integrated::db_thread_config::max_queue_size, database::integrated::db_thread_config::pool_type, and database::integrated::db_thread_config::thread_count.

◆ test_destructor_safety()

void test_destructor_safety ( )

Definition at line 181 of file test_thread_adapter.cpp.

181 {
182 // Test that adapter can be constructed and destroyed safely
183 {
184 db_thread_config config;
185 config.thread_count = 2;
186
187 thread_adapter adapter(config);
188
189 // Try to initialize
190 auto init_result = adapter.initialize();
191
192 if (init_result.is_ok()) {
193 // Submit a task
194 auto future = adapter.submit([]() { return 1; });
195 future.get();
196 }
197
198 // Destructor will be called here (should call shutdown internally)
199 }
200
201 // Should not crash
202}

References database::integrated::adapters::thread_adapter::initialize(), database::integrated::adapters::thread_adapter::submit(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

◆ test_double_initialization()

void test_double_initialization ( )

Definition at line 219 of file test_thread_adapter.cpp.

219 {
220 db_thread_config config;
221 config.thread_count = 2;
222
223 thread_adapter adapter(config);
224
225 auto init1 = adapter.initialize();
226
227 if (init1.is_ok()) {
228 // Second initialization may succeed (idempotent) or fail
229 // The important thing is it doesn't crash
230 auto init2 = adapter.initialize();
231 // Either behavior is acceptable
232 (void)init2;
233
234 adapter.shutdown();
235 }
236}

References database::integrated::adapters::thread_adapter::initialize(), database::integrated::adapters::thread_adapter::shutdown(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

◆ test_move_semantics()

void test_move_semantics ( )

Definition at line 164 of file test_thread_adapter.cpp.

164 {
165 db_thread_config config;
166 config.thread_count = 2;
167
168 thread_adapter adapter1(config);
169
170 // Should support move construction
171 thread_adapter adapter2(std::move(adapter1));
172
173 // Moved-to instance should be usable
174 auto init_result = adapter2.initialize();
175 if (init_result.is_ok()) {
176 adapter2.shutdown();
177 }
178}

References database::integrated::adapters::thread_adapter::initialize(), database::integrated::adapters::thread_adapter::shutdown(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

◆ test_multiple_instances()

void test_multiple_instances ( )

Definition at line 149 of file test_thread_adapter.cpp.

149 {
150 db_thread_config config1;
151 config1.thread_count = 2;
152
153 db_thread_config config2;
154 config2.thread_count = 3;
155
156 // Should be able to create multiple adapters
157 thread_adapter adapter1(config1);
158 thread_adapter adapter2(config2);
159
160 // Both should be constructible
161}

References database::integrated::db_thread_config::thread_count.

◆ test_shutdown_without_init()

void test_shutdown_without_init ( )

Definition at line 205 of file test_thread_adapter.cpp.

205 {
206 db_thread_config config;
207 config.thread_count = 2;
208
209 thread_adapter adapter(config);
210
211 // Should be able to shutdown without initialize
212 auto result = adapter.shutdown();
213
214 // Should succeed or handle gracefully
215 (void)result;
216}

References database::integrated::adapters::thread_adapter::shutdown(), and database::integrated::db_thread_config::thread_count.

Here is the call graph for this function:

Variable Documentation

◆ tests_failed

int tests_failed = 0

Definition at line 26 of file test_thread_adapter.cpp.

Referenced by main().

◆ tests_passed

int tests_passed = 0

Definition at line 25 of file test_thread_adapter.cpp.

Referenced by main().