File: test_concurrent_append.cpp

package info (click to toggle)
duckdb 1.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 299,196 kB
  • sloc: cpp: 865,414; ansic: 57,292; python: 18,871; sql: 12,663; lisp: 11,751; yacc: 7,412; lex: 1,682; sh: 747; makefile: 558
file content (55 lines) | stat: -rw-r--r-- 1,304 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "catch.hpp"
#include "duckdb/main/appender.hpp"
#include "test_helpers.hpp"

#include <atomic>
#include <thread>
#include <vector>

using namespace duckdb;
using namespace std;

atomic<int> finished_threads;

#define THREAD_COUNT    8
#define INSERT_ELEMENTS 2000

static void append_to_integers(DuckDB *db, size_t threadnr) {
	Connection con(*db);

	Appender appender(con, "integers");
	for (size_t i = 0; i < INSERT_ELEMENTS; i++) {
		appender.BeginRow();
		appender.Append<int32_t>(1);
		appender.EndRow();
	}
	finished_threads++;
	while (finished_threads != THREAD_COUNT)
		;
	appender.Close();
}

TEST_CASE("Test concurrent appends", "[appender][.]") {
	duckdb::unique_ptr<QueryResult> result;
	DBConfig config;
	config.options.maximum_threads = 1;
	DuckDB db(nullptr, &config);
	Connection con(db);

	// create a single table to append to
	REQUIRE_NO_FAIL(con.Query("CREATE TABLE integers(i INTEGER)"));

	finished_threads = 0;

	thread threads[THREAD_COUNT];
	for (size_t i = 0; i < THREAD_COUNT; i++) {
		threads[i] = thread(append_to_integers, &db, i);
	}

	for (size_t i = 0; i < THREAD_COUNT; i++) {
		threads[i].join();
	}
	// check how many entries we have
	result = con.Query("SELECT COUNT(*) FROM integers");
	REQUIRE(CHECK_COLUMN(result, 0, {THREAD_COUNT * INSERT_ELEMENTS}));
}