|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package fsblkstorage |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/golang/protobuf/proto" |
| 23 | + "github.com/hyperledger/fabric/core/ledgernext/testutil" |
| 24 | +) |
| 25 | + |
| 26 | +func TestBlockfileStream(t *testing.T) { |
| 27 | + testBlockfileStream(t, 0) |
| 28 | + testBlockfileStream(t, 1) |
| 29 | + testBlockfileStream(t, 10) |
| 30 | +} |
| 31 | + |
| 32 | +func testBlockfileStream(t *testing.T, numBlocks int) { |
| 33 | + env := newTestEnv(t) |
| 34 | + defer env.Cleanup() |
| 35 | + w := newTestBlockfileWrapper(t, env) |
| 36 | + blockfileMgr := w.blockfileMgr |
| 37 | + |
| 38 | + blocks := testutil.ConstructTestBlocks(t, numBlocks) |
| 39 | + w.addBlocks(blocks) |
| 40 | + w.close() |
| 41 | + |
| 42 | + s, err := newBlockfileStream(deriveBlockfilePath(blockfileMgr.rootDir, 0), 0) |
| 43 | + defer s.close() |
| 44 | + testutil.AssertNoError(t, err, "Error in constructing blockfile stream") |
| 45 | + |
| 46 | + blockCount := 0 |
| 47 | + for { |
| 48 | + blockBytes, err := s.nextBlockBytes() |
| 49 | + testutil.AssertNoError(t, err, "Error in getting next block") |
| 50 | + if blockBytes == nil { |
| 51 | + break |
| 52 | + } |
| 53 | + blockCount++ |
| 54 | + } |
| 55 | + // After the stream has been exhausted, both blockBytes and err should be nil |
| 56 | + blockBytes, err := s.nextBlockBytes() |
| 57 | + testutil.AssertNil(t, blockBytes) |
| 58 | + testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file") |
| 59 | + testutil.AssertEquals(t, blockCount, numBlocks) |
| 60 | +} |
| 61 | + |
| 62 | +func TestBlockFileStreamUnexpectedEOF(t *testing.T) { |
| 63 | + partialBlockBytes := []byte{} |
| 64 | + dummyBlockBytes := testutil.ConstructRandomBytes(t, 100) |
| 65 | + lenBytes := proto.EncodeVarint(uint64(len(dummyBlockBytes))) |
| 66 | + partialBlockBytes = append(partialBlockBytes, lenBytes...) |
| 67 | + partialBlockBytes = append(partialBlockBytes, dummyBlockBytes...) |
| 68 | + testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:1]) |
| 69 | + testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:2]) |
| 70 | + testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:5]) |
| 71 | + testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:20]) |
| 72 | +} |
| 73 | + |
| 74 | +func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockBytes []byte) { |
| 75 | + env := newTestEnv(t) |
| 76 | + defer env.Cleanup() |
| 77 | + w := newTestBlockfileWrapper(t, env) |
| 78 | + blockfileMgr := w.blockfileMgr |
| 79 | + blocks := testutil.ConstructTestBlocks(t, numBlocks) |
| 80 | + w.addBlocks(blocks) |
| 81 | + blockfileMgr.currentFileWriter.append(partialBlockBytes, true) |
| 82 | + w.close() |
| 83 | + s, err := newBlockfileStream(deriveBlockfilePath(blockfileMgr.rootDir, 0), 0) |
| 84 | + defer s.close() |
| 85 | + testutil.AssertNoError(t, err, "Error in constructing blockfile stream") |
| 86 | + |
| 87 | + for i := 0; i < numBlocks; i++ { |
| 88 | + blockBytes, err := s.nextBlockBytes() |
| 89 | + testutil.AssertNotNil(t, blockBytes) |
| 90 | + testutil.AssertNoError(t, err, "Error in getting next block") |
| 91 | + } |
| 92 | + blockBytes, err := s.nextBlockBytes() |
| 93 | + testutil.AssertNil(t, blockBytes) |
| 94 | + testutil.AssertSame(t, err, ErrUnexpectedEndOfBlockfile) |
| 95 | +} |
| 96 | + |
| 97 | +func TestBlockStream(t *testing.T) { |
| 98 | + testBlockStream(t, 1) |
| 99 | + testBlockStream(t, 2) |
| 100 | + testBlockStream(t, 10) |
| 101 | +} |
| 102 | + |
| 103 | +func testBlockStream(t *testing.T, numFiles int) { |
| 104 | + env := newTestEnv(t) |
| 105 | + defer env.Cleanup() |
| 106 | + w := newTestBlockfileWrapper(t, env) |
| 107 | + defer w.close() |
| 108 | + blockfileMgr := w.blockfileMgr |
| 109 | + |
| 110 | + numBlocksInEachFile := 10 |
| 111 | + for i := 0; i < numFiles; i++ { |
| 112 | + blocks := testutil.ConstructTestBlocks(t, numBlocksInEachFile) |
| 113 | + w.addBlocks(blocks) |
| 114 | + blockfileMgr.moveToNextFile() |
| 115 | + } |
| 116 | + s, err := newBlockStream(blockfileMgr.rootDir, 0, 0, numFiles-1) |
| 117 | + defer s.close() |
| 118 | + testutil.AssertNoError(t, err, "Error in constructing new block stream") |
| 119 | + blockCount := 0 |
| 120 | + for { |
| 121 | + blockBytes, err := s.nextBlockBytes() |
| 122 | + testutil.AssertNoError(t, err, "Error in getting next block") |
| 123 | + if blockBytes == nil { |
| 124 | + break |
| 125 | + } |
| 126 | + blockCount++ |
| 127 | + } |
| 128 | + // After the stream has been exhausted, both blockBytes and err should be nil |
| 129 | + blockBytes, err := s.nextBlockBytes() |
| 130 | + testutil.AssertNil(t, blockBytes) |
| 131 | + testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file") |
| 132 | + testutil.AssertEquals(t, blockCount, numFiles*numBlocksInEachFile) |
| 133 | +} |
0 commit comments