blob: 21735b0cbc2d412fad56400362c9b1dd5805bd6a [file] [log] [blame]
Kun Yiaa17bda2019-11-14 13:12:42 -08001#include "blob_mock.hpp"
2#include "manager.hpp"
3
4#include <string>
5
6#include <gtest/gtest.h>
7
8namespace blobs
9{
10
11using namespace std::chrono_literals;
12
13using ::testing::_;
14using ::testing::Return;
15
16TEST(ManagerExpireTest, OpenWithLongTimeoutSucceeds)
17{
18 // With a long timeout, open should succeed without calling expire.
19 BlobManager mgr(2min);
20 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
21 auto m1ptr = m1.get();
22 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
23
24 uint16_t flags = OpenFlags::read, sess;
25 std::string path = "/asdf/asdf";
26
27 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillOnce(Return(true));
28 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillOnce(Return(true));
29 EXPECT_TRUE(mgr.open(flags, path, &sess));
30 // Do not expect the open session to expire
31 EXPECT_CALL(*m1ptr, expire(sess)).Times(0);
32}
33
34TEST(ManagerExpireTest, ZeroTimeoutWillCauseExpiration)
35{
36 // With timeout being zero, every open will cause all previous opened
37 // sessions to expire.
38 BlobManager mgr(0min);
39 std::unique_ptr<BlobMock> m1 = std::make_unique<BlobMock>();
40 auto m1ptr = m1.get();
41 EXPECT_TRUE(mgr.registerHandler(std::move(m1)));
42
43 uint16_t flags = OpenFlags::read, sess;
44 std::string path = "/asdf/asdf";
45 const int testIterations = 10;
46
47 EXPECT_CALL(*m1ptr, canHandleBlob(path)).WillRepeatedly(Return(true));
48 EXPECT_CALL(*m1ptr, open(_, flags, path)).WillRepeatedly(Return(true));
49 for (int i = 0; i < testIterations; ++i)
50 {
51 if (i != 0)
52 {
53 // Here 'sess' is the session ID obtained in previous loop
54 EXPECT_CALL(*m1ptr, expire(sess)).WillOnce(Return(true));
55 }
56 EXPECT_TRUE(mgr.open(flags, path, &sess));
57 }
58}
59} // namespace blobs