Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" |
| 2 | |
| 3 | using ::testing::_; |
| 4 | using ::testing::Return; |
| 5 | using ::testing::StartsWith; |
| 6 | |
| 7 | using namespace std::string_literals; |
| 8 | using namespace binstore; |
| 9 | |
| 10 | namespace blobs |
| 11 | { |
| 12 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 13 | class BinaryStoreBlobHandlerOpenTest : public BinaryStoreBlobHandlerTest |
| 14 | { |
| 15 | protected: |
| 16 | static inline std::string openTestBaseId = "/test/"s; |
| 17 | static inline std::string openTestBlobId = "/test/blob0"s; |
| 18 | static inline std::string openTestInvalidBlobId = "/invalid/blob0"s; |
| 19 | static inline uint16_t openTestROFlags = OpenFlags::read; |
| 20 | static inline uint16_t openTestRWFlags = OpenFlags::read | OpenFlags::write; |
| 21 | static inline uint16_t openTestSessionId = 0; |
| 22 | }; |
| 23 | |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 24 | TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenCannotHandleId) |
| 25 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 26 | EXPECT_FALSE(handler.open(openTestSessionId, openTestROFlags, |
| 27 | openTestInvalidBlobId)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | TEST_F(BinaryStoreBlobHandlerOpenTest, FailWhenStoreOpenReturnsFailure) |
| 31 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 32 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 33 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 34 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 35 | .WillOnce(Return(false)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 36 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 37 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 38 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 39 | EXPECT_FALSE( |
| 40 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | TEST_F(BinaryStoreBlobHandlerOpenTest, SucceedWhenStoreOpenReturnsTrue) |
| 44 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 45 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 46 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 47 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 48 | .WillOnce(Return(true)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 49 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 50 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 51 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 52 | EXPECT_TRUE( |
| 53 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 56 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailForInvalidSession) |
| 57 | { |
| 58 | uint16_t invalidSessionId = 1; |
| 59 | EXPECT_FALSE(handler.close(invalidSessionId)); |
| 60 | } |
| 61 | |
| 62 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailWhenStoreCloseFails) |
| 63 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 64 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 65 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 66 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 67 | .WillOnce(Return(true)); |
| 68 | EXPECT_CALL(*store, close()).WillOnce(Return(false)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 69 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 70 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 71 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 72 | EXPECT_TRUE( |
| 73 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 74 | EXPECT_FALSE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseSucceedWhenStoreCloseSucceeds) |
| 78 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 79 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 80 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 81 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 82 | .WillOnce(Return(true)); |
| 83 | EXPECT_CALL(*store, close()).WillOnce(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 84 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 85 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 86 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 87 | EXPECT_TRUE( |
| 88 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 89 | EXPECT_TRUE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | TEST_F(BinaryStoreBlobHandlerOpenTest, ClosedSessionCannotBeReclosed) |
| 93 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 94 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 95 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 96 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 97 | .WillOnce(Return(true)); |
| 98 | EXPECT_CALL(*store, close()).WillRepeatedly(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 99 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 100 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 101 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 102 | EXPECT_TRUE( |
| 103 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 104 | EXPECT_TRUE(handler.close(openTestSessionId)); |
| 105 | EXPECT_FALSE(handler.close(openTestSessionId)); |
| 106 | EXPECT_FALSE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 109 | } // namespace blobs |