test_config.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. }
  17. TEST(Config, ParsesProvidedValues) {
  18. auto j = nlohmann::json::parse(R"({
  19. "log_level":"debug",
  20. "http":{"bind_address":"127.0.0.1","port":9090},
  21. "database":{"address":"db:9004","timeout_ms":20000}
  22. })");
  23. Config c = Config::fromJson(j);
  24. EXPECT_EQ(c.logLevel, "debug");
  25. EXPECT_EQ(c.httpBind, "127.0.0.1");
  26. EXPECT_EQ(c.httpPort, 9090);
  27. EXPECT_EQ(c.dbAddress, "db:9004");
  28. EXPECT_EQ(c.dbTimeoutMs, 20000u);
  29. }
  30. // The pairing that makes the test above mean something: a `database` block
  31. // that names only the address must leave the deadline alone. Without this, a
  32. // parser that overwrote the timeout with a zero whenever the section was
  33. // present would still pass - and a zero is "no timeout" to the client, which
  34. // is the unbounded call the deadline exists to prevent.
  35. TEST(Config, ADatabaseBlockWithoutATimeoutKeepsTheDefault) {
  36. auto j = nlohmann::json::parse(R"({"database":{"address":"other:9004"}})");
  37. Config c = Config::fromJson(j);
  38. EXPECT_EQ(c.dbAddress, "other:9004");
  39. EXPECT_EQ(c.dbTimeoutMs, 5000u);
  40. }
  41. TEST(Config, IgnoresUnknownKeys) {
  42. auto j = nlohmann::json::parse(R"({"nonsense":true,"http":{"port":1234}})");
  43. Config c = Config::fromJson(j);
  44. EXPECT_EQ(c.httpPort, 1234);
  45. EXPECT_EQ(c.httpBind, "0.0.0.0");
  46. }