#include #include "config.hpp" using svapi::Config; TEST(Config, DefaultsWhenEmpty) { Config c = Config::fromJson(nlohmann::json::object()); EXPECT_EQ(c.logLevel, "info"); EXPECT_EQ(c.httpBind, "0.0.0.0"); EXPECT_EQ(c.httpPort, 8080); EXPECT_EQ(c.dbAddress, "localhost:9004"); // The per-call gRPC deadline. 5000 is the value DbGateway hardcoded as a // default argument before this was configurable, and the default is pinned // here BECAUSE it must not drift: moving it silently changes whether a // slow database call reaches a caller as a 503 or as a late 200, which is // a decision for whoever sets it rather than for a release. EXPECT_EQ(c.dbTimeoutMs, 5000u); // 0 means "cpp-httplib's own default", NOT "no threads". Writing 8 here // would freeze today's httplib default into our config, so a future // httplib that sized its pool better would be silently overridden by a // stale copy of the old behaviour. EXPECT_EQ(c.httpThreadPoolSize, 0u); } TEST(Config, ParsesProvidedValues) { auto j = nlohmann::json::parse(R"({ "log_level":"debug", "http":{"bind_address":"127.0.0.1","port":9090,"thread_pool_size":2}, "database":{"address":"db:9004","timeout_ms":20000} })"); Config c = Config::fromJson(j); EXPECT_EQ(c.logLevel, "debug"); EXPECT_EQ(c.httpBind, "127.0.0.1"); EXPECT_EQ(c.httpPort, 9090); EXPECT_EQ(c.dbAddress, "db:9004"); EXPECT_EQ(c.dbTimeoutMs, 20000u); EXPECT_EQ(c.httpThreadPoolSize, 2u); } // The pairing that makes the test above mean something: a `database` block // that names only the address must leave the deadline alone. Without this, a // parser that overwrote the timeout with a zero whenever the section was // present would still pass - and a zero is "no timeout" to the client, which // is the unbounded call the deadline exists to prevent. TEST(Config, ADatabaseBlockWithoutATimeoutKeepsTheDefault) { auto j = nlohmann::json::parse(R"({"database":{"address":"other:9004"}})"); Config c = Config::fromJson(j); EXPECT_EQ(c.dbAddress, "other:9004"); EXPECT_EQ(c.dbTimeoutMs, 5000u); } TEST(Config, IgnoresUnknownKeys) { auto j = nlohmann::json::parse(R"({"nonsense":true,"http":{"port":1234}})"); Config c = Config::fromJson(j); EXPECT_EQ(c.httpPort, 1234); EXPECT_EQ(c.httpBind, "0.0.0.0"); }