test_timestamp_precision.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /**
  2. * Unit tests for per-collection timestamp precision (v1.6.0 Task 7).
  3. *
  4. * Exercises MemoryStore + CollectionConfigManager directly — no gRPC server.
  5. *
  6. * Test cases:
  7. * 1. ms is the default precision
  8. * 2. ns yields unique timestamps in a tight 1000-doc insert loop
  9. * 3. ms stamps fit in the ms range (< 10^15) over 1000 docs
  10. * 4. setConfig() is idempotent
  11. * 5. configFor() reflects setConfig() immediately (no reload required)
  12. * 6. Unknown collections return the default config
  13. */
  14. #include "../service/src/memory_store.hpp"
  15. #include "../service/src/config/collection_config_manager.hpp"
  16. #include "../service/src/document.hpp"
  17. #include <cassert>
  18. #include <cstdint>
  19. #include <iostream>
  20. #include <nlohmann/json.hpp>
  21. #include <set>
  22. #include <string>
  23. using smartbotic::database::CollectionCfg;
  24. using smartbotic::database::CollectionConfigManager;
  25. using smartbotic::database::CollectionOptions;
  26. using smartbotic::database::Document;
  27. using smartbotic::database::MemoryStore;
  28. namespace {
  29. // Anything below 10^15 fits comfortably in the ms-since-epoch range (≈ 2001..),
  30. // anything above is unambiguously ns-since-epoch in the current era.
  31. constexpr uint64_t NS_THRESHOLD = 1'000'000'000'000'000ULL; // 10^15
  32. MemoryStore::Config defaultConfig() {
  33. MemoryStore::Config cfg;
  34. cfg.nodeId = "test";
  35. cfg.maxMemoryBytes = 256ULL * 1024 * 1024;
  36. return cfg;
  37. }
  38. Document makeDoc(const std::string& id = "") {
  39. Document d;
  40. if (!id.empty()) d.id = id;
  41. d.set_data(nlohmann::json{{"value", "x"}});
  42. return d;
  43. }
  44. } // anonymous namespace
  45. void test_default_is_ms() {
  46. MemoryStore store(defaultConfig());
  47. store.start();
  48. CollectionConfigManager mgr(store);
  49. store.setConfigManager(&mgr);
  50. store.createCollection("docs_default", CollectionOptions{});
  51. auto id = store.insert("docs_default", makeDoc());
  52. auto doc = store.get("docs_default", id);
  53. assert(doc.has_value());
  54. assert(doc->createdAt > 0);
  55. assert(doc->createdAt < NS_THRESHOLD); // ms range
  56. assert(doc->updatedAt > 0);
  57. assert(doc->updatedAt < NS_THRESHOLD);
  58. store.stop();
  59. std::cout << "PASS: default precision is ms (createdAt=" << doc->createdAt << ")\n";
  60. }
  61. void test_ns_yields_unique_timestamps_in_tight_loop() {
  62. MemoryStore store(defaultConfig());
  63. store.start();
  64. CollectionConfigManager mgr(store);
  65. store.setConfigManager(&mgr);
  66. store.createCollection("docs_ns", CollectionOptions{});
  67. CollectionCfg ns_cfg;
  68. ns_cfg.timestampPrecision = "ns";
  69. std::string err;
  70. bool ok = mgr.setConfig("docs_ns", ns_cfg, err);
  71. assert(ok);
  72. std::set<uint64_t> seen;
  73. constexpr int N = 1000;
  74. for (int i = 0; i < N; ++i) {
  75. auto id = store.insert("docs_ns", makeDoc());
  76. auto doc = store.get("docs_ns", id);
  77. assert(doc.has_value());
  78. assert(doc->createdAt >= NS_THRESHOLD); // ns range
  79. auto [it, inserted] = seen.insert(doc->createdAt);
  80. assert(inserted); // every timestamp must be unique
  81. }
  82. assert(seen.size() == static_cast<size_t>(N));
  83. store.stop();
  84. std::cout << "PASS: ns precision produces " << N
  85. << " unique timestamps in tight loop\n";
  86. }
  87. void test_ms_baseline_fits_in_ms_range() {
  88. MemoryStore store(defaultConfig());
  89. store.start();
  90. CollectionConfigManager mgr(store);
  91. store.setConfigManager(&mgr);
  92. store.createCollection("docs_ms", CollectionOptions{});
  93. CollectionCfg ms_cfg;
  94. ms_cfg.timestampPrecision = "ms";
  95. std::string err;
  96. bool ok = mgr.setConfig("docs_ms", ms_cfg, err);
  97. assert(ok);
  98. constexpr int N = 1000;
  99. for (int i = 0; i < N; ++i) {
  100. auto id = store.insert("docs_ms", makeDoc());
  101. auto doc = store.get("docs_ms", id);
  102. assert(doc.has_value());
  103. assert(doc->createdAt < NS_THRESHOLD); // ms range
  104. }
  105. // We deliberately do NOT assert on tie-production here — it's machine-
  106. // dependent. The invariant that ms-stamped docs stay in the ms range is
  107. // what the default-preservation story needs.
  108. store.stop();
  109. std::cout << "PASS: ms precision stamps fit in ms range (" << N << " docs)\n";
  110. }
  111. void test_idempotent_configure() {
  112. MemoryStore store(defaultConfig());
  113. store.start();
  114. CollectionConfigManager mgr(store);
  115. store.setConfigManager(&mgr);
  116. CollectionCfg cfg;
  117. cfg.timestampPrecision = "ns";
  118. std::string err;
  119. assert(mgr.setConfig("docs_idem", cfg, err));
  120. assert(mgr.configFor("docs_idem").timestampPrecision == "ns");
  121. assert(mgr.hasExplicitConfig("docs_idem"));
  122. // Second call with the same value — should succeed and leave state intact.
  123. assert(mgr.setConfig("docs_idem", cfg, err));
  124. assert(mgr.configFor("docs_idem").timestampPrecision == "ns");
  125. assert(mgr.hasExplicitConfig("docs_idem"));
  126. store.stop();
  127. std::cout << "PASS: setConfig is idempotent\n";
  128. }
  129. void test_cache_reflects_configure_immediately() {
  130. MemoryStore store(defaultConfig());
  131. store.start();
  132. CollectionConfigManager mgr(store);
  133. store.setConfigManager(&mgr);
  134. // Before configure: default ns (v2.2), not explicit.
  135. assert(mgr.configFor("docs_cache").timestampPrecision == "ns");
  136. assert(!mgr.hasExplicitConfig("docs_cache"));
  137. CollectionCfg cfg;
  138. cfg.timestampPrecision = "ms";
  139. std::string err;
  140. assert(mgr.setConfig("docs_cache", cfg, err));
  141. // After configure: ms, immediately — no reloadFromStore() required.
  142. assert(mgr.configFor("docs_cache").timestampPrecision == "ms");
  143. assert(mgr.hasExplicitConfig("docs_cache"));
  144. store.stop();
  145. std::cout << "PASS: cache reflects setConfig immediately\n";
  146. }
  147. void test_default_for_unknown_collection() {
  148. MemoryStore store(defaultConfig());
  149. store.start();
  150. CollectionConfigManager mgr(store);
  151. store.setConfigManager(&mgr);
  152. auto cfg = mgr.configFor("never_configured_collection");
  153. // v2.2 — default flipped from "ms" to "ns" so rapid-write collections
  154. // get nanosecond stamps without an explicit configureCollection() call.
  155. assert(cfg.timestampPrecision == "ns");
  156. assert(!mgr.hasExplicitConfig("never_configured_collection"));
  157. store.stop();
  158. std::cout << "PASS: default config returned for unknown collection\n";
  159. }
  160. int main() {
  161. test_default_is_ms();
  162. test_ns_yields_unique_timestamps_in_tight_loop();
  163. test_ms_baseline_fits_in_ms_range();
  164. test_idempotent_configure();
  165. test_cache_reflects_configure_immediately();
  166. test_default_for_unknown_collection();
  167. std::cout << "\nAll timestamp precision tests PASSED!\n";
  168. return 0;
  169. }