Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 1 | #include "handler_unittest.hpp" |
| 2 | |
Kun Yi | d297c9f | 2019-01-09 13:52:30 -0800 | [diff] [blame] | 3 | using namespace std::string_literals; |
| 4 | |
| 5 | namespace blobs |
| 6 | { |
| 7 | |
| 8 | class BinaryStoreBlobHandlerCommitTest : public BinaryStoreBlobHandlerTest |
| 9 | { |
| 10 | protected: |
| 11 | BinaryStoreBlobHandlerCommitTest() |
| 12 | { |
| 13 | addDefaultStore(commitTestBaseId); |
| 14 | } |
| 15 | |
| 16 | static inline std::string commitTestBaseId = "/test/"s; |
| 17 | static inline std::string commitTestBlobId = "/test/blob0"s; |
| 18 | static inline std::vector<uint8_t> commitTestData = {0, 1, 2, 3, 4, |
| 19 | 5, 6, 7, 8, 9}; |
| 20 | static inline std::vector<uint8_t> commitTestDataToOverwrite = {10, 11, 12, |
| 21 | 13, 14}; |
| 22 | static inline std::vector<uint8_t> commitMetaUnused; |
| 23 | |
| 24 | static inline uint16_t commitTestSessionId = 0; |
| 25 | static inline uint16_t commitTestNewSessionId = 1; |
| 26 | static inline uint32_t commitTestOffset = 0; |
| 27 | |
| 28 | void openAndWriteTestData() |
| 29 | { |
| 30 | uint16_t flags = OpenFlags::read | OpenFlags::write; |
| 31 | EXPECT_TRUE(handler.open(commitTestSessionId, flags, commitTestBlobId)); |
| 32 | EXPECT_TRUE(handler.write(commitTestSessionId, commitTestOffset, |
| 33 | commitTestData)); |
| 34 | } |
| 35 | |
| 36 | void openWriteThenCommitData() |
| 37 | { |
| 38 | openAndWriteTestData(); |
| 39 | EXPECT_TRUE(handler.commit(commitTestSessionId, commitMetaUnused)); |
| 40 | } |
| 41 | }; |
| 42 | |
| 43 | TEST_F(BinaryStoreBlobHandlerCommitTest, CommittedDataIsReadable) |
| 44 | { |
| 45 | openWriteThenCommitData(); |
| 46 | |
| 47 | EXPECT_EQ(commitTestData, |
| 48 | handler.read(commitTestSessionId, commitTestOffset, |
| 49 | commitTestData.size())); |
| 50 | } |
| 51 | |
| 52 | TEST_F(BinaryStoreBlobHandlerCommitTest, CommittedDataCanBeReopened) |
| 53 | { |
| 54 | openWriteThenCommitData(); |
| 55 | |
| 56 | EXPECT_TRUE(handler.close(commitTestSessionId)); |
| 57 | EXPECT_TRUE(handler.open(commitTestNewSessionId, OpenFlags::read, |
| 58 | commitTestBlobId)); |
| 59 | EXPECT_EQ(commitTestData, |
| 60 | handler.read(commitTestNewSessionId, commitTestOffset, |
| 61 | commitTestData.size())); |
| 62 | } |
| 63 | |
| 64 | TEST_F(BinaryStoreBlobHandlerCommitTest, OverwrittenDataCanBeCommitted) |
| 65 | { |
| 66 | openWriteThenCommitData(); |
| 67 | |
| 68 | EXPECT_TRUE(handler.write(commitTestSessionId, commitTestOffset, |
| 69 | commitTestDataToOverwrite)); |
| 70 | EXPECT_TRUE(handler.commit(commitTestSessionId, commitMetaUnused)); |
| 71 | EXPECT_TRUE(handler.close(commitTestSessionId)); |
| 72 | |
| 73 | EXPECT_TRUE(handler.open(commitTestNewSessionId, OpenFlags::read, |
| 74 | commitTestBlobId)); |
| 75 | EXPECT_EQ(commitTestDataToOverwrite, |
| 76 | handler.read(commitTestNewSessionId, commitTestOffset, |
| 77 | commitTestDataToOverwrite.size())); |
| 78 | } |
| 79 | |
| 80 | TEST_F(BinaryStoreBlobHandlerCommitTest, UncommittedDataIsLostUponClose) |
| 81 | { |
| 82 | openWriteThenCommitData(); |
| 83 | |
| 84 | EXPECT_TRUE(handler.write(commitTestSessionId, commitTestOffset, |
| 85 | commitTestDataToOverwrite)); |
| 86 | EXPECT_TRUE(handler.close(commitTestSessionId)); |
| 87 | |
| 88 | EXPECT_TRUE(handler.open(commitTestNewSessionId, OpenFlags::read, |
| 89 | commitTestBlobId)); |
| 90 | EXPECT_EQ(commitTestData, |
| 91 | handler.read(commitTestNewSessionId, commitTestOffset, |
| 92 | commitTestData.size())); |
| 93 | } |
| 94 | |
| 95 | } // namespace blobs |