|
|
@@ -22,8 +22,12 @@ namespace {
|
|
|
/// collection -> document id -> body.
|
|
|
std::map<std::string, std::map<std::string, json>> g_db;
|
|
|
std::map<std::string, bool> g_created;
|
|
|
+/// Round trips, counted. `ensure()` is supposed to open a collection once per
|
|
|
+/// process; counting the call is the only way to assert that, because the
|
|
|
+/// outcome is identical either way.
|
|
|
+int g_getCollectionInfoCalls = 0;
|
|
|
|
|
|
-void resetDb() { g_db.clear(); g_created.clear(); }
|
|
|
+void resetDb() { g_db.clear(); g_created.clear(); g_getCollectionInfoCalls = 0; }
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
@@ -36,6 +40,7 @@ bool Client::createCollection(const std::string& name, uint32_t, bool, uint32_t,
|
|
|
}
|
|
|
|
|
|
std::optional<Client::CollectionInfo> Client::getCollectionInfo(const std::string& name) {
|
|
|
+ ++g_getCollectionInfoCalls;
|
|
|
if (!g_created.count(name)) return std::nullopt;
|
|
|
CollectionInfo info;
|
|
|
info.name = name;
|
|
|
@@ -209,6 +214,41 @@ TEST_F(FunctionRunLogTest, ProjectsDoNotShareARunLog) {
|
|
|
EXPECT_EQ(store.list("p1", "fn", 10)[0].runId, "a");
|
|
|
}
|
|
|
|
|
|
+// `append` calls `ensure`, so before the latch every recorded run paid a
|
|
|
+// getCollectionInfo in front of its upsert - on the path a caller is waiting
|
|
|
+// on, after the function's answer already exists.
|
|
|
+//
|
|
|
+// This counts the round trip rather than observing an outcome, because the
|
|
|
+// outcome is identical with and without the latch. The two EXPECTs below are a
|
|
|
+// pair and neither works alone: the count alone would also pass if `append`
|
|
|
+// had stopped touching the database at all, which is why the rows are read
|
|
|
+// back in the same test.
|
|
|
+TEST_F(FunctionRunLogTest, TheCollectionIsOpenedOncePerProjectHoweverManyRunsAreRecorded) {
|
|
|
+ svapi::FunctionRunLogStore store(fakeGateway(), 100);
|
|
|
+ for (int i = 0; i < 25; ++i) store.append("p1", entry("r" + std::to_string(i), "fn", static_cast<uint64_t>(i)));
|
|
|
+
|
|
|
+ EXPECT_EQ(g_getCollectionInfoCalls, 1) << "one open per project, not one per append";
|
|
|
+ // The pairing: the saving came from not re-asking, NOT from not writing.
|
|
|
+ EXPECT_EQ(store.list("p1", "fn", 100).size(), 25u);
|
|
|
+}
|
|
|
+
|
|
|
+// The latch is per PROJECT rather than a single flag, because a project created
|
|
|
+// after startup has no collection yet and its first recorded run is what
|
|
|
+// creates one. Collapsing it to one bool would make that project's log depend
|
|
|
+// on whether some OTHER project had been written to first.
|
|
|
+TEST_F(FunctionRunLogTest, ANewProjectIsStillOpenedAfterAnotherProjectWasLatched) {
|
|
|
+ svapi::FunctionRunLogStore store(fakeGateway(), 100);
|
|
|
+ store.append("p1", entry("a", "fn", 1));
|
|
|
+ ASSERT_EQ(g_getCollectionInfoCalls, 1);
|
|
|
+
|
|
|
+ store.append("p2", entry("b", "fn", 2));
|
|
|
+ EXPECT_EQ(g_getCollectionInfoCalls, 2) << "a second project must get its own open";
|
|
|
+ EXPECT_EQ(store.list("p2", "fn", 100).size(), 1u);
|
|
|
+ // And the first project is not re-opened by the second project's arrival.
|
|
|
+ store.append("p1", entry("c", "fn", 3));
|
|
|
+ EXPECT_EQ(g_getCollectionInfoCalls, 2);
|
|
|
+}
|
|
|
+
|
|
|
} // namespace
|
|
|
|
|
|
TEST(FunctionRunLogRecord, CarriesResponseHeadersSoAStoredRunRendersLikeAnEnvelope) {
|