| 1 |
#include <pthread.h> |
| 2 |
#include <time.h> |
| 3 |
|
| 4 |
#include "../include/cowsql.h" |
| 5 |
|
| 6 |
#include "./lib/runner.h" |
| 7 |
|
| 8 |
TEST_MODULE(integration); |
| 9 |
|
| 10 |
#if 0 |
| 11 |
|
| 12 |
/****************************************************************************** |
| 13 |
* |
| 14 |
* Helpers |
| 15 |
* |
| 16 |
******************************************************************************/ |
| 17 |
|
| 18 |
/* A worker that keeps inserting rows into a test table and fetching them back, |
| 19 |
* checking that they have been all inserted. */ |
| 20 |
struct worker |
| 21 |
{ |
| 22 |
struct test_client *client; /* A connected client */ |
| 23 |
int i; /* Worker index */ |
| 24 |
int a; /* Start inserting from this number */ |
| 25 |
int n; /* Number of insertions to perform */ |
| 26 |
pthread_t thread; /* System thread we run in */ |
| 27 |
}; |
| 28 |
|
| 29 |
static void *__worker_run(void *arg) |
| 30 |
{ |
| 31 |
struct worker *w; |
| 32 |
char *leader; |
| 33 |
uint64_t heartbeat; |
| 34 |
uint32_t db_id; |
| 35 |
int b; |
| 36 |
int i; |
| 37 |
|
| 38 |
munit_assert_ptr_not_null(arg); |
| 39 |
|
| 40 |
w = (struct worker *)arg; |
| 41 |
|
| 42 |
/* Initialize the connection and open a database. */ |
| 43 |
test_client_handshake(w->client); |
| 44 |
test_client_leader(w->client, &leader); |
| 45 |
test_client_client(w->client, &heartbeat); |
| 46 |
test_client_open(w->client, "test.db", &db_id); |
| 47 |
|
| 48 |
b = w->a + w->n; |
| 49 |
|
| 50 |
for (i = w->a; i < b; i++) { |
| 51 |
uint32_t stmt_id; |
| 52 |
char sql[128]; |
| 53 |
struct test_client_result result; |
| 54 |
struct test_client_rows rows; |
| 55 |
struct test_client_row *row; |
| 56 |
int j; |
| 57 |
|
| 58 |
/* Insert a row in the test table. */ |
| 59 |
sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i); |
| 60 |
|
| 61 |
test_client_prepare(w->client, db_id, sql, &stmt_id); |
| 62 |
test_client_exec(w->client, db_id, stmt_id, &result); |
| 63 |
|
| 64 |
munit_assert_int(result.rows_affected, ==, 1); |
| 65 |
|
| 66 |
test_client_finalize(w->client, db_id, stmt_id); |
| 67 |
|
| 68 |
/* Fetch all rows within our own working range. */ |
| 69 |
sprintf(sql, "SELECT n FROM test WHERE n >= %d AND n < %d", |
| 70 |
w->a, b); |
| 71 |
|
| 72 |
test_client_prepare(w->client, db_id, sql, &stmt_id); |
| 73 |
test_client_query(w->client, db_id, stmt_id, &rows); |
| 74 |
|
| 75 |
munit_assert_int(rows.column_count, ==, 1); |
| 76 |
munit_assert_string_equal(rows.column_names[0], "n"); |
| 77 |
|
| 78 |
row = rows.next; |
| 79 |
for (j = w->a; j <= i; j++) { |
| 80 |
munit_assert_ptr_not_null(row); |
| 81 |
|
| 82 |
munit_assert_int(row->types[0], ==, SQLITE_INTEGER); |
| 83 |
munit_assert_int(*(int64_t *)row->values[0], ==, j); |
| 84 |
|
| 85 |
row = row->next; |
| 86 |
} |
| 87 |
|
| 88 |
test_client_rows_close(&rows); |
| 89 |
test_client_finalize(w->client, db_id, stmt_id); |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
static void __worker_start(struct worker *w, |
| 96 |
struct test_server *server, |
| 97 |
int i, |
| 98 |
int a, |
| 99 |
int n) |
| 100 |
{ |
| 101 |
int err; |
| 102 |
|
| 103 |
w->i = i; |
| 104 |
w->a = a; |
| 105 |
w->n = n; |
| 106 |
|
| 107 |
test_server_connect(server, &w->client); |
| 108 |
|
| 109 |
err = pthread_create(&w->thread, 0, &__worker_run, (void *)w); |
| 110 |
if (err) { |
| 111 |
munit_errorf("failed to spawn test worker thread: %s", |
| 112 |
strerror(errno)); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static void __worker_wait(struct worker *w) |
| 117 |
{ |
| 118 |
int err; |
| 119 |
void *retval; |
| 120 |
|
| 121 |
err = pthread_join(w->thread, &retval); |
| 122 |
if (err) { |
| 123 |
munit_errorf("failed to wait test worker thread: %s", |
| 124 |
strerror(errno)); |
| 125 |
} |
| 126 |
|
| 127 |
test_client_close(w->client); |
| 128 |
free(w->client); |
| 129 |
} |
| 130 |
|
| 131 |
/****************************************************************************** |
| 132 |
* |
| 133 |
* Setup and tear down |
| 134 |
* |
| 135 |
******************************************************************************/ |
| 136 |
|
| 137 |
static void *setup(const MunitParameter params[], void *user_data) |
| 138 |
{ |
| 139 |
struct test_server *server; |
| 140 |
const char *errmsg; |
| 141 |
int err; |
| 142 |
|
| 143 |
(void)user_data; |
| 144 |
(void)params; |
| 145 |
|
| 146 |
err = cowsql_init(&errmsg); |
| 147 |
munit_assert_int(err, ==, 0); |
| 148 |
|
| 149 |
server = test_server_start("unix", params); |
| 150 |
|
| 151 |
return server; |
| 152 |
} |
| 153 |
|
| 154 |
static void tear_down(void *data) |
| 155 |
{ |
| 156 |
struct test_server *server = data; |
| 157 |
int rc; |
| 158 |
|
| 159 |
test_server_stop(server); |
| 160 |
|
| 161 |
rc = sqlite3_shutdown(); |
| 162 |
munit_assert_int(rc, ==, 0); |
| 163 |
} |
| 164 |
|
| 165 |
/****************************************************************************** |
| 166 |
* |
| 167 |
* Tests |
| 168 |
* |
| 169 |
******************************************************************************/ |
| 170 |
|
| 171 |
TEST_SUITE(exec); |
| 172 |
TEST_SETUP(exec, setup); |
| 173 |
TEST_TEAR_DOWN(exec, tear_down); |
| 174 |
|
| 175 |
#include <unistd.h> |
| 176 |
|
| 177 |
TEST_CASE(exec, single_query, NULL) |
| 178 |
{ |
| 179 |
struct test_server *server = data; |
| 180 |
struct test_client *client; |
| 181 |
char *leader; |
| 182 |
uint64_t heartbeat; |
| 183 |
uint32_t db_id; |
| 184 |
uint32_t stmt_id; |
| 185 |
struct test_client_result result; |
| 186 |
struct test_client_rows rows; |
| 187 |
|
| 188 |
(void)params; |
| 189 |
|
| 190 |
test_server_connect(server, &client); |
| 191 |
|
| 192 |
/* Initialize the connection and open a database. */ |
| 193 |
test_client_handshake(client); |
| 194 |
test_client_leader(client, &leader); |
| 195 |
test_client_client(client, &heartbeat); |
| 196 |
test_client_open(client, "test.db", &db_id); |
| 197 |
munit_assert_int(db_id, ==, 0); |
| 198 |
|
| 199 |
/* Create a test table. */ |
| 200 |
test_client_prepare(client, db_id, "CREATE TABLE test (n INT)", |
| 201 |
&stmt_id); |
| 202 |
test_client_exec(client, db_id, stmt_id, &result); |
| 203 |
test_client_finalize(client, db_id, stmt_id); |
| 204 |
|
| 205 |
/* Insert a row in the test table. */ |
| 206 |
test_client_prepare(client, db_id, "INSERT INTO test VALUES(123)", |
| 207 |
&stmt_id); |
| 208 |
|
| 209 |
munit_assert_int(stmt_id, ==, 0); |
| 210 |
|
| 211 |
test_client_exec(client, db_id, stmt_id, &result); |
| 212 |
|
| 213 |
munit_assert_int(result.last_insert_id, ==, 1); |
| 214 |
munit_assert_int(result.rows_affected, ==, 1); |
| 215 |
|
| 216 |
test_client_finalize(client, db_id, stmt_id); |
| 217 |
|
| 218 |
/* Select rows from the test table. */ |
| 219 |
test_client_prepare(client, db_id, "SELECT n FROM test", &stmt_id); |
| 220 |
|
| 221 |
munit_assert_int(stmt_id, ==, 0); |
| 222 |
|
| 223 |
test_client_query(client, db_id, stmt_id, &rows); |
| 224 |
|
| 225 |
munit_assert_int(rows.column_count, ==, 1); |
| 226 |
munit_assert_string_equal(rows.column_names[0], "n"); |
| 227 |
|
| 228 |
munit_assert_ptr_not_null(rows.next); |
| 229 |
munit_assert_int(rows.next->types[0], ==, SQLITE_INTEGER); |
| 230 |
munit_assert_int(*(int64_t *)rows.next->values[0], ==, 123); |
| 231 |
|
| 232 |
test_client_rows_close(&rows); |
| 233 |
|
| 234 |
test_client_finalize(client, db_id, stmt_id); |
| 235 |
|
| 236 |
test_client_close(client); |
| 237 |
free(client); |
| 238 |
|
| 239 |
return MUNIT_OK; |
| 240 |
} |
| 241 |
|
| 242 |
TEST_CASE(exec, large_query, NULL) |
| 243 |
{ |
| 244 |
struct test_server *server = data; |
| 245 |
struct test_client *client; |
| 246 |
char *leader; |
| 247 |
uint64_t heartbeat; |
| 248 |
uint32_t db_id; |
| 249 |
uint32_t stmt_id; |
| 250 |
struct test_client_result result; |
| 251 |
struct test_client_rows rows; |
| 252 |
int i; |
| 253 |
|
| 254 |
(void)params; |
| 255 |
|
| 256 |
test_server_connect(server, &client); |
| 257 |
|
| 258 |
/* Initialize the connection and open a database. */ |
| 259 |
test_client_handshake(client); |
| 260 |
test_client_leader(client, &leader); |
| 261 |
test_client_client(client, &heartbeat); |
| 262 |
test_client_open(client, "test.db", &db_id); |
| 263 |
munit_assert_int(db_id, ==, 0); |
| 264 |
|
| 265 |
/* Create a test table. */ |
| 266 |
test_client_prepare(client, db_id, "CREATE TABLE test (n INT)", |
| 267 |
&stmt_id); |
| 268 |
test_client_exec(client, db_id, stmt_id, &result); |
| 269 |
test_client_finalize(client, db_id, stmt_id); |
| 270 |
|
| 271 |
test_client_prepare(client, db_id, "BEGIN", &stmt_id); |
| 272 |
test_client_exec(client, db_id, stmt_id, &result); |
| 273 |
test_client_finalize(client, db_id, stmt_id); |
| 274 |
|
| 275 |
/* Insert lots of rows in the test table. */ |
| 276 |
test_client_prepare(client, db_id, "INSERT INTO test VALUES(123456789)", |
| 277 |
&stmt_id); |
| 278 |
|
| 279 |
for (i = 0; i < 256; i++) { |
| 280 |
munit_assert_int(stmt_id, ==, 0); |
| 281 |
test_client_exec(client, db_id, stmt_id, &result); |
| 282 |
munit_assert_int(result.rows_affected, ==, 1); |
| 283 |
} |
| 284 |
|
| 285 |
test_client_finalize(client, db_id, stmt_id); |
| 286 |
|
| 287 |
test_client_prepare(client, db_id, "COMMIT", &stmt_id); |
| 288 |
test_client_exec(client, db_id, stmt_id, &result); |
| 289 |
test_client_finalize(client, db_id, stmt_id); |
| 290 |
|
| 291 |
/* Select all rows from the test table. */ |
| 292 |
test_client_prepare(client, db_id, "SELECT n FROM test", &stmt_id); |
| 293 |
|
| 294 |
munit_assert_int(stmt_id, ==, 0); |
| 295 |
|
| 296 |
test_client_query(client, db_id, stmt_id, &rows); |
| 297 |
|
| 298 |
munit_assert_int(rows.column_count, ==, 1); |
| 299 |
munit_assert_string_equal(rows.column_names[0], "n"); |
| 300 |
|
| 301 |
munit_assert_ptr_not_null(rows.next); |
| 302 |
munit_assert_int(rows.next->types[0], ==, SQLITE_INTEGER); |
| 303 |
munit_assert_int(*(int64_t *)rows.next->values[0], ==, 123456789); |
| 304 |
|
| 305 |
test_client_rows_close(&rows); |
| 306 |
|
| 307 |
test_client_finalize(client, db_id, stmt_id); |
| 308 |
|
| 309 |
test_client_close(client); |
| 310 |
|
| 311 |
free(client); |
| 312 |
|
| 313 |
return MUNIT_OK; |
| 314 |
} |
| 315 |
|
| 316 |
TEST_CASE(exec, multi_thread, NULL) |
| 317 |
{ |
| 318 |
struct test_server *server = data; |
| 319 |
struct worker *workers; |
| 320 |
struct test_client *client; |
| 321 |
struct test_client_result result; |
| 322 |
char *leader; |
| 323 |
uint64_t heartbeat; |
| 324 |
uint32_t db_id; |
| 325 |
uint32_t stmt_id; |
| 326 |
|
| 327 |
(void)params; |
| 328 |
|
| 329 |
int n = 2; |
| 330 |
int i; |
| 331 |
|
| 332 |
test_server_connect(server, &client); |
| 333 |
|
| 334 |
/* Initialize the connection and open a database. */ |
| 335 |
test_client_handshake(client); |
| 336 |
test_client_leader(client, &leader); |
| 337 |
test_client_client(client, &heartbeat); |
| 338 |
test_client_open(client, "test.db", &db_id); |
| 339 |
munit_assert_int(db_id, ==, 0); |
| 340 |
|
| 341 |
/* Create a test table and close this client. */ |
| 342 |
test_client_prepare(client, db_id, "CREATE TABLE test (n INT)", |
| 343 |
&stmt_id); |
| 344 |
test_client_exec(client, db_id, stmt_id, &result); |
| 345 |
test_client_finalize(client, db_id, stmt_id); |
| 346 |
|
| 347 |
test_client_close(client); |
| 348 |
|
| 349 |
/* Spawn the workers. */ |
| 350 |
workers = munit_malloc(n * sizeof *workers); |
| 351 |
|
| 352 |
for (i = 0; i < n; i++) { |
| 353 |
__worker_start(&(workers[i]), server, i, i * 100000, 4); |
| 354 |
} |
| 355 |
|
| 356 |
/* Wait for the workers. */ |
| 357 |
for (i = 0; i < n; i++) { |
| 358 |
__worker_wait(&(workers[i])); |
| 359 |
} |
| 360 |
|
| 361 |
free(client); |
| 362 |
free(workers); |
| 363 |
|
| 364 |
return MUNIT_OK; |
| 365 |
} |
| 366 |
|
| 367 |
#endif |