test_config.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <gtest/gtest.h>
  2. #include "config.hpp"
  3. using svapi::Config;
  4. TEST(Config, DefaultsWhenEmpty) {
  5. Config c = Config::fromJson(nlohmann::json::object());
  6. EXPECT_EQ(c.logLevel, "info");
  7. EXPECT_EQ(c.httpBind, "0.0.0.0");
  8. EXPECT_EQ(c.httpPort, 8080);
  9. EXPECT_EQ(c.dbAddress, "localhost:9004");
  10. // The per-call gRPC deadline. 5000 is the value DbGateway hardcoded as a
  11. // default argument before this was configurable, and the default is pinned
  12. // here BECAUSE it must not drift: moving it silently changes whether a
  13. // slow database call reaches a caller as a 503 or as a late 200, which is
  14. // a decision for whoever sets it rather than for a release.
  15. EXPECT_EQ(c.dbTimeoutMs, 5000u);
  16. // 0 means "cpp-httplib's own default", NOT "no threads". Writing 8 here
  17. // would freeze today's httplib default into our config, so a future
  18. // httplib that sized its pool better would be silently overridden by a
  19. // stale copy of the old behaviour.
  20. EXPECT_EQ(c.httpThreadPoolSize, 0u);
  21. }
  22. TEST(Config, ParsesProvidedValues) {
  23. auto j = nlohmann::json::parse(R"({
  24. "log_level":"debug",
  25. "http":{"bind_address":"127.0.0.1","port":9090,"thread_pool_size":2},
  26. "database":{"address":"db:9004","timeout_ms":20000}
  27. })");
  28. Config c = Config::fromJson(j);
  29. EXPECT_EQ(c.logLevel, "debug");
  30. EXPECT_EQ(c.httpBind, "127.0.0.1");
  31. EXPECT_EQ(c.httpPort, 9090);
  32. EXPECT_EQ(c.dbAddress, "db:9004");
  33. EXPECT_EQ(c.dbTimeoutMs, 20000u);
  34. EXPECT_EQ(c.httpThreadPoolSize, 2u);
  35. }
  36. // The pairing that makes the test above mean something: a `database` block
  37. // that names only the address must leave the deadline alone. Without this, a
  38. // parser that overwrote the timeout with a zero whenever the section was
  39. // present would still pass - and a zero is "no timeout" to the client, which
  40. // is the unbounded call the deadline exists to prevent.
  41. TEST(Config, ADatabaseBlockWithoutATimeoutKeepsTheDefault) {
  42. auto j = nlohmann::json::parse(R"({"database":{"address":"other:9004"}})");
  43. Config c = Config::fromJson(j);
  44. EXPECT_EQ(c.dbAddress, "other:9004");
  45. EXPECT_EQ(c.dbTimeoutMs, 5000u);
  46. }
  47. TEST(Config, IgnoresUnknownKeys) {
  48. auto j = nlohmann::json::parse(R"({"nonsense":true,"http":{"port":1234}})");
  49. Config c = Config::fromJson(j);
  50. EXPECT_EQ(c.httpPort, 1234);
  51. EXPECT_EQ(c.httpBind, "0.0.0.0");
  52. }