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; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 5 | using ::testing::UnorderedElementsAreArray; |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 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 | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 24 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenFailWhenNoBlob) |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 25 | { |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 26 | EXPECT_FALSE( |
| 27 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 28 | } |
| 29 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 30 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenFailWhenStoreOpenReturnsFailure) |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 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 | |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 43 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenSucceedWhenStoreOpenReturnsTrue) |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 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 | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 56 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenFailForNonMatchingBasePath) |
| 57 | { |
| 58 | addDefaultStore(openTestBaseId); |
| 59 | EXPECT_FALSE(handler.open(openTestSessionId, openTestROFlags, |
| 60 | openTestInvalidBlobId)); |
| 61 | } |
| 62 | |
| 63 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenCloseSucceedForValidBlobId) |
| 64 | { |
| 65 | addDefaultStore(openTestBaseId); |
| 66 | |
| 67 | EXPECT_FALSE(handler.close(openTestSessionId)); // Haven't open |
| 68 | EXPECT_TRUE( |
| 69 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 70 | EXPECT_TRUE(handler.close(openTestSessionId)); |
| 71 | EXPECT_FALSE(handler.close(openTestSessionId)); // Already closed |
| 72 | } |
| 73 | |
| 74 | TEST_F(BinaryStoreBlobHandlerOpenTest, OpenSuccessShowsBlobId) |
| 75 | { |
| 76 | addDefaultStore(openTestBaseId); |
| 77 | |
| 78 | EXPECT_TRUE( |
| 79 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 80 | EXPECT_THAT(handler.getBlobIds(), |
| 81 | UnorderedElementsAreArray({openTestBaseId, openTestBlobId})); |
| 82 | } |
| 83 | |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 84 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailForInvalidSession) |
| 85 | { |
| 86 | uint16_t invalidSessionId = 1; |
Kun Yi | 6baa713 | 2019-01-08 21:21:02 -0800 | [diff] [blame] | 87 | |
| 88 | addDefaultStore(openTestBaseId); |
| 89 | EXPECT_TRUE( |
| 90 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 91 | EXPECT_FALSE(handler.close(invalidSessionId)); |
| 92 | } |
| 93 | |
| 94 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseFailWhenStoreCloseFails) |
| 95 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 96 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 97 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 98 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 99 | .WillOnce(Return(true)); |
| 100 | EXPECT_CALL(*store, close()).WillOnce(Return(false)); |
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 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 103 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 104 | EXPECT_TRUE( |
| 105 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 106 | EXPECT_FALSE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TEST_F(BinaryStoreBlobHandlerOpenTest, CloseSucceedWhenStoreCloseSucceeds) |
| 110 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 111 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 112 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 113 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 114 | .WillOnce(Return(true)); |
| 115 | EXPECT_CALL(*store, close()).WillOnce(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 116 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 117 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 118 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 119 | EXPECT_TRUE( |
| 120 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 121 | EXPECT_TRUE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | TEST_F(BinaryStoreBlobHandlerOpenTest, ClosedSessionCannotBeReclosed) |
| 125 | { |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 126 | auto store = defaultMockStore(openTestBaseId); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 127 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 128 | EXPECT_CALL(*store, openOrCreateBlob(_, openTestROFlags)) |
| 129 | .WillOnce(Return(true)); |
| 130 | EXPECT_CALL(*store, close()).WillRepeatedly(Return(true)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 131 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 132 | handler.addNewBinaryStore(std::move(store)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 133 | |
Kun Yi | 64dc05c | 2018-12-19 13:19:03 -0800 | [diff] [blame] | 134 | EXPECT_TRUE( |
| 135 | handler.open(openTestSessionId, openTestROFlags, openTestBlobId)); |
| 136 | EXPECT_TRUE(handler.close(openTestSessionId)); |
| 137 | EXPECT_FALSE(handler.close(openTestSessionId)); |
| 138 | EXPECT_FALSE(handler.close(openTestSessionId)); |
Kun Yi | c0adbc3 | 2018-12-18 22:35:29 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Kun Yi | 38146a0 | 2018-12-18 21:54:26 -0800 | [diff] [blame] | 141 | } // namespace blobs |