test_registry.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "db_test_util.hpp"
  2. #include "errors.hpp"
  3. #include "collection_registry.hpp"
  4. using namespace svapi;
  5. TEST(CollectionMeta, JsonRoundTrip) {
  6. CollectionMeta m{"docs", "vector", 1536, "text-embedding-3-small", 123};
  7. CollectionMeta r = CollectionMeta::fromJson(m.toJson());
  8. EXPECT_EQ(r.name, "docs"); EXPECT_EQ(r.kind, "vector");
  9. EXPECT_EQ(r.vectorDimension, 1536u); EXPECT_EQ(r.embeddingModel, "text-embedding-3-small");
  10. }
  11. TEST(CollectionRegistry, IsolatedPerProject) {
  12. SVAPI_REQUIRE_DB(db);
  13. const std::string pa = testutil::tmpName("rega"), pb = testutil::tmpName("regb");
  14. db->ensureProject(pa); db->ensureProject(pb);
  15. CollectionRegistry reg(*db);
  16. reg.ensure(pa); reg.ensure(pb);
  17. EXPECT_TRUE(reg.add(pa, {"users", "json", 0, "", 0}));
  18. EXPECT_TRUE(reg.get(pa, "users").has_value());
  19. EXPECT_FALSE(reg.get(pb, "users").has_value()); // isolation across projects
  20. EXPECT_EQ(reg.list(pa).size(), 1u);
  21. EXPECT_EQ(reg.list(pb).size(), 0u);
  22. EXPECT_TRUE(reg.remove(pa, "users"));
  23. EXPECT_FALSE(reg.get(pa, "users").has_value());
  24. db->dropProject(pa); db->dropProject(pb);
  25. }
  26. // Regression guard for issue #1: a freshly-constructed registry (as on a new
  27. // server process) must surface previously-stored entries for every project once
  28. // bootstrap() has re-opened them. The startup path warmed only the default
  29. // project before the fix, so named projects' collections went missing on restart.
  30. TEST(CollectionRegistry, BootstrapWarmsAllProjects) {
  31. SVAPI_REQUIRE_DB(db);
  32. const std::string pa = testutil::tmpName("bootA"), pb = testutil::tmpName("bootB");
  33. db->ensureProject(pa); db->ensureProject(pb);
  34. {
  35. CollectionRegistry seed(*db);
  36. seed.ensure(pa); seed.ensure(pb);
  37. EXPECT_TRUE(seed.add(pa, {"alpha", "json", 0, "", 0}));
  38. EXPECT_TRUE(seed.add(pb, {"beta", "vector", 8, "m", 0}));
  39. }
  40. CollectionRegistry fresh(*db);
  41. fresh.bootstrap({pa, pb}); // the startup warm-up that fixes issue #1
  42. EXPECT_EQ(fresh.list(pa).size(), 1u);
  43. EXPECT_EQ(fresh.list(pb).size(), 1u);
  44. EXPECT_TRUE(fresh.get(pa, "alpha").has_value());
  45. EXPECT_TRUE(fresh.get(pb, "beta").has_value());
  46. db->dropProject(pa); db->dropProject(pb);
  47. }
  48. // A transient database failure must not be reported as "no such collection".
  49. //
  50. // Client::get returns nullopt for BOTH "no such document" and a transport
  51. // failure (it only throws for PERMISSION_DENIED), so the registry cannot tell
  52. // them apart from the return value alone. Left unhandled that reaches a caller
  53. // as 404, which a careful client cannot retry - adding 404 to a retry set
  54. // would retry every genuine absence - so a consumer lost a 6-minute ingest of
  55. // an 8 GB mailbox to a blip on a collection that existed before and after.
  56. //
  57. // Driven against an UNREACHABLE gateway, which is the sustained form of the
  58. // same ambiguity: every get() returns nullopt because nothing answers.
  59. TEST(RegistryTransient, AnUnreachableDatabaseIs503NotAMissingCollection) {
  60. // Port 1 on loopback: nothing listens, so every call fails.
  61. svapi::DbGateway dead("127.0.0.1:1", 100);
  62. // connect() FIRST, and its failure is expected and ignored. The client
  63. // crashes rather than throwing on an RPC issued before connect() has built
  64. // its stub, so a test that skipped this would segfault and say nothing
  65. // about the registry. Production always connects (main() exits 1 if the
  66. // database is unreachable at startup), so connecting here is also the
  67. // faithful shape - what is being tested is a database that goes away
  68. // AFTER a successful start.
  69. (void)dead.connect();
  70. svapi::CollectionRegistry registry(dead);
  71. try {
  72. (void)registry.get("someproject", "somecollection");
  73. FAIL() << "an unreachable database reported a definite answer about existence";
  74. } catch (const svapi::ApiError& e) {
  75. EXPECT_EQ(svapi::httpStatus(e.code), 503)
  76. << "must be retryable, not a 404 the caller can never retry";
  77. EXPECT_EQ(e.slug, "db_error");
  78. }
  79. }
  80. // The other half: against a REACHABLE database, a genuinely absent collection
  81. // must still be reported as absent rather than as an outage. The fix must not
  82. // turn every 404 into a 503.
  83. TEST(RegistryTransient, AGenuinelyAbsentCollectionIsStillAbsent) {
  84. auto db = testutil::connectOrNull();
  85. if (!db) GTEST_SKIP() << "smartbotic-database not reachable";
  86. svapi::CollectionRegistry registry(*db);
  87. const std::string project = testutil::tmpName("transient");
  88. db->ensureProject(project);
  89. registry.ensure(project);
  90. EXPECT_FALSE(registry.get(project, "definitely-not-there").has_value());
  91. db->dropProject(project);
  92. }