feat: implement MoloApp plans 01-15 with integration
Full C++23 framework tree: runtime, observability, security, data, app, realtime, supervisor, clients, CLI, example plus enterprise tenancy, audit, operability, governance, supplychain. Postgres via docker compose for tests.
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
// Minimal test helper: stdlib only, loud failures with line numbers.
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
static int g_checks = 0;
|
||||
|
||||
#define CHECK(cond) \
|
||||
do { \
|
||||
++g_checks; \
|
||||
if (!(cond)) { \
|
||||
std::cerr << "FAIL " << __FILE__ << ":" << __LINE__ << ": " #cond \
|
||||
<< std::endl; \
|
||||
return 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define CHECK_EQ(a, b) \
|
||||
do { \
|
||||
++g_checks; \
|
||||
if (!((a) == (b))) { \
|
||||
std::cerr << "FAIL " << __FILE__ << ":" << __LINE__ << ": " #a \
|
||||
<< " != " #b << std::endl; \
|
||||
return 1; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define SUITE_DONE(name) \
|
||||
do { \
|
||||
std::cout << "PASS " << name << " (" << g_checks << " checks)" \
|
||||
<< std::endl; \
|
||||
return 0; \
|
||||
} while (0)
|
||||
@@ -0,0 +1,217 @@
|
||||
// Step 8: KV store plus cache with TTL, tags, LRU, singleflight and
|
||||
// ORM write-through invalidation.
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "cache/cache.hpp"
|
||||
#include "cache/policy.hpp"
|
||||
#include "cache/singleflight.hpp"
|
||||
#include "context.hpp"
|
||||
#include "drivers/sqlite/db.hpp"
|
||||
#include "drivers/sqlite/store.hpp"
|
||||
#include "kv/memory.hpp"
|
||||
#include "kv/store.hpp"
|
||||
#include "mini.hpp"
|
||||
#include "orm/invalidation.hpp"
|
||||
#include "orm/model.hpp"
|
||||
#include "orm/sync.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::kv;
|
||||
using namespace molo::data::cache;
|
||||
|
||||
class Doc : public orm::Model<Doc> {
|
||||
public:
|
||||
static std::string table_name() { return "docs"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<orm::FieldDef> fields() {
|
||||
return {orm::FieldDef::primary_key(),
|
||||
orm::FieldDef::varchar("title", 32)};
|
||||
}
|
||||
};
|
||||
|
||||
static Bytes to_bytes(const std::string& s) {
|
||||
return Bytes(s.begin(), s.end());
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Basic set/get/del/exists.
|
||||
{
|
||||
MemoryKv kv;
|
||||
CHECK(!kv.get("a").has_value());
|
||||
kv.set("a", to_bytes("1"), 60);
|
||||
CHECK(kv.exists("a"));
|
||||
CHECK(kv.get("a") == to_bytes("1"));
|
||||
CHECK(kv.del("a"));
|
||||
CHECK(!kv.exists("a"));
|
||||
}
|
||||
|
||||
// TTL expiry.
|
||||
{
|
||||
MemoryKv kv;
|
||||
kv.set("short", to_bytes("x"), 1);
|
||||
CHECK(kv.exists("short"));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1100));
|
||||
CHECK(!kv.get("short").has_value());
|
||||
}
|
||||
|
||||
// Scan pagination.
|
||||
{
|
||||
MemoryKv kv;
|
||||
for (int i = 0; i < 25; ++i)
|
||||
kv.set("user:" + std::to_string(i), to_bytes("v"), 60);
|
||||
kv.set("other:1", to_bytes("v"), 60);
|
||||
ScanPage p1 = kv.scan("user:", "", 10);
|
||||
CHECK(p1.keys.size() == 10 && !p1.done);
|
||||
ScanPage p2 = kv.scan("user:", p1.next_cursor, 10);
|
||||
CHECK(p2.keys.size() == 10 && !p2.done);
|
||||
ScanPage p3 = kv.scan("user:", p2.next_cursor, 10);
|
||||
CHECK(p3.keys.size() == 5 && p3.done);
|
||||
}
|
||||
|
||||
// LRU eviction past the key cap.
|
||||
{
|
||||
MemoryKv kv(4);
|
||||
kv.set("a", to_bytes("1"), 60);
|
||||
kv.set("b", to_bytes("1"), 60);
|
||||
kv.set("c", to_bytes("1"), 60);
|
||||
kv.set("d", to_bytes("1"), 60);
|
||||
(void)kv.get("a"); // refresh a
|
||||
kv.set("e", to_bytes("1"), 60);
|
||||
CHECK(kv.exists("a"));
|
||||
CHECK(!kv.exists("b")); // least recently used evicted
|
||||
CHECK(kv.evictions() == 1);
|
||||
}
|
||||
|
||||
// Key and value caps enforced.
|
||||
{
|
||||
MemoryKv kv;
|
||||
bool threw = false;
|
||||
try {
|
||||
kv.set("", to_bytes("x"), 60);
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
threw = false;
|
||||
try {
|
||||
kv.set("k", Bytes(17 * 1024 * 1024, 0), 60);
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// Cache get-or-compute with tags and explicit invalidation.
|
||||
{
|
||||
MemoryKv kv;
|
||||
Cache cache(kv);
|
||||
int computes = 0;
|
||||
Bytes v = cache.get_or_compute(
|
||||
"u:1", {tag_for_table("users")},
|
||||
[&] {
|
||||
++computes;
|
||||
return to_bytes("ada");
|
||||
});
|
||||
CHECK(v == to_bytes("ada") && computes == 1);
|
||||
Bytes v2 = cache.get_or_compute(
|
||||
"u:1", {tag_for_table("users")},
|
||||
[&] {
|
||||
++computes;
|
||||
return to_bytes("ada");
|
||||
});
|
||||
CHECK(v2 == to_bytes("ada") && computes == 1); // served from cache
|
||||
CHECK(cache.hits() == 1); // second call hits; first call missed twice
|
||||
cache.invalidate("u:1");
|
||||
CHECK(!cache.get("u:1").has_value());
|
||||
cache.put("t:1", to_bytes("x"), {tag_for_table("teams")});
|
||||
cache.put("t:2", to_bytes("y"), {tag_for_table("teams")});
|
||||
CHECK(cache.purge_tag(tag_for_table("teams")) == 2);
|
||||
CHECK(!cache.get("t:1").has_value());
|
||||
}
|
||||
|
||||
// Cache LRU at entry cap.
|
||||
{
|
||||
MemoryKv kv;
|
||||
CachePolicy policy;
|
||||
policy.max_entries = 3;
|
||||
Cache cache(kv, policy);
|
||||
cache.put("a", to_bytes("1"));
|
||||
cache.put("b", to_bytes("1"));
|
||||
cache.put("c", to_bytes("1"));
|
||||
(void)cache.get("a");
|
||||
cache.put("d", to_bytes("1"));
|
||||
CHECK(cache.get("a").has_value());
|
||||
CHECK(!cache.get("b").has_value());
|
||||
}
|
||||
|
||||
// Stampede: 100 concurrent computes collapse to 1.
|
||||
{
|
||||
MemoryKv kv;
|
||||
Cache cache(kv);
|
||||
std::atomic<int> computes = 0;
|
||||
std::vector<std::thread> threads;
|
||||
std::atomic<int> ok = 0;
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
threads.emplace_back([&] {
|
||||
Bytes v = cache.get_or_compute(
|
||||
"hot", {}, [&] {
|
||||
++computes;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||
return to_bytes("value");
|
||||
});
|
||||
if (v == to_bytes("value")) ++ok;
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) t.join();
|
||||
CHECK(ok == 100);
|
||||
CHECK(computes == 1);
|
||||
}
|
||||
|
||||
// SingleFlight shares results directly too.
|
||||
{
|
||||
SingleFlight<int> flight;
|
||||
std::atomic<int> runs = 0;
|
||||
std::atomic<int> bad = 0;
|
||||
std::vector<std::thread> threads;
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
threads.emplace_back([&] {
|
||||
const int v = flight.compute("k", [&] {
|
||||
++runs;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(20));
|
||||
return 7;
|
||||
});
|
||||
if (v != 7) ++bad;
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) t.join();
|
||||
CHECK(runs == 1);
|
||||
CHECK(bad == 0);
|
||||
}
|
||||
|
||||
// ORM write invalidates the cached read across the bus.
|
||||
{
|
||||
auto store = std::make_shared<sqlite::MemoryStore>();
|
||||
sqlite::SqliteDb db(sqlite::SqliteConfig{}, store);
|
||||
db.open(Context::background("test"));
|
||||
Context ctx = Context::background("test");
|
||||
orm::Syncer().sync(ctx, db, {orm::describe_model<Doc>()});
|
||||
orm::InvalidationBus bus;
|
||||
MemoryKv kv;
|
||||
Cache cache(kv);
|
||||
cache.attach_bus(bus);
|
||||
|
||||
Doc d;
|
||||
d.set("title", make_text("v1"));
|
||||
d.save(db, ctx, &bus, 1);
|
||||
cache.put("doc:list", to_bytes("v1"), {tag_for_table("docs")});
|
||||
CHECK(cache.get("doc:list").has_value());
|
||||
d.set("title", make_text("v2"));
|
||||
d.save(db, ctx, &bus, 2); // write-through purge
|
||||
CHECK(!cache.get("doc:list").has_value());
|
||||
}
|
||||
|
||||
SUITE_DONE("kv_cache");
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
// Step 5: ORM core against the real SQLite path, plus Postgres SQL
|
||||
// generation proof through a scripted connection.
|
||||
#include <vector>
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "context.hpp"
|
||||
#include "drivers/sqlite/db.hpp"
|
||||
#include "drivers/sqlite/store.hpp"
|
||||
#include "mini.hpp"
|
||||
#include "orm/invalidation.hpp"
|
||||
#include "orm/model.hpp"
|
||||
#include "orm/query.hpp"
|
||||
#include "orm/relations.hpp"
|
||||
#include "orm/sync.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::orm;
|
||||
using namespace molo::data::sqlite;
|
||||
|
||||
class User : public Model<User> {
|
||||
public:
|
||||
static std::string table_name() { return "users"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
auto id = FieldDef::primary_key();
|
||||
auto name = FieldDef::varchar("name", 32);
|
||||
name.indexed = true;
|
||||
auto email = FieldDef::text("email");
|
||||
email.unique = true;
|
||||
return {id, name, email};
|
||||
}
|
||||
};
|
||||
|
||||
class Todo : public Model<Todo> {
|
||||
public:
|
||||
static std::string table_name() { return "todos"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
auto id = FieldDef::primary_key();
|
||||
auto user_id = FieldDef::bigint("user_id");
|
||||
user_id.indexed = true;
|
||||
auto title = FieldDef::varchar("title", 64);
|
||||
auto done = FieldDef::boolean("done");
|
||||
return {id, user_id, title, done};
|
||||
}
|
||||
};
|
||||
|
||||
class Membership : public Model<Membership> {
|
||||
public:
|
||||
static std::string table_name() { return "memberships"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
return {FieldDef::primary_key(), FieldDef::bigint("user_id"),
|
||||
FieldDef::bigint("team_id")};
|
||||
}
|
||||
};
|
||||
|
||||
class Team : public Model<Team> {
|
||||
public:
|
||||
static std::string table_name() { return "teams"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
return {FieldDef::primary_key(), FieldDef::varchar("name", 32)};
|
||||
}
|
||||
};
|
||||
|
||||
// Scripted Postgres-side connection: records SQL, returns canned results.
|
||||
class ScriptedPg : public Connection {
|
||||
public:
|
||||
Backend backend() const override { return Backend::Postgres; }
|
||||
std::string backend_id() const override { return "pg:scripted"; }
|
||||
Result exec(const Context&, const BoundStatement& stmt) override {
|
||||
seen.push_back(stmt.sql());
|
||||
return Result{};
|
||||
}
|
||||
Prepared prepare(const Context&, const BoundStatement& stmt) override {
|
||||
return Prepared{"p", stmt.sql()};
|
||||
}
|
||||
Result exec_prepared(const Context&, const Prepared&,
|
||||
const std::vector<Value>&) override {
|
||||
return Result{};
|
||||
}
|
||||
void ping(const Context&) override {}
|
||||
void close() override {}
|
||||
std::vector<std::string> seen;
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db(SqliteConfig{}, store);
|
||||
db.open(Context::background("test"));
|
||||
Context ctx = Context::background("test");
|
||||
|
||||
// Syncer boots the schema from models.
|
||||
Syncer syncer;
|
||||
SyncReport rep = syncer.sync(ctx, db,
|
||||
{describe_model<User>(), describe_model<Todo>(),
|
||||
describe_model<Membership>(),
|
||||
describe_model<Team>()});
|
||||
CHECK(rep.tables_created == 4);
|
||||
CHECK(rep.indexes_added == 2); // users.name, todos.user_id
|
||||
CHECK(store->has_table("users") && store->has_table("todos"));
|
||||
|
||||
// Second sync is a no-op diff.
|
||||
SyncReport rep2 =
|
||||
syncer.sync(ctx, db, {describe_model<User>(), describe_model<Todo>()});
|
||||
CHECK(rep2.tables_created == 0 && rep2.columns_added == 0);
|
||||
|
||||
// CRUD with hooks and invalidation events.
|
||||
InvalidationBus bus;
|
||||
std::vector<Invalidation> events;
|
||||
bus.subscribe([&](const Invalidation& inv) { events.push_back(inv); });
|
||||
|
||||
User ada;
|
||||
ada.set("name", make_text("ada"));
|
||||
ada.set("email", make_text("ada@ex.dev"));
|
||||
ada.save(db, ctx, &bus, 41);
|
||||
CHECK(ada.persisted());
|
||||
CHECK(std::get<std::int64_t>(ada.get("id")) == 1);
|
||||
CHECK(events.size() == 1 && events[0].op == "insert" &&
|
||||
events[0].txid == 41 && events[0].table == "users");
|
||||
|
||||
ada.set("email", make_text("ada2@ex.dev"));
|
||||
ada.save(db, ctx, &bus, 42);
|
||||
CHECK(events.size() == 2 && events[1].op == "update");
|
||||
|
||||
std::vector<User> found =
|
||||
Query<User>(db).filter("name", make_text("ada")).all(ctx);
|
||||
CHECK(found.size() == 1);
|
||||
CHECK(std::get<std::string>(found[0].get("email")) == "ada2@ex.dev");
|
||||
CHECK(Query<User>(db).filter("name", make_text("ada")).exists(ctx));
|
||||
CHECK(!Query<User>(db).filter("name", make_text("nobody")).exists(ctx));
|
||||
CHECK(Query<User>(db).count(ctx) == 1);
|
||||
|
||||
// Injection filter binds literally.
|
||||
CHECK(Query<User>(db).filter("name", make_text("' OR '1'='1")).count(ctx) ==
|
||||
0);
|
||||
|
||||
ada.remove(db, ctx, &bus, 43);
|
||||
CHECK(events.size() == 3 && events[2].op == "delete");
|
||||
CHECK(Query<User>(db).count(ctx) == 0);
|
||||
|
||||
// Relations: HasMany lazy and eager, BelongsTo, ManyToMany.
|
||||
User grace;
|
||||
grace.set("name", make_text("grace"));
|
||||
grace.set("email", make_text("g@ex.dev"));
|
||||
grace.save(db, ctx);
|
||||
User ed;
|
||||
ed.set("name", make_text("edsger"));
|
||||
ed.set("email", make_text("e@ex.dev"));
|
||||
ed.save(db, ctx);
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
Todo t;
|
||||
t.set("user_id", grace.get("id"));
|
||||
t.set("title", make_text("g" + std::to_string(i)));
|
||||
t.set("done", make_bool(false));
|
||||
t.save(db, ctx);
|
||||
}
|
||||
Todo solo;
|
||||
solo.set("user_id", ed.get("id"));
|
||||
solo.set("title", make_text("solo"));
|
||||
solo.set("done", make_bool(true));
|
||||
solo.save(db, ctx);
|
||||
|
||||
HasMany<User, Todo> user_todos{"user_id", "id"};
|
||||
CHECK(user_todos.load(db, ctx, grace).size() == 3);
|
||||
BelongsTo<Todo, User> todo_user{"user_id", "id"};
|
||||
std::vector<Todo> one =
|
||||
Query<Todo>(db).filter("title", make_text("solo")).all(ctx);
|
||||
CHECK(todo_user.load(db, ctx, one[0]).size() == 1);
|
||||
|
||||
std::vector<User> users = Query<User>(db).order("id").all(ctx);
|
||||
auto grouped = user_todos.preload(db, ctx, users);
|
||||
CHECK(grouped["2"].size() == 3); // grace id 2
|
||||
CHECK(grouped["3"].size() == 1); // ed id 3
|
||||
|
||||
Team a;
|
||||
a.set("name", make_text("core"));
|
||||
a.save(db, ctx);
|
||||
Membership m;
|
||||
m.set("user_id", grace.get("id"));
|
||||
m.set("team_id", a.get("id"));
|
||||
m.save(db, ctx);
|
||||
ManyToMany<User, Team> user_teams{"memberships", "user_id", "team_id", "id",
|
||||
"id"};
|
||||
std::vector<Team> teams = user_teams.load(db, ctx, grace);
|
||||
CHECK(teams.size() == 1);
|
||||
CHECK(std::get<std::string>(teams[0].get("name")) == "core");
|
||||
|
||||
// Pagination and ordering.
|
||||
{
|
||||
std::vector<Todo> page =
|
||||
Query<Todo>(db).order("title").limit(2).offset(1).all(ctx);
|
||||
CHECK(page.size() == 2);
|
||||
CHECK(std::get<std::string>(page[0].get("title")) == "g1");
|
||||
}
|
||||
|
||||
// Postgres SQL generation uses $n placeholders and PG types.
|
||||
{
|
||||
ScriptedPg pg;
|
||||
std::vector<User> none =
|
||||
Query<User>(pg).filter("name", make_text("x")).limit(5).all(ctx);
|
||||
CHECK(pg.seen.size() == 1);
|
||||
CHECK(pg.seen[0].find("WHERE name = $1") != std::string::npos);
|
||||
CHECK(pg.seen[0].find("LIMIT $2") != std::string::npos);
|
||||
User u;
|
||||
u.set("name", make_text("n"));
|
||||
u.set("email", make_text("e"));
|
||||
u.save(pg, ctx);
|
||||
CHECK(pg.seen.back().find("VALUES ($1, $2)") != std::string::npos);
|
||||
CHECK(create_table_sql(describe_model<User>(), Backend::Postgres).find(
|
||||
"BIGINT GENERATED ALWAYS AS IDENTITY") != std::string::npos);
|
||||
CHECK(create_table_sql(describe_model<User>(), Backend::Sqlite).find(
|
||||
"PRIMARY KEY") != std::string::npos);
|
||||
}
|
||||
|
||||
SUITE_DONE("orm");
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
// Step 2: Postgres wire client through a loopback fake server, plus
|
||||
// codecs, messages and SCRAM vectors.
|
||||
#include <arpa/inet.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "drivers/pg/auth.hpp"
|
||||
#include "drivers/pg/codecs.hpp"
|
||||
#include "drivers/pg/conn.hpp"
|
||||
#include "drivers/pg/extended.hpp"
|
||||
#include "drivers/pg/message.hpp"
|
||||
#include "drivers/pg/secret.hpp"
|
||||
#include "drivers/pg/sha256.hpp"
|
||||
#include "mini.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::pg;
|
||||
|
||||
static std::string hex_of(const std::array<std::uint8_t, 32>& h) {
|
||||
static const char* hex = "0123456789abcdef";
|
||||
std::string out;
|
||||
for (auto c : h) {
|
||||
out.push_back(hex[c >> 4]);
|
||||
out.push_back(hex[c & 15]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static std::vector<std::uint8_t> recv_exact_fd(int fd, std::size_t n) {
|
||||
std::vector<std::uint8_t> out(n);
|
||||
std::size_t got = 0;
|
||||
while (got < n) {
|
||||
const ssize_t r = recv(fd, out.data() + got, n - got, 0);
|
||||
if (r <= 0) throw std::runtime_error("fake server recv failed");
|
||||
got += static_cast<std::size_t>(r);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
static void send_all_fd(int fd, const std::vector<std::uint8_t>& b) {
|
||||
std::size_t sent = 0;
|
||||
while (sent < b.size()) {
|
||||
const ssize_t n = send(fd, b.data() + sent, b.size() - sent, 0);
|
||||
if (n <= 0) throw std::runtime_error("fake server send failed");
|
||||
sent += static_cast<std::size_t>(n);
|
||||
}
|
||||
}
|
||||
|
||||
static std::vector<std::uint8_t> row_desc_msg() {
|
||||
Writer b;
|
||||
b.put_i16(2);
|
||||
b.put_cstr("id");
|
||||
b.put_i32(0);
|
||||
b.put_i16(0);
|
||||
b.put_i32(oid::kInt4);
|
||||
b.put_i16(4);
|
||||
b.put_i32(-1);
|
||||
b.put_i16(0);
|
||||
b.put_cstr("name");
|
||||
b.put_i32(0);
|
||||
b.put_i16(0);
|
||||
b.put_i32(oid::kText);
|
||||
b.put_i16(-1);
|
||||
b.put_i32(-1);
|
||||
b.put_i16(0);
|
||||
return envelope('T', b.take());
|
||||
}
|
||||
|
||||
// Minimal fake Postgres: trust auth, canned SELECT 1 and extended rows.
|
||||
static void fake_server(int listen_fd, std::atomic<bool>& stop) {
|
||||
const int fd = accept(listen_fd, nullptr, nullptr);
|
||||
if (fd < 0) return;
|
||||
try {
|
||||
// Startup: length-prefixed, no type byte.
|
||||
const auto len_b = recv_exact_fd(fd, 4);
|
||||
const std::int32_t len = get_net_i32(len_b.data());
|
||||
(void)recv_exact_fd(fd, static_cast<std::size_t>(len - 4));
|
||||
Writer auth;
|
||||
auth.put_i32(0);
|
||||
send_all_fd(fd, envelope('R', auth.take()));
|
||||
Writer ps;
|
||||
ps.put_cstr("client_encoding");
|
||||
ps.put_cstr("UTF8");
|
||||
send_all_fd(fd, envelope('S', ps.take()));
|
||||
Writer kd;
|
||||
kd.put_i32(1234);
|
||||
kd.put_i32(5678);
|
||||
send_all_fd(fd, envelope('K', kd.take()));
|
||||
send_all_fd(fd, envelope('Z', {static_cast<std::uint8_t>('I')}));
|
||||
while (!stop) {
|
||||
const auto hdr = recv_exact_fd(fd, 5);
|
||||
const char type = static_cast<char>(hdr[0]);
|
||||
const std::int32_t mlen = get_net_i32(hdr.data() + 1);
|
||||
const auto body = recv_exact_fd(fd, static_cast<std::size_t>(mlen - 4));
|
||||
if (type == 'X' || type == 0) break;
|
||||
if (type == 'Q') {
|
||||
Reader r(body);
|
||||
const std::string sql = r.get_cstr();
|
||||
if (sql == "SELECT 1") {
|
||||
Writer d;
|
||||
d.put_i16(1);
|
||||
d.put_cstr("?column?");
|
||||
d.put_i32(0);
|
||||
d.put_i16(0);
|
||||
d.put_i32(oid::kInt4);
|
||||
d.put_i16(4);
|
||||
d.put_i32(-1);
|
||||
d.put_i16(0);
|
||||
send_all_fd(fd, envelope('T', d.take()));
|
||||
Writer row;
|
||||
row.put_i16(1);
|
||||
row.put_i32(1);
|
||||
row.put_u8('1');
|
||||
send_all_fd(fd, envelope('D', row.take()));
|
||||
Writer cc;
|
||||
cc.put_cstr("SELECT 1");
|
||||
send_all_fd(fd, envelope('C', cc.take()));
|
||||
} else {
|
||||
Writer cc;
|
||||
cc.put_cstr("SELECT 0");
|
||||
send_all_fd(fd, envelope('C', cc.take()));
|
||||
}
|
||||
send_all_fd(fd, envelope('Z', {static_cast<std::uint8_t>('I')}));
|
||||
} else if (type == 'P') {
|
||||
send_all_fd(fd, envelope('1', {}));
|
||||
} else if (type == 'B') {
|
||||
send_all_fd(fd, envelope('2', {}));
|
||||
} else if (type == 'D') {
|
||||
send_all_fd(fd, row_desc_msg());
|
||||
} else if (type == 'E') {
|
||||
Writer row;
|
||||
row.put_i16(2);
|
||||
row.put_i32(2);
|
||||
row.put_str("42");
|
||||
row.put_i32(3);
|
||||
row.put_str("ada");
|
||||
send_all_fd(fd, envelope('D', row.take()));
|
||||
Writer cc;
|
||||
cc.put_cstr("SELECT 1");
|
||||
send_all_fd(fd, envelope('C', cc.take()));
|
||||
} else if (type == 'S' || type == 'H') {
|
||||
send_all_fd(fd, envelope('Z', {static_cast<std::uint8_t>('I')}));
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// SHA-256 NIST vector for "abc".
|
||||
CHECK(hex_of(Sha256::hash("abc")) ==
|
||||
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
|
||||
// HMAC-SHA-256 RFC 4231 case 1.
|
||||
{
|
||||
const std::vector<std::uint8_t> key(20, 0x0b);
|
||||
CHECK(hex_of(hmac_sha256(key, "Hi There")) ==
|
||||
"b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7");
|
||||
}
|
||||
// Base64 roundtrip.
|
||||
{
|
||||
const std::vector<std::uint8_t> raw = {1, 2, 3, 250, 0, 17};
|
||||
const auto enc = base64_encode(raw);
|
||||
CHECK(base64_decode(enc) == raw);
|
||||
}
|
||||
// SCRAM-SHA-256 exchange vector, cross-checked byte for byte against
|
||||
// an independent Python hashlib implementation.
|
||||
{
|
||||
ScramSha256Client scram("user", "pencil", "rOprNGfwEbeRWgbNEkqO");
|
||||
CHECK(scram.client_first_message() ==
|
||||
"n,,n=user,r=rOprNGfwEbeRWgbNEkqO");
|
||||
const std::string final = scram.process_server_first(
|
||||
"r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,"
|
||||
"s=W22ZaJ0SNY7soEsUEjb6gQ==,i=4096");
|
||||
CHECK(final ==
|
||||
"c=biws,r=rOprNGfwEbeRWgbNEkqO%hvYDpWUa2RaTCAfuxFIlj)hNlF$k0,"
|
||||
"p=dHzbZapWIk4jUhN+Ute9ytag9zjfMHgsqmmiz7AndVQ=");
|
||||
scram.verify_server_final("v=6rriTRBi23WpRR/wtup+mMhUZUn/dB5nLTJRsjl95G4=");
|
||||
CHECK(scram.done());
|
||||
}
|
||||
// SASLprep-lite rejects controls.
|
||||
{
|
||||
bool threw = false;
|
||||
try {
|
||||
saslprep_lite(std::string("ab\x01"));
|
||||
} catch (const std::runtime_error&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// Message framing roundtrips.
|
||||
{
|
||||
const auto st = msg::startup("u", "d");
|
||||
Reader r(st);
|
||||
const std::int32_t len = r.get_i32();
|
||||
CHECK(len == static_cast<std::int32_t>(st.size()));
|
||||
CHECK(r.get_i32() == 196608);
|
||||
const auto q = msg::simple_query("SELECT 1");
|
||||
CHECK(q[0] == 'Q');
|
||||
const auto ef = msg::parse_error([] {
|
||||
Writer b;
|
||||
b.put_u8('S');
|
||||
b.put_cstr("ERROR");
|
||||
b.put_u8('C');
|
||||
b.put_cstr("42P01");
|
||||
b.put_u8('M');
|
||||
b.put_cstr("nope");
|
||||
b.put_u8(0);
|
||||
return b.take();
|
||||
}());
|
||||
CHECK(ef.code == "42P01" && ef.message == "nope");
|
||||
}
|
||||
|
||||
// Codecs roundtrip text and binary.
|
||||
{
|
||||
CHECK(encode_text(make_int(-5)) == "-5");
|
||||
CHECK(encode_text(make_bool(true)) == "t");
|
||||
CHECK(encode_text(make_bytes({0xab, 0x01})) == "\\xab01");
|
||||
const auto bin = encode_binary(make_int(1234567890123LL));
|
||||
CHECK(bin.size() == 8);
|
||||
Value v = decode_cell(bin, true, oid::kInt8, 1);
|
||||
CHECK(std::get<std::int64_t>(v) == 1234567890123LL);
|
||||
const std::vector<std::uint8_t> tb = {'4', '2'};
|
||||
Value t = decode_cell(tb, true, oid::kInt4, 0);
|
||||
CHECK(std::get<std::int64_t>(t) == 42);
|
||||
const std::vector<std::uint8_t> bb = {'\\', 'x', 'a', 'b'};
|
||||
Value bv = decode_cell(bb, true, oid::kBytea, 0);
|
||||
CHECK(std::get<Bytes>(bv) == Bytes{0xab});
|
||||
const auto bd = encode_binary(make_double(1.5));
|
||||
CHECK(std::get<double>(decode_cell(bd, true, oid::kFloat8, 1)) == 1.5);
|
||||
const auto now = std::chrono::system_clock::now();
|
||||
const auto bt = encode_binary(make_time(now));
|
||||
Value tv = decode_cell(bt, true, oid::kTimestampTz, 1);
|
||||
const auto back = std::get<TimePoint>(tv);
|
||||
const auto diff = std::chrono::duration_cast<std::chrono::microseconds>(
|
||||
now - back)
|
||||
.count();
|
||||
CHECK(diff >= 0 && diff < 1000000);
|
||||
}
|
||||
|
||||
// Extended script carries params out of band.
|
||||
{
|
||||
BoundStatement stmt("SELECT $1", {make_text("oops' OR '1'='1")});
|
||||
ExtendedScript script = build_extended(stmt, false, 7);
|
||||
CHECK(script.statement_name == "molo_s7");
|
||||
CHECK(!script.bytes.empty());
|
||||
// SQL text and value both present but framed separately: Parse holds
|
||||
// the SQL, Bind holds the value.
|
||||
const std::string blob(script.bytes.begin(), script.bytes.end());
|
||||
CHECK(blob.find("SELECT $1") != std::string::npos);
|
||||
CHECK(blob.find("oops' OR '1'='1") != std::string::npos);
|
||||
}
|
||||
|
||||
// Secrets and redaction.
|
||||
{
|
||||
Secret s = Secret::from_string("hunter2");
|
||||
CHECK(!s.empty() && s.view() == "hunter2");
|
||||
const std::string red =
|
||||
redact_conninfo("host=db password=hunter2 dbname=app");
|
||||
CHECK(red.find("hunter2") == std::string::npos);
|
||||
CHECK(red.find("***") != std::string::npos);
|
||||
}
|
||||
|
||||
// Live wire test against the loopback fake server.
|
||||
{
|
||||
const int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
CHECK(listen_fd >= 0);
|
||||
sockaddr_in addr{};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = 0;
|
||||
CHECK(bind(listen_fd, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) ==
|
||||
0);
|
||||
CHECK(listen(listen_fd, 1) == 0);
|
||||
socklen_t slen = sizeof(addr);
|
||||
CHECK(getsockname(listen_fd, reinterpret_cast<sockaddr*>(&addr), &slen) ==
|
||||
0);
|
||||
const int port = ntohs(addr.sin_port);
|
||||
std::atomic<bool> stop = false;
|
||||
std::thread server(fake_server, listen_fd, std::ref(stop));
|
||||
|
||||
Context ctx("pg-test", "", 10);
|
||||
PgConnection conn;
|
||||
PgConnectParams params;
|
||||
params.host = "127.0.0.1";
|
||||
params.port = port;
|
||||
params.user = "u";
|
||||
params.database = "d";
|
||||
conn.connect(ctx, std::move(params));
|
||||
CHECK(conn.backend() == Backend::Postgres);
|
||||
Result one = conn.simple(ctx, "SELECT 1");
|
||||
CHECK(one.size() == 1);
|
||||
CHECK(std::get<std::int64_t>(one.raw(0)[0]) == 1);
|
||||
Result ext =
|
||||
conn.exec(ctx, BoundStatement("SELECT $1", {make_int(5)}));
|
||||
CHECK(ext.size() == 1);
|
||||
CHECK(ext.columns()[0] == "id" && ext.columns()[1] == "name");
|
||||
CHECK(std::get<std::int64_t>(ext.raw(0)[0]) == 42);
|
||||
CHECK(std::get<std::string>(ext.raw(0)[1]) == "ada");
|
||||
conn.ping(ctx);
|
||||
conn.close();
|
||||
stop = true;
|
||||
close(listen_fd);
|
||||
server.join();
|
||||
}
|
||||
|
||||
SUITE_DONE("pg_wire");
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
// Step 4: pool and transaction behavior against the real SQLite path.
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "context.hpp"
|
||||
#include "drivers/sqlite/db.hpp"
|
||||
#include "drivers/sqlite/store.hpp"
|
||||
#include "mini.hpp"
|
||||
#include "pool.hpp"
|
||||
#include "pool_config.hpp"
|
||||
#include "transaction.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::sqlite;
|
||||
|
||||
static std::unique_ptr<SqliteDb> make_db(
|
||||
std::shared_ptr<MemoryStore> store) {
|
||||
auto db = std::make_unique<SqliteDb>(SqliteConfig{}, store);
|
||||
db->open(Context::background("pool"));
|
||||
return db;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Basic acquire, use, return, reuse.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
PoolConfig cfg = PoolConfig::sqlite();
|
||||
Pool<SqliteDb> pool(cfg, [&] { return make_db(store); });
|
||||
Context ctx = Context::background("test");
|
||||
{
|
||||
auto c = pool.acquire(ctx);
|
||||
c->exec(ctx, BoundStatement::raw("CREATE TABLE t (id INTEGER)"));
|
||||
CHECK(pool.total_count() == 1);
|
||||
}
|
||||
CHECK(pool.idle_count() == 1);
|
||||
{
|
||||
auto c = pool.acquire(ctx);
|
||||
c->exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(1)}));
|
||||
}
|
||||
CHECK(pool.total_count() == 1); // reused, not regrown
|
||||
}
|
||||
|
||||
// Exhaustion fails fast with structured error.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
PoolConfig cfg = PoolConfig::sqlite();
|
||||
cfg.max_size = 1;
|
||||
cfg.acquire_timeout_sec = 1;
|
||||
Pool<SqliteDb> pool(cfg, [&] { return make_db(store); });
|
||||
Context ctx = Context::background("test");
|
||||
auto held = pool.acquire(ctx);
|
||||
bool threw = false;
|
||||
try {
|
||||
auto extra = pool.acquire(ctx);
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "MOLO_POOL";
|
||||
}
|
||||
CHECK(threw);
|
||||
CHECK(pool.exhausted_count() >= 1);
|
||||
}
|
||||
|
||||
// Waiter cap overflow fails fast too.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
PoolConfig cfg = PoolConfig::sqlite();
|
||||
cfg.max_size = 1;
|
||||
cfg.max_waiters = 1;
|
||||
cfg.acquire_timeout_sec = 5;
|
||||
Pool<SqliteDb> pool(cfg, [&] { return make_db(store); });
|
||||
Context ctx = Context::background("test");
|
||||
auto held = pool.acquire(ctx);
|
||||
std::atomic<bool> waiter_failed = false;
|
||||
std::atomic<bool> waiter_started = false;
|
||||
std::thread waiter([&] {
|
||||
waiter_started = true;
|
||||
try {
|
||||
auto c = pool.acquire(Context::background("w"));
|
||||
} catch (const DbError& e) {
|
||||
if (e.code() == "MOLO_POOL") waiter_failed = true;
|
||||
}
|
||||
});
|
||||
while (!waiter_started) std::this_thread::yield();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
bool threw = false;
|
||||
try {
|
||||
auto extra = pool.acquire(ctx);
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "MOLO_POOL";
|
||||
}
|
||||
CHECK(threw);
|
||||
held = decltype(held)(); // release, waiter proceeds
|
||||
waiter.join();
|
||||
CHECK(!waiter_failed);
|
||||
}
|
||||
|
||||
// Concurrent checkouts across threads.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
PoolConfig cfg = PoolConfig::sqlite();
|
||||
cfg.max_size = 4;
|
||||
Pool<SqliteDb> pool(cfg, [&] { return make_db(store); });
|
||||
{
|
||||
auto c = pool.acquire(Context::background("test"));
|
||||
c->exec(Context::background("test"),
|
||||
BoundStatement::raw("CREATE TABLE t (id INTEGER)"));
|
||||
}
|
||||
std::vector<std::thread> threads;
|
||||
for (int w = 0; w < 4; ++w) {
|
||||
threads.emplace_back([&, w] {
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
auto c = pool.acquire(Context::background("w"));
|
||||
c->exec(Context::background("w"),
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)",
|
||||
{make_int(w * 100 + i)}));
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) t.join();
|
||||
auto c = pool.acquire(Context::background("test"));
|
||||
Result r =
|
||||
c->exec(Context::background("test"), BoundStatement("SELECT * FROM t"));
|
||||
CHECK(r.size() == 40);
|
||||
}
|
||||
|
||||
// Transactions: commit, rollback, savepoints, scope rollback.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db = [&] {
|
||||
SqliteDb d(SqliteConfig{}, store);
|
||||
d.open(Context::background("test"));
|
||||
return d;
|
||||
}();
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw("CREATE TABLE t (id INTEGER)"));
|
||||
|
||||
{
|
||||
Transaction txn(db, ctx);
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(1)}));
|
||||
txn.commit();
|
||||
CHECK(!txn.active());
|
||||
}
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT * FROM t")).size() == 1);
|
||||
|
||||
{
|
||||
Transaction txn(db, ctx);
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(2)}));
|
||||
txn.rollback();
|
||||
}
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT * FROM t")).size() == 1);
|
||||
|
||||
{
|
||||
Transaction txn(db, ctx);
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(3)}));
|
||||
const std::string sp = txn.savepoint();
|
||||
CHECK(sp == "molo_sp_1_1");
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(4)}));
|
||||
txn.rollback_to(sp);
|
||||
txn.release_savepoint(sp);
|
||||
txn.commit();
|
||||
}
|
||||
Result r = db.exec(ctx, BoundStatement("SELECT id FROM t ORDER BY id"));
|
||||
CHECK(r.size() == 2);
|
||||
CHECK(std::get<std::int64_t>(r.raw(1)[0]) == 3);
|
||||
|
||||
{
|
||||
Transaction txn(db, ctx);
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(9)}));
|
||||
} // scope exit rolls back
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT * FROM t")).size() == 2);
|
||||
|
||||
// Serializable opt-in accepted on the SQLite path too.
|
||||
{
|
||||
Transaction txn(db, ctx, Isolation::Serializable);
|
||||
txn.commit();
|
||||
}
|
||||
}
|
||||
|
||||
SUITE_DONE("pool_txn");
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
// Step 9: pubsub broker fan-out, ordering, drops, patterns.
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "mini.hpp"
|
||||
#include "pubsub/broker.hpp"
|
||||
#include "pubsub/channel.hpp"
|
||||
|
||||
using namespace molo::data::pubsub;
|
||||
|
||||
static Payload to_payload(int n) {
|
||||
const std::string s = std::to_string(n);
|
||||
return Payload(s.begin(), s.end());
|
||||
}
|
||||
|
||||
static int from_payload(const Payload& p) {
|
||||
return std::stoi(std::string(p.begin(), p.end()));
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Pattern matching rules.
|
||||
CHECK(pattern_match("orders:created", "orders:created"));
|
||||
CHECK(pattern_match("orders:created", "orders:*"));
|
||||
CHECK(pattern_match("orders:created", "*:created"));
|
||||
CHECK(pattern_match("a:b:c", "*:b:*"));
|
||||
CHECK(!pattern_match("orders:created", "orders:deleted"));
|
||||
CHECK(!pattern_match("a:b", "a:b:c"));
|
||||
CHECK(!pattern_match("a:b:c", "a:*"));
|
||||
|
||||
// 10k messages fan out to 100 subscribers in order.
|
||||
{
|
||||
Broker broker;
|
||||
const int kSubs = 100;
|
||||
const int kMsgs = 10000;
|
||||
std::vector<std::vector<int>> received(kSubs);
|
||||
std::vector<int> ids;
|
||||
for (int s = 0; s < kSubs; ++s)
|
||||
ids.push_back(broker.subscribe(
|
||||
"events:all", [&, s](const Message& m) {
|
||||
received[s].push_back(from_payload(m.payload));
|
||||
}));
|
||||
for (int i = 0; i < kMsgs; ++i) {
|
||||
DeliveryReport r = broker.publish("events:all", to_payload(i), "text");
|
||||
CHECK(!r.no_subscribers && r.callbacks == kSubs);
|
||||
}
|
||||
CHECK(broker.published() == kMsgs);
|
||||
for (int s = 0; s < kSubs; ++s) {
|
||||
CHECK(static_cast<int>(received[s].size()) == kMsgs);
|
||||
for (int i = 0; i < kMsgs; ++i)
|
||||
if (received[s][i] != i) {
|
||||
CHECK(false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int id : ids) broker.unsubscribe(id);
|
||||
DeliveryReport r = broker.publish("events:all", to_payload(0));
|
||||
CHECK(r.no_subscribers && broker.no_subscriber() == 1);
|
||||
}
|
||||
|
||||
// Slow queued subscriber drops oldest, counted; publisher never blocks.
|
||||
{
|
||||
Broker broker;
|
||||
auto sub = broker.subscribe_queued("slow:work");
|
||||
const int kMsgs = 3000;
|
||||
int dropped_reports = 0;
|
||||
for (int i = 0; i < kMsgs; ++i) {
|
||||
DeliveryReport r = broker.publish("slow:work", to_payload(i));
|
||||
dropped_reports += r.dropped;
|
||||
}
|
||||
CHECK(sub->depth() == 1024);
|
||||
CHECK(sub->dropped() == kMsgs - 1024);
|
||||
CHECK(dropped_reports == kMsgs - 1024);
|
||||
auto batch = sub->poll();
|
||||
CHECK(static_cast<int>(batch.size()) == 1024);
|
||||
// Oldest surviving message is kMsgs - 1024.
|
||||
CHECK(from_payload(batch.front().payload) == kMsgs - 1024);
|
||||
CHECK(from_payload(batch.back().payload) == kMsgs - 1);
|
||||
}
|
||||
|
||||
// Patterns route, exact names do not leak across topics.
|
||||
{
|
||||
Broker broker;
|
||||
std::atomic<int> wild = 0;
|
||||
std::atomic<int> exact = 0;
|
||||
broker.subscribe("orders:*",
|
||||
[&](const Message&) { ++wild; });
|
||||
broker.subscribe("orders:created",
|
||||
[&](const Message&) { ++exact; });
|
||||
broker.publish("orders:created", to_payload(1));
|
||||
broker.publish("orders:deleted", to_payload(2));
|
||||
broker.publish("users:created", to_payload(3));
|
||||
CHECK(wild == 2 && exact == 1);
|
||||
}
|
||||
|
||||
// Payload cap enforced, channel names validated.
|
||||
{
|
||||
Broker broker;
|
||||
bool threw = false;
|
||||
try {
|
||||
broker.publish("big:one", Payload(1024 * 1024 + 1, 0));
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
threw = false;
|
||||
try {
|
||||
broker.subscribe("bad name", [](const Message&) {});
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// Concurrent publishers do not lose inline deliveries.
|
||||
{
|
||||
Broker broker;
|
||||
std::atomic<int> total = 0;
|
||||
std::mutex mutex;
|
||||
broker.subscribe("race:x", [&](const Message&) {
|
||||
std::lock_guard<std::mutex> lock(mutex);
|
||||
++total;
|
||||
});
|
||||
std::vector<std::thread> threads;
|
||||
for (int t = 0; t < 8; ++t) {
|
||||
threads.emplace_back([&, t] {
|
||||
for (int i = 0; i < 500; ++i)
|
||||
broker.publish("race:x", to_payload(t * 1000 + i));
|
||||
});
|
||||
}
|
||||
for (auto& th : threads) th.join();
|
||||
CHECK(total == 4000);
|
||||
}
|
||||
|
||||
SUITE_DONE("pubsub");
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
// Step 3: SQLite handling through the real SQL layer: CRUD, constraints,
|
||||
// injection-proof binding, pragmas, busy retry, single-writer gate,
|
||||
// checkpoints, file persistence and path refusal.
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "drivers/sqlite/busy.hpp"
|
||||
#include "drivers/sqlite/db.hpp"
|
||||
#include "drivers/sqlite/store.hpp"
|
||||
#include "mini.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::sqlite;
|
||||
|
||||
static SqliteDb fresh_db(std::shared_ptr<MemoryStore> store = nullptr) {
|
||||
SqliteConfig cfg;
|
||||
if (!store) store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db(cfg, store);
|
||||
db.open(Context::background("test"));
|
||||
return db;
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Pragma sequence applied at open.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db = fresh_db(store);
|
||||
const auto& pragmas = store->applied_pragmas();
|
||||
CHECK(pragmas.size() == 4);
|
||||
CHECK(pragmas[0] == "journal_mode=WAL");
|
||||
CHECK(pragmas[1] == "synchronous=NORMAL");
|
||||
CHECK(pragmas[2] == "foreign_keys=ON");
|
||||
CHECK(pragmas[3].rfind("busy_timeout=", 0) == 0);
|
||||
CHECK(db.backend() == Backend::Sqlite);
|
||||
db.ping(Context::background("test"));
|
||||
}
|
||||
|
||||
// Network paths refused.
|
||||
{
|
||||
SqliteConfig cfg;
|
||||
cfg.path = "//remote/share/db.sqlite";
|
||||
SqliteDb db(cfg);
|
||||
bool threw = false;
|
||||
try {
|
||||
db.open(Context::background("test"));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "MOLO_PATH";
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// CRUD plus constraints.
|
||||
{
|
||||
SqliteDb db = fresh_db();
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw(
|
||||
"CREATE TABLE users (id INTEGER PRIMARY KEY, "
|
||||
"name VARCHAR(32) NOT NULL, age INTEGER)"));
|
||||
Result ins = db.exec(ctx, BoundStatement(
|
||||
"INSERT INTO users (name, age) VALUES (?, ?)",
|
||||
{make_text("ada"), make_int(36)}));
|
||||
CHECK(ins.affected() == 1 && ins.last_insert_id() == 1);
|
||||
Result sel = db.exec(
|
||||
ctx, BoundStatement("SELECT id, name, age FROM users WHERE id = ?",
|
||||
{make_int(1)}));
|
||||
CHECK(sel.size() == 1);
|
||||
CHECK(std::get<std::string>(sel.raw(0)[1]) == "ada");
|
||||
|
||||
// Injection string binds literally, never executes.
|
||||
db.exec(ctx, BoundStatement("INSERT INTO users (name) VALUES (?)",
|
||||
{make_text("x' OR '1'='1")}));
|
||||
Result all =
|
||||
db.exec(ctx, BoundStatement("SELECT name FROM users ORDER BY id"));
|
||||
CHECK(all.size() == 2);
|
||||
CHECK(std::get<std::string>(all.raw(1)[0]) == "x' OR '1'='1");
|
||||
|
||||
// Length enforcement is SQLITE_TOOBIG with SQLite shape.
|
||||
bool threw = false;
|
||||
try {
|
||||
db.exec(ctx, BoundStatement(
|
||||
"INSERT INTO users (name) VALUES (?)",
|
||||
{make_text("way-too-long-name-for-varchar-32!!")}));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "SQLITE_TOOBIG" && e.healable();
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// NOT NULL enforcement.
|
||||
threw = false;
|
||||
try {
|
||||
db.exec(ctx, BoundStatement("INSERT INTO users (age) VALUES (?)",
|
||||
{make_int(1)}));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "SQLITE_CONSTRAINT" && !e.healable();
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// Missing table/column shapes.
|
||||
threw = false;
|
||||
try {
|
||||
db.exec(ctx, BoundStatement("SELECT * FROM missing_tbl"));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "SQLITE_NO_TABLE" && e.healable() &&
|
||||
parse_missing_table(e.detail()) == "missing_tbl";
|
||||
}
|
||||
CHECK(threw);
|
||||
threw = false;
|
||||
try {
|
||||
db.exec(ctx, BoundStatement("SELECT nope FROM users"));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "SQLITE_NO_COLUMN" &&
|
||||
parse_missing_column(e.detail()) == "nope";
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// UPDATE and DELETE with affected counts.
|
||||
Result upd = db.exec(
|
||||
ctx, BoundStatement("UPDATE users SET age = ? WHERE id = ?",
|
||||
{make_int(37), make_int(1)}));
|
||||
CHECK(upd.affected() == 1);
|
||||
Result del = db.exec(
|
||||
ctx, BoundStatement("DELETE FROM users WHERE id = ?", {make_int(2)}));
|
||||
CHECK(del.affected() == 1);
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT * FROM users")).size() == 1);
|
||||
}
|
||||
|
||||
// JOIN, ORDER, LIMIT/OFFSET, COUNT.
|
||||
{
|
||||
SqliteDb db = fresh_db();
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw(
|
||||
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"));
|
||||
db.exec(ctx, BoundStatement::raw("CREATE TABLE todos (id INTEGER PRIMARY "
|
||||
"KEY, user_id INTEGER, title TEXT)"));
|
||||
for (int i = 1; i <= 3; ++i)
|
||||
db.exec(ctx, BoundStatement("INSERT INTO users (name) VALUES (?)",
|
||||
{make_text("u" + std::to_string(i))}));
|
||||
for (int i = 1; i <= 5; ++i)
|
||||
db.exec(ctx, BoundStatement(
|
||||
"INSERT INTO todos (user_id, title) VALUES (?, ?)",
|
||||
{make_int(1 + (i % 3)),
|
||||
make_text("t" + std::to_string(i))}));
|
||||
Result j = db.exec(ctx,
|
||||
BoundStatement("SELECT users.name, todos.title FROM "
|
||||
"users JOIN todos ON users.id = "
|
||||
"todos.user_id ORDER BY todos.title "
|
||||
"LIMIT 2 OFFSET 1"));
|
||||
CHECK(j.size() == 2);
|
||||
CHECK(std::get<std::string>(j.raw(0)[1]) == "t2");
|
||||
Result cnt =
|
||||
db.exec(ctx, BoundStatement("SELECT COUNT(*) FROM todos WHERE "
|
||||
"user_id = ?",
|
||||
{make_int(2)}));
|
||||
CHECK(std::get<std::int64_t>(cnt.raw(0)[0]) == 2);
|
||||
}
|
||||
|
||||
// Busy retry then loud failure.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db = fresh_db(store);
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw("CREATE TABLE t (id INTEGER)"));
|
||||
store->inject_busy(2);
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(1)}));
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT * FROM t")).size() == 1);
|
||||
store->inject_busy(100);
|
||||
bool threw = false;
|
||||
try {
|
||||
db.exec(ctx,
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)", {make_int(2)}));
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "SQLITE_BUSY";
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// Single-writer gate: concurrent writers serialize without loss.
|
||||
{
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db = fresh_db(store);
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw("CREATE TABLE t (id INTEGER)"));
|
||||
std::vector<std::thread> threads;
|
||||
for (int w = 0; w < 8; ++w) {
|
||||
threads.emplace_back([&, w] {
|
||||
for (int i = 0; i < 25; ++i)
|
||||
db.exec(Context::background("w"),
|
||||
BoundStatement("INSERT INTO t (id) VALUES (?)",
|
||||
{make_int(w * 100 + i)}));
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) t.join();
|
||||
CHECK(db.exec(ctx, BoundStatement("SELECT COUNT(*) FROM t")).size() == 1);
|
||||
Result c = db.exec(ctx, BoundStatement("SELECT COUNT(*) FROM t"));
|
||||
CHECK(std::get<std::int64_t>(c.raw(0)[0]) == 200);
|
||||
}
|
||||
|
||||
// Checkpoint scheduler and file persistence roundtrip.
|
||||
{
|
||||
const std::string path = "/tmp/molo_data_test.sqlite snap";
|
||||
std::remove(path.c_str());
|
||||
{
|
||||
SqliteConfig cfg;
|
||||
cfg.path = "/tmp/molo_data_test.sqlite";
|
||||
std::remove(cfg.path.c_str());
|
||||
SqliteDb db(cfg);
|
||||
db.open(Context::background("test"));
|
||||
Context ctx = Context::background("test");
|
||||
db.exec(ctx, BoundStatement::raw("CREATE TABLE k (id INTEGER PRIMARY "
|
||||
"KEY, v VARCHAR(16))"));
|
||||
for (int i = 0; i < 50; ++i)
|
||||
db.exec(ctx, BoundStatement("INSERT INTO k (v) VALUES (?)",
|
||||
{make_text("v" + std::to_string(i))}));
|
||||
db.checkpoint();
|
||||
CHECK(db.checkpoints_run() >= 1);
|
||||
CHECK(db.wal().total_pages() > 0);
|
||||
db.close();
|
||||
}
|
||||
{
|
||||
SqliteConfig cfg;
|
||||
cfg.path = "/tmp/molo_data_test.sqlite";
|
||||
SqliteDb db(cfg);
|
||||
db.open(Context::background("test"));
|
||||
Result c = db.exec(Context::background("test"),
|
||||
BoundStatement("SELECT COUNT(*) FROM k"));
|
||||
CHECK(std::get<std::int64_t>(c.raw(0)[0]) == 50);
|
||||
std::remove(cfg.path.c_str());
|
||||
}
|
||||
(void)path;
|
||||
}
|
||||
|
||||
SUITE_DONE("sqlite");
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
// Steps 6-7: sync plus migration journal plus healer matrix on both
|
||||
// backends (SQLite live, Postgres through a scripted connection).
|
||||
#include <filesystem>
|
||||
#include <vector>
|
||||
|
||||
#include "connection.hpp"
|
||||
#include "context.hpp"
|
||||
#include "drivers/sqlite/db.hpp"
|
||||
#include "drivers/sqlite/store.hpp"
|
||||
#include "mini.hpp"
|
||||
#include "orm/healer.hpp"
|
||||
#include "orm/migration.hpp"
|
||||
#include "orm/migrator.hpp"
|
||||
#include "orm/model.hpp"
|
||||
#include "orm/query.hpp"
|
||||
#include "orm/sync.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
using namespace molo::data::orm;
|
||||
using namespace molo::data::sqlite;
|
||||
|
||||
class Widget : public Model<Widget> {
|
||||
public:
|
||||
static std::string table_name() { return "widgets"; }
|
||||
static std::string primary_key() { return "id"; }
|
||||
static std::vector<FieldDef> fields() {
|
||||
auto id = FieldDef::primary_key();
|
||||
auto sku = FieldDef::varchar("sku", 8);
|
||||
auto qty = FieldDef::bigint("qty");
|
||||
auto note = FieldDef::text("note");
|
||||
return {id, sku, qty, note};
|
||||
}
|
||||
};
|
||||
|
||||
// Scripted connection failing on demand with Postgres codes.
|
||||
class ScriptedPg : public Connection {
|
||||
public:
|
||||
Backend backend() const override { return Backend::Postgres; }
|
||||
std::string backend_id() const override { return "pg:scripted"; }
|
||||
Result exec(const Context&, const BoundStatement& stmt) override {
|
||||
seen.push_back(stmt.sql());
|
||||
if (!script.empty()) {
|
||||
const bool fail = script.front();
|
||||
script.erase(script.begin());
|
||||
if (fail) throw DbError(Backend::Postgres, fail_code, fail_detail);
|
||||
return Result{};
|
||||
}
|
||||
if (fail_next) {
|
||||
fail_next = false;
|
||||
throw DbError(Backend::Postgres, fail_code, fail_detail);
|
||||
}
|
||||
if (always_fail)
|
||||
throw DbError(Backend::Postgres, fail_code, fail_detail);
|
||||
return Result{};
|
||||
}
|
||||
Prepared prepare(const Context&, const BoundStatement& stmt) override {
|
||||
return Prepared{"p", stmt.sql()};
|
||||
}
|
||||
Result exec_prepared(const Context&, const Prepared&,
|
||||
const std::vector<Value>&) override {
|
||||
return Result{};
|
||||
}
|
||||
void ping(const Context&) override {}
|
||||
void close() override {}
|
||||
|
||||
std::vector<std::string> seen;
|
||||
bool fail_next = false;
|
||||
bool always_fail = false;
|
||||
std::vector<bool> script; // per-call fail flags, consumed in order
|
||||
std::string fail_code;
|
||||
std::string fail_detail;
|
||||
};
|
||||
|
||||
static std::string fresh_dir(const std::string& name) {
|
||||
const std::string dir = "/tmp/molo_" + name;
|
||||
std::filesystem::remove_all(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto store = std::make_shared<MemoryStore>();
|
||||
SqliteDb db(SqliteConfig{}, store);
|
||||
db.open(Context::background("test"));
|
||||
Context ctx = Context::background("test");
|
||||
Syncer syncer;
|
||||
syncer.sync(ctx, db, {describe_model<Widget>()});
|
||||
|
||||
Widget w;
|
||||
w.set("sku", make_text("abc"));
|
||||
w.set("qty", make_int(5));
|
||||
w.set("note", make_text("first"));
|
||||
w.save(db, ctx);
|
||||
|
||||
// Dropped column heals: ADD COLUMN plus journal plus one retry.
|
||||
{
|
||||
store->debug_drop_column("widgets", "note");
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_col"));
|
||||
Result r = healer.execute_with_heal(
|
||||
db, ctx, BoundStatement("SELECT note FROM widgets"));
|
||||
CHECK(r.size() == 1);
|
||||
CHECK(store->table("widgets").cols.size() == 4);
|
||||
bool journaled = false;
|
||||
for (const auto& e :
|
||||
std::filesystem::directory_iterator("/tmp/molo_heal_col"))
|
||||
if (e.path().extension() == ".sql") journaled = true;
|
||||
CHECK(journaled);
|
||||
}
|
||||
|
||||
// Dropped table heals: CREATE TABLE plus retry.
|
||||
{
|
||||
store->debug_drop_table("widgets");
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_table"));
|
||||
Result r = healer.execute_with_heal(
|
||||
db, ctx, BoundStatement("SELECT * FROM widgets"));
|
||||
CHECK(r.empty());
|
||||
CHECK(store->has_table("widgets"));
|
||||
}
|
||||
|
||||
// Shortened VARCHAR heals via rebuild: length x4, rows preserved.
|
||||
{
|
||||
Widget w2;
|
||||
w2.set("sku", make_text("ok"));
|
||||
w2.set("qty", make_int(1));
|
||||
w2.set("note", make_text("n"));
|
||||
w2.save(db, ctx);
|
||||
store->debug_shorten_varchar("widgets", "sku", 4);
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_varchar"));
|
||||
BoundStatement stmt("INSERT INTO widgets (sku, qty) VALUES (?, ?)",
|
||||
{make_text("longer-than-4"), make_int(2)});
|
||||
Result r = healer.execute_with_heal(db, ctx, stmt);
|
||||
CHECK(r.affected() == 1);
|
||||
Result all = db.exec(ctx, BoundStatement("SELECT sku FROM widgets"));
|
||||
CHECK(all.size() == 2); // rebuild preserved the old row
|
||||
// New width is 16: a 12-char value now fits.
|
||||
db.exec(ctx, BoundStatement("INSERT INTO widgets (sku) VALUES (?)",
|
||||
{make_text("twelve-chars")}));
|
||||
}
|
||||
|
||||
// Narrowed int heals via rebuild to BIGINT.
|
||||
{
|
||||
store->debug_narrow_int("widgets", "qty");
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_int"));
|
||||
BoundStatement stmt("INSERT INTO widgets (sku, qty) VALUES (?, ?)",
|
||||
{make_text("s"), make_int(9999999999LL)});
|
||||
Result r = healer.execute_with_heal(db, ctx, stmt);
|
||||
CHECK(r.affected() == 1);
|
||||
}
|
||||
|
||||
// Postgres matrix through the scripted connection.
|
||||
{
|
||||
ScriptedPg pg;
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_pg"));
|
||||
pg.fail_next = true;
|
||||
pg.fail_code = "42P01";
|
||||
pg.fail_detail = "relation \"widgets\" does not exist";
|
||||
Result r = healer.execute_with_heal(
|
||||
pg, ctx, BoundStatement("SELECT * FROM widgets"));
|
||||
(void)r;
|
||||
CHECK(pg.seen.size() == 3); // failing stmt, CREATE TABLE, retry
|
||||
CHECK(pg.seen[1].find("CREATE TABLE") != std::string::npos);
|
||||
CHECK(pg.seen[2] == "SELECT * FROM widgets");
|
||||
// 42703 plans ADD COLUMN.
|
||||
DbError e(Backend::Postgres, "42703",
|
||||
"column \"note\" of relation \"widgets\" does not exist");
|
||||
HealPlan p = healer.plan(e, BoundStatement("SELECT note FROM widgets"));
|
||||
CHECK(p.valid && p.table == "widgets" && p.column == "note");
|
||||
CHECK(p.ddl[0].find("ADD COLUMN") != std::string::npos);
|
||||
// 22001 plans VARCHAR growth.
|
||||
DbError t(Backend::Postgres, "22001",
|
||||
"value too long for type character varying(8)");
|
||||
HealPlan pv = healer.plan(
|
||||
t, BoundStatement("INSERT INTO widgets (sku) VALUES ($1)",
|
||||
{make_text("way-too-long-sku-value")}));
|
||||
CHECK(pv.valid);
|
||||
CHECK(pv.ddl[0].find("VARCHAR(32)") != std::string::npos);
|
||||
// Fatal codes never plan.
|
||||
DbError f(Backend::Postgres, "23505", "duplicate key");
|
||||
CHECK(!healer.plan(f, BoundStatement("SELECT 1")).valid);
|
||||
}
|
||||
|
||||
// Retry failure propagates with the record attached.
|
||||
{
|
||||
ScriptedPg pg;
|
||||
pg.script = {true, false, true}; // stmt fails, DDL ok, retry fails
|
||||
pg.fail_code = "42P01";
|
||||
pg.fail_detail = "relation \"widgets\" does not exist";
|
||||
Healer healer({describe_model<Widget>()}, fresh_dir("heal_fail"));
|
||||
bool threw = false;
|
||||
try {
|
||||
healer.execute_with_heal(pg, ctx,
|
||||
BoundStatement("SELECT * FROM widgets"));
|
||||
} catch (const DbError& e) {
|
||||
threw = !e.migration_record().empty();
|
||||
}
|
||||
CHECK(threw);
|
||||
}
|
||||
|
||||
// Migrator: apply, list, checksum verify, pending.
|
||||
{
|
||||
Migrator migrator;
|
||||
Migration m1 =
|
||||
Migration::make("0001_widgets", "CREATE TABLE mig_a (id INTEGER);",
|
||||
"test migration");
|
||||
migrator.apply(ctx, db, m1);
|
||||
CHECK(store->has_table("mig_a"));
|
||||
CHECK(migrator.is_applied(ctx, db, "0001_widgets"));
|
||||
auto applied = migrator.list_applied(ctx, db);
|
||||
CHECK(!applied.empty());
|
||||
// Re-apply is idempotent when the checksum matches.
|
||||
migrator.apply(ctx, db, m1);
|
||||
// Tampered checksum aborts loudly.
|
||||
Migration tampered = m1;
|
||||
tampered.checksum = "deadbeefdeadbeef";
|
||||
bool threw = false;
|
||||
try {
|
||||
migrator.apply(ctx, db, tampered);
|
||||
} catch (const DbError& e) {
|
||||
threw = e.code() == "MOLO_MIGRATION";
|
||||
}
|
||||
CHECK(threw);
|
||||
Migration m2 = Migration::make("0002_more", "CREATE TABLE mig_b (id INTEGER);",
|
||||
"second");
|
||||
auto pend = migrator.pending(ctx, db, {m1, m2});
|
||||
CHECK(pend.size() == 1 && pend[0].id == "0002_more");
|
||||
// Journal file write works.
|
||||
const std::string dir = fresh_dir("journal");
|
||||
const std::string path = Migrator::write_auto_journal(dir, m2);
|
||||
CHECK(std::filesystem::exists(path));
|
||||
}
|
||||
|
||||
SUITE_DONE("sync_heal_migrate");
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
// Step 1: Value/Row/Result/Error plus classification table.
|
||||
#include "mini.hpp"
|
||||
#include "value.hpp"
|
||||
#include "row.hpp"
|
||||
#include "result.hpp"
|
||||
#include "error.hpp"
|
||||
#include "statement.hpp"
|
||||
#include "context.hpp"
|
||||
|
||||
using namespace molo::data;
|
||||
|
||||
int main() {
|
||||
// Value factories and names.
|
||||
CHECK(is_null(make_null()));
|
||||
CHECK(!is_null(make_int(1)));
|
||||
CHECK(std::string(value_type_name(make_bool(true))) == "bool");
|
||||
CHECK(std::string(value_type_name(make_text("x"))) == "text");
|
||||
|
||||
// Row access.
|
||||
Row row({"a", "b"}, {make_int(7), make_text("hi")});
|
||||
CHECK(row.size() == 2);
|
||||
CHECK(std::get<std::int64_t>(row.get("a")) == 7);
|
||||
CHECK(row.has("b") && !row.has("zzz"));
|
||||
bool threw = false;
|
||||
try {
|
||||
row.get("zzz");
|
||||
} catch (const std::out_of_range&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// Result shape.
|
||||
Result r({"x"}, {{make_int(1)}, {make_int(2)}}, 2, 9);
|
||||
CHECK(r.size() == 2 && r.affected() == 2 && r.last_insert_id() == 9);
|
||||
CHECK(std::get<std::int64_t>(r.row(1).get("x")) == 2);
|
||||
CHECK(Result{}.empty());
|
||||
|
||||
// Classification matrix: Postgres.
|
||||
CHECK(classify(Backend::Postgres, "42P01").kind == HealKind::CreateTable);
|
||||
CHECK(!classify(Backend::Postgres, "42P01").fatal);
|
||||
CHECK(classify(Backend::Postgres, "42703").kind == HealKind::AddColumn);
|
||||
CHECK(classify(Backend::Postgres, "22001").kind == HealKind::ExtendVarchar);
|
||||
CHECK(classify(Backend::Postgres, "42804").kind == HealKind::WidenInt);
|
||||
CHECK(classify(Backend::Postgres, "40001").retryable_txn);
|
||||
CHECK(classify(Backend::Postgres, "40001").fatal);
|
||||
CHECK(classify(Backend::Postgres, "23505").fatal);
|
||||
CHECK(classify(Backend::Postgres, "23505").kind == HealKind::None);
|
||||
|
||||
// Classification matrix: SQLite.
|
||||
CHECK(classify(Backend::Sqlite, "SQLITE_NO_TABLE").kind ==
|
||||
HealKind::CreateTable);
|
||||
CHECK(classify(Backend::Sqlite, "SQLITE_NO_COLUMN").kind ==
|
||||
HealKind::AddColumn);
|
||||
CHECK(classify(Backend::Sqlite, "SQLITE_TOOBIG").kind ==
|
||||
HealKind::ExtendVarchar);
|
||||
CHECK(classify(Backend::Sqlite, "SQLITE_MISMATCH").kind ==
|
||||
HealKind::WidenInt);
|
||||
CHECK(classify(Backend::Sqlite, "SQLITE_CONSTRAINT").fatal);
|
||||
|
||||
// DbError flags follow classification.
|
||||
DbError healable(Backend::Postgres, "42P01", "nope");
|
||||
CHECK(healable.healable() && !healable.retryable_txn());
|
||||
DbError fatal(Backend::Postgres, "0A000", "nope");
|
||||
CHECK(!fatal.healable());
|
||||
DbError ser(Backend::Postgres, "40001", "retry me");
|
||||
CHECK(ser.retryable_txn());
|
||||
|
||||
// Message parsers.
|
||||
CHECK(parse_missing_table("no such table: users") == "users");
|
||||
CHECK(parse_missing_table("other") == "");
|
||||
CHECK(parse_missing_column("no such column: age") == "age");
|
||||
|
||||
// Statements: param cap and raw flag.
|
||||
BoundStatement s("SELECT $1", {make_int(1)});
|
||||
CHECK(!s.has_raw() && s.params().size() == 1);
|
||||
CHECK(BoundStatement::raw("SELECT 1").has_raw());
|
||||
check_identifier("users.name_1");
|
||||
threw = false;
|
||||
try {
|
||||
check_identifier("a-b");
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
threw = false;
|
||||
try {
|
||||
check_identifier(std::string(64, 'x'));
|
||||
} catch (const std::invalid_argument&) {
|
||||
threw = true;
|
||||
}
|
||||
CHECK(threw);
|
||||
|
||||
// Context deadlines.
|
||||
Context ctx("corr-1", "tenant-a", 30);
|
||||
CHECK(ctx.correlation_id() == "corr-1");
|
||||
CHECK(ctx.tenant() == "tenant-a");
|
||||
CHECK(!ctx.expired() && ctx.remaining_ms() > 0);
|
||||
Context bg = Context::background();
|
||||
CHECK(!bg.expired());
|
||||
|
||||
SUITE_DONE("value_error");
|
||||
}
|
||||
Reference in New Issue
Block a user