test: Add unit tests for session expiration

Add simple tests to ensure the session expire method is called when
the timeout is set to zero.

Signed-off-by: Kun Yi <kunyi731@gmail.com>
Change-Id: Ic6e973ede760a6a6f15726d5b6c93a892d858c8b
diff --git a/test/Makefile.am b/test/Makefile.am
index ea45d7b..722b6b7 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -34,6 +34,7 @@
 	manager_unittest \
 	manager_getsession_unittest \
 	manager_open_unittest \
+	manager_expire_unittest \
 	manager_stat_unittest \
 	manager_sessionstat_unittest \
 	manager_commit_unittest \
@@ -95,6 +96,9 @@
 manager_open_unittest_SOURCES = manager_open_unittest.cpp
 manager_open_unittest_LDADD = $(top_builddir)/libblobcmds_common.la
 
+manager_expire_unittest_SOURCES = manager_expire_unittest.cpp
+manager_expire_unittest_LDADD = $(top_builddir)/libblobcmds_common.la
+
 manager_stat_unittest_SOURCES = manager_stat_unittest.cpp
 manager_stat_unittest_LDADD = $(top_builddir)/libblobcmds_common.la
 
diff --git a/test/manager_expire_unittest.cpp b/test/manager_expire_unittest.cpp
new file mode 100644
index 0000000..21735b0
--- /dev/null
+++ b/test/manager_expire_unittest.cpp
@@ -0,0 +1,59 @@
+#include "blob_mock.hpp"
+#include "manager.hpp"
+
+#include <string>
+
+#include <gtest/gtest.h>
+
+namespace blobs
+{
+
+using namespace std::chrono_literals;
+
+using ::testing::_;
+using ::testing::Return;
+
+TEST(ManagerExpireTest, OpenWithLongTimeoutSucceeds)
+{
+    // With a long timeout, open should succeed without calling expire.
+    BlobManager mgr(2min);
+    std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
+    auto m1ptr = m1.get();
+    EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
+
+    uint16_t flags = OpenFlags::read, sess;
+    std::string path = "/asdf/asdf";
+
+    EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
+    EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
+    EXPECT_TRUE(mgr.open(flags, path, &sess));
+    // Do not expect the open session to expire
+    EXPECT_CALL(*m1ptr, expire(sess)).Times(0);
+}
+
+TEST(ManagerExpireTest, ZeroTimeoutWillCauseExpiration)
+{
+    // With timeout being zero, every open will cause all previous opened
+    // sessions to expire.
+    BlobManager mgr(0min);
+    std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
+    auto m1ptr = m1.get();
+    EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
+
+    uint16_t flags = OpenFlags::read, sess;
+    std::string path = "/asdf/asdf";
+    const int testIterations = 10;
+
+    EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
+    EXPECT_CALL(*m1ptr, open(_, flags, path)).WillRepeatedly(Return(true));
+    for (int i = 0; i < testIterations; ++i)
+    {
+        if (i != 0)
+        {
+            // Here 'sess' is the session ID obtained in previous loop
+            EXPECT_CALL(*m1ptr, expire(sess)).WillOnce(Return(true));
+        }
+        EXPECT_TRUE(mgr.open(flags, path, &sess));
+    }
+}
+} // namespace blobs