1 | #include "catch.hpp" |
---|---|
2 | #include "duckdb/main/appender.hpp" |
3 | #include "test_helpers.hpp" |
4 | |
5 | #include <atomic> |
6 | #include <thread> |
7 | #include <vector> |
8 | |
9 | using namespace duckdb; |
10 | using namespace std; |
11 | |
12 | atomic<int> finished_threads; |
13 | |
14 | #define THREAD_COUNT 20 |
15 | #define INSERT_ELEMENTS 2000 |
16 | |
17 | static void append_to_integers(DuckDB *db, size_t threadnr) { |
18 | REQUIRE(db); |
19 | Connection con(*db); |
20 | |
21 | Appender appender(con, "integers"); |
22 | for (size_t i = 0; i < INSERT_ELEMENTS; i++) { |
23 | appender.BeginRow(); |
24 | appender.Append<int32_t>(1); |
25 | appender.EndRow(); |
26 | } |
27 | finished_threads++; |
28 | while (finished_threads != THREAD_COUNT) |
29 | ; |
30 | appender.Close(); |
31 | } |
32 | |
33 | TEST_CASE("Test concurrent appends", "[appender][.]") { |
34 | unique_ptr<QueryResult> result; |
35 | DuckDB db(nullptr); |
36 | Connection con(db); |
37 | |
38 | // create a single table to append to |
39 | REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)")); |
40 | |
41 | finished_threads = 0; |
42 | |
43 | thread threads[THREAD_COUNT]; |
44 | for (size_t i = 0; i < THREAD_COUNT; i++) { |
45 | threads[i] = thread(append_to_integers, &db, i); |
46 | } |
47 | |
48 | for (size_t i = 0; i < THREAD_COUNT; i++) { |
49 | threads[i].join(); |
50 | } |
51 | // check how many entries we have |
52 | result = con.Query("SELECT COUNT(*) FROM integers"); |
53 | REQUIRE(CHECK_COLUMN(result, 0, {THREAD_COUNT * INSERT_ELEMENTS})); |
54 | } |
55 |