summaryrefslogtreecommitdiff
path: root/src/mongo/db/session_test.cpp
diff options
context:
space:
mode:
authorNathan Louie <nathan.louie@10gen.com>2018-06-21 15:46:51 -0400
committerNathan Louie <nathan.louie@10gen.com>2018-06-25 17:21:30 -0400
commitfcda650b90f497c26ecfe121249f421adada8451 (patch)
tree89f4ad314fc5f94bf7b8211548e842f0baaf796f /src/mongo/db/session_test.cpp
parentaaec2087d9e70fa24293326bf5297808570dcc11 (diff)
downloadmongo-fcda650b90f497c26ecfe121249f421adada8451.tar.gz
unit tests
Diffstat (limited to 'src/mongo/db/session_test.cpp')
-rw-r--r--src/mongo/db/session_test.cpp136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/mongo/db/session_test.cpp b/src/mongo/db/session_test.cpp
index 3dd5053070d..e6ed6ca2ef1 100644
--- a/src/mongo/db/session_test.cpp
+++ b/src/mongo/db/session_test.cpp
@@ -1464,6 +1464,142 @@ TEST_F(SessionTest, TrackTotalOpenTransactionsWithCommit) {
ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), beforeTransactionStart);
}
+TEST_F(SessionTest, TrackTotalActiveAndInactiveTransactionsWithCommit) {
+ const auto sessionId = makeLogicalSessionIdForTest();
+ Session session(sessionId);
+ session.refreshFromStorageIfNeeded(opCtx());
+
+ const TxnNumber txnNum = 1;
+ opCtx()->setLogicalSessionId(sessionId);
+ opCtx()->setTxnNumber(txnNum);
+
+ unsigned long long firstActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long firstInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that the first unstash only increments the active counter only.
+ session.beginOrContinueTxn(opCtx(), txnNum, false, true, "testDB", "insert");
+ session.unstashTransactionResources(opCtx(), "insert");
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(), firstActiveCounter + 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(), firstInactiveCounter);
+
+ unsigned long long secondActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long secondInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that stashing the transaction resources decrements active counter and increments
+ // inactive counter.
+ { Lock::GlobalLock lk(opCtx(), MODE_IX, Date_t::now(), Lock::InterruptBehavior::kThrow); }
+ session.stashTransactionResources(opCtx());
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(),
+ secondActiveCounter - 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(),
+ secondInactiveCounter + 1U);
+
+ unsigned long long thirdActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long thirdInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that the second unstash increments the active counter and decrements the inactive
+ // counter.
+ session.unstashTransactionResources(opCtx(), "insert");
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(), thirdActiveCounter + 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(),
+ thirdInactiveCounter - 1U);
+
+ unsigned long long fourthActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long fourthInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that committing a transaction decrements the active counter only.
+ session.commitTransaction(opCtx());
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(),
+ fourthActiveCounter - 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(), fourthInactiveCounter);
+}
+
+TEST_F(SessionTest, TrackTotalActiveAndInactiveTransactionsWithStashedAbort) {
+ const auto sessionId = makeLogicalSessionIdForTest();
+ Session session(sessionId);
+ session.refreshFromStorageIfNeeded(opCtx());
+
+ const TxnNumber txnNum = 1;
+ opCtx()->setLogicalSessionId(sessionId);
+ opCtx()->setTxnNumber(txnNum);
+
+ unsigned long long firstActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long firstInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that the first unstash only increments the active counter only.
+ session.beginOrContinueTxn(opCtx(), txnNum, false, true, "testDB", "insert");
+ session.unstashTransactionResources(opCtx(), "insert");
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(), firstActiveCounter + 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(), firstInactiveCounter);
+
+ unsigned long long secondActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long secondInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that stashing the transaction resources decrements active counter and increments
+ // inactive counter.
+ { Lock::GlobalLock lk(opCtx(), MODE_IX, Date_t::now(), Lock::InterruptBehavior::kThrow); }
+ session.stashTransactionResources(opCtx());
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(),
+ secondActiveCounter - 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(),
+ secondInactiveCounter + 1U);
+
+ unsigned long long thirdActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long thirdInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that aborting a stashed transaction decrements the inactive counter only.
+ session.abortArbitraryTransaction();
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(), thirdActiveCounter - 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(),
+ thirdInactiveCounter);
+}
+
+TEST_F(SessionTest, TrackTotalActiveAndInactiveTransactionsWithUnstashedAbort) {
+ const auto sessionId = makeLogicalSessionIdForTest();
+ Session session(sessionId);
+ session.refreshFromStorageIfNeeded(opCtx());
+
+ const TxnNumber txnNum = 1;
+ opCtx()->setLogicalSessionId(sessionId);
+ opCtx()->setTxnNumber(txnNum);
+
+ unsigned long long firstActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long firstInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that the first unstash only increments the active counter only.
+ session.beginOrContinueTxn(opCtx(), txnNum, false, true, "testDB", "insert");
+ session.unstashTransactionResources(opCtx(), "insert");
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(), firstActiveCounter + 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(), firstInactiveCounter);
+
+ unsigned long long secondActiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentActive();
+ unsigned long long secondInactiveCounter =
+ ServerTransactionsMetrics::get(opCtx())->getCurrentInactive();
+
+ // Tests that aborting a stashed transaction decrements the active counter only.
+ session.abortArbitraryTransaction();
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentActive(),
+ secondActiveCounter - 1U);
+ ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentInactive(), secondInactiveCounter);
+}
+
/**
* Test fixture for transactions metrics.
*/