Skip to content

Commit a6321d3

Browse files
committed
refactor: multi-threaded sync testing with random text insert
1 parent d091c2c commit a6321d3

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

test/main.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
#include <stdio.h>
1010
#include <stdlib.h>
11+
#include <utils.h>
1112
#include "sqlite3.h"
13+
#include <pthread.h>
1214

1315
#define PEERS 50
1416
#define DB_PATH "health-track.sqlite"
@@ -182,10 +184,14 @@ int test_init (const char *db_path, int init) {
182184
snprintf(network_init, sizeof(network_init), "SELECT cloudsync_network_init('%s?apikey=%s');", getenv("CONNECTION_STRING"), getenv("APIKEY"));
183185
rc = db_exec(db, network_init); RCHECK
184186

185-
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM users;", 0); RCHECK
186187
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM activities;", 0); RCHECK
187188
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM workouts;", 0); RCHECK
188-
//rc = db_exec(db, "INSERT INTO users (id, name) VALUES ('test', 'test')"); RCHECK
189+
char value[UUID_STR_MAXLEN];
190+
cloudsync_uuid_v7_string(value, true);
191+
char sql[256];
192+
snprintf(sql, sizeof(sql), "INSERT INTO users (id, name) VALUES ('%s', '%s');", value, value);
193+
rc = db_exec(db, sql); RCHECK
194+
rc = db_expect_int(db, "SELECT COUNT(*) as count FROM users;", 1); RCHECK
189195
rc = db_expect_gt0(db, "SELECT cloudsync_network_sync();"); RCHECK
190196
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM users;"); RCHECK
191197
rc = db_expect_gt0(db, "SELECT COUNT(*) as count FROM activities;"); RCHECK
@@ -253,6 +259,19 @@ int test_report(const char *description, int rc){
253259
return rc;
254260
}
255261

262+
void* worker(void* arg) {
263+
int thread_id = *(int*)arg;
264+
265+
char description[32];
266+
snprintf(description, sizeof(description), "%d/%d Peer Test", thread_id, PEERS);
267+
if(test_report(description, test_init(":memory:", 1))){
268+
printf("PEER %d FAIL.\n", thread_id);
269+
exit(thread_id+1);
270+
}
271+
272+
return NULL;
273+
}
274+
256275
int main (void) {
257276
int rc = SQLITE_OK;
258277

@@ -272,10 +291,20 @@ int main (void) {
272291

273292
remove(DB_PATH); // remove the database file
274293

275-
for(int i=0; i<PEERS; i++){
276-
char description[32];
277-
snprintf(description, sizeof(description), "%d/%d Peer Test", i+1, PEERS);
278-
rc += test_report(description, test_init(":memory:", 1));
294+
pthread_t threads[PEERS];
295+
int thread_ids[PEERS];
296+
297+
for (int i = 0; i < PEERS; i++) {
298+
thread_ids[i] = i;
299+
if (pthread_create(&threads[i], NULL, worker, &thread_ids[i]) != 0) {
300+
perror("pthread_create");
301+
exit(1);
302+
}
303+
}
304+
305+
// Wait for all threads to finish
306+
for (int i = 0; i < PEERS; i++) {
307+
pthread_join(threads[i], NULL);
279308
}
280309

281310
printf("\n");

0 commit comments

Comments
 (0)