/* ~/.pic/pic.sqlite's store settings, its fair busy handler, picu_project, and * user-state writes under a long writer (the stand-in for a clone round). * Every child process carries its own deadline (alarm), so none outlives a * killed test. HOME must be a private directory. */ #include #include #include #include #include #include #include #include #include #include "platform.h" #include "userdb.h" #define WRITERS 8 #define WRITES 50 static long long now_us(void) { return (long long)(pic_platform_get()->mono_ns() / 1000); } static void sleep_ms(long ms) { struct timespec ts = {ms / 1000, (ms % 1000) * 1000000L}; while (nanosleep(&ts, &ts) != 0) { } } static long long pragma(sqlite3 *db, const char *sql) { sqlite3_stmt *q = NULL; long long v = -1; if (sqlite3_prepare_v2(db, sql, -1, &q, NULL) == SQLITE_OK && sqlite3_step(q) == SQLITE_ROW) { v = sqlite3_column_int64(q, 0); } sqlite3_finalize(q); return v; } static int exec(sqlite3 *db, const char *sql) { return sqlite3_exec(db, sql, NULL, NULL, NULL) == SQLITE_OK ? 0 : -1; } static int count_project(void *ctx, const pic_project *p) { int *n = ctx; assert(strcmp(p->name, "demo") == 0 && strcmp(p->prefix, "fslrepo_demo_") == 0); assert(strcmp(p->remote, "https://example.org/demo") == 0 && strcmp(p->cred, "DEMO_PW") == 0); assert(strcmp(p->kind, "fossil") == 0 && strcmp(p->state, "cloning") == 0 && p->created_ms == 7); (*n)++; return 0; } static void test_settings(void) { char err[256] = ""; sqlite3 *db = pic_userdb(err, sizeof err); sqlite3 *other = NULL; assert(db); assert(pragma(db, "PRAGMA synchronous") == 2); /* FULL */ assert(pragma(db, "PRAGMA foreign_keys") == 0); #ifdef __APPLE__ assert(pragma(db, "PRAGMA fullfsync") == 1); #endif /* Any other opener gets the same settings from pic_userdb_settings. */ assert(sqlite3_open(":memory:", &other) == SQLITE_OK); assert(exec(other, "PRAGMA synchronous=OFF") == 0); assert(pic_userdb_settings(other) == 0); assert(pragma(other, "PRAGMA synchronous") == 2); #ifdef __APPLE__ assert(pragma(other, "PRAGMA fullfsync") == 1); #endif sqlite3_close(other); puts("ok store settings"); } static void test_project_table(void) { sqlite3 *db = pic_userdb(NULL, 0); int n = 0; assert(pic_projects_each(count_project, &n) == 0 && n == 0); assert(exec(db, "INSERT INTO picu_project(name, prefix, remote, cred, kind, state, created_ms)" " VALUES('demo', 'fslrepo_demo_', 'https://example.org/demo', 'DEMO_PW', 'fossil', 'cloning', 7)") == 0); assert(pic_projects_each(count_project, &n) == 0 && n == 1); /* kind, state and the userinfo-free remote are enforced by the table. */ assert(exec(db, "INSERT INTO picu_project(name, prefix, remote, kind, state, created_ms)" " VALUES('x', 'fslrepo_x_', 'https://h/x', 'git', 'ready', 1)") != 0); assert(exec(db, "INSERT INTO picu_project(name, prefix, remote, kind, state, created_ms)" " VALUES('x', 'fslrepo_x_', 'https://h/x', 'zit', 'done', 1)") != 0); assert(exec(db, "INSERT INTO picu_project(name, prefix, remote, kind, state, created_ms)" " VALUES('x', 'fslrepo_x_', 'https://u:pw@h/x', 'zit', 'ready', 1)") != 0); assert(exec(db, "DELETE FROM picu_project") == 0); puts("ok picu_project"); } /* A child that holds the write lock for hold_ms, after telling the parent it has it. */ static pid_t holder(long hold_ms) { int fds[2]; char c; pid_t pid; assert(pipe(fds) == 0); pid = fork(); assert(pid >= 0); if (pid == 0) { sqlite3 *db; alarm(30); close(fds[0]); db = pic_userdb(NULL, 0); if (!db || exec(db, "BEGIN IMMEDIATE") != 0 || exec(db, "INSERT INTO picu_setting(key, value, updated_ms) VALUES('held', 'x', 0)" " ON CONFLICT(key) DO UPDATE SET updated_ms = updated_ms + 1") != 0) { _exit(1); } (void)!write(fds[1], "h", 1); sleep_ms(hold_ms); _exit(exec(db, "COMMIT") == 0 ? 0 : 1); } close(fds[1]); assert(read(fds[0], &c, 1) == 1); close(fds[0]); return pid; } static void test_busy(void) { int status; long long t; pid_t pid; pic_userdb_close(); pid = holder(1000); t = now_us(); assert(pic_setting_set("busy", "1") == 0); /* waits the lock out */ t = now_us() - t; assert(t >= 800000); assert(waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0); pic_userdb_close(); pid = holder(7000); t = now_us(); assert(pic_setting_set("busy", "2") != 0); /* gives up at the 5 s deadline */ t = now_us() - t; printf("ok busy handler: waited out a 1 s holder; gave up on a 7 s one after %lld ms\n", t / 1000); assert(t >= 4900000 && t < 6500000); assert(waitpid(pid, &status, 0) == pid && WIFEXITED(status) && WEXITSTATUS(status) == 0); pic_userdb_close(); } static int cmp_ll(const void *a, const void *b) { long long x = *(const long long *)a, y = *(const long long *)b; return (x > y) - (x < y); } /* 8 writers of user state while a long writer holds ~100 ms transactions. */ static void test_contention(void) { int fds[2]; pid_t writers[WRITERS], longw; long long lat[WRITERS * WRITES]; size_t got = 0; int status, failed = 0; pic_userdb_close(); assert(pipe(fds) == 0); longw = fork(); assert(longw >= 0); if (longw == 0) { sqlite3 *db; alarm(60); close(fds[0]); close(fds[1]); db = pic_userdb(NULL, 0); if (!db || exec(db, "CREATE TABLE IF NOT EXISTS test_clone_round(n INTEGER, payload BLOB)") != 0) { _exit(1); } for (;;) { /* a clone round: one write transaction of ~100 ms, then the network */ if (exec(db, "BEGIN IMMEDIATE") != 0 || exec(db, "INSERT INTO test_clone_round VALUES(1, randomblob(65536))") != 0) { _exit(1); } sleep_ms(100); if (exec(db, "COMMIT") != 0) { _exit(1); } sleep_ms(20); } } sleep_ms(50); for (int w = 0; w < WRITERS; w++) { writers[w] = fork(); assert(writers[w] >= 0); if (writers[w] == 0) { long long mine[WRITES]; char key[32]; alarm(60); close(fds[0]); (void)snprintf(key, sizeof key, "writer%d", w); for (int i = 0; i < WRITES; i++) { long long t = now_us(); mine[i] = pic_setting_set(key, "v") == 0 ? now_us() - t : -1; sleep_ms(5 + (w + i) % 7); } _exit(write(fds[1], mine, sizeof mine) == (ssize_t)sizeof mine ? 0 : 1); } } close(fds[1]); for (;;) { ssize_t n = read(fds[0], (char *)lat + got, sizeof lat - got); if (n <= 0) { break; } got += (size_t)n; } close(fds[0]); for (int w = 0; w < WRITERS; w++) { assert(waitpid(writers[w], &status, 0) == writers[w] && WIFEXITED(status) && WEXITSTATUS(status) == 0); } kill(longw, SIGTERM); assert(waitpid(longw, &status, 0) == longw && WIFSIGNALED(status)); assert(got == sizeof lat); for (size_t i = 0; i < WRITERS * WRITES; i++) { failed += lat[i] < 0; } qsort(lat, WRITERS * WRITES, sizeof lat[0], cmp_ll); printf("contention: %d writers x %d user-state writes beside 100 ms write transactions:" " p50 %.1f ms, p99 %.1f ms, max %.1f ms, failed %d\n", WRITERS, WRITES, (double)lat[WRITERS * WRITES / 2] / 1000.0, (double)lat[WRITERS * WRITES * 99 / 100] / 1000.0, (double)lat[WRITERS * WRITES - 1] / 1000.0, failed); assert(failed == 0); } int main(void) { test_settings(); test_project_table(); test_busy(); test_contention(); puts("test_userdb: all passed"); return 0; }