summaryrefslogtreecommitdiffstats
path: root/test/manager_expire_unittest.cpp
blob: 21735b0cbc2d412fad56400362c9b1dd5805bd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
OpenPOWER on IntegriCloud