github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/block_stream_test.go (about) 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/common/ledger/testutil" 24 "github.com/hyperledger/fabric/protos/common" 25 ) 26 27 func TestBlockfileStream(t *testing.T) { 28 testBlockfileStream(t, 0) 29 testBlockfileStream(t, 1) 30 testBlockfileStream(t, 10) 31 } 32 33 func testBlockfileStream(t *testing.T, numBlocks int) { 34 env := newTestEnv(t, NewConf(testPath(), 0)) 35 defer env.Cleanup() 36 ledgerid := "testledger" 37 w := newTestBlockfileWrapper(env, ledgerid) 38 blocks := testutil.ConstructTestBlocks(t, numBlocks) 39 w.addBlocks(blocks) 40 w.close() 41 42 s, err := newBlockfileStream(w.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, NewConf(testPath(), 0)) 76 defer env.Cleanup() 77 w := newTestBlockfileWrapper(env, "testLedger") 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(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 ledgerID := "testLedger" 105 env := newTestEnv(t, NewConf(testPath(), 0)) 106 defer env.Cleanup() 107 w := newTestBlockfileWrapper(env, ledgerID) 108 defer w.close() 109 blockfileMgr := w.blockfileMgr 110 111 numBlocksInEachFile := 10 112 bg, gb := testutil.NewBlockGenerator(t, ledgerID, false) 113 w.addBlocks([]*common.Block{gb}) 114 for i := 0; i < numFiles; i++ { 115 numBlocks := numBlocksInEachFile 116 if i == 0 { 117 // genesis block already added so adding one less block 118 numBlocks = numBlocksInEachFile - 1 119 } 120 blocks := bg.NextTestBlocks(numBlocks) 121 w.addBlocks(blocks) 122 blockfileMgr.moveToNextFile() 123 } 124 s, err := newBlockStream(blockfileMgr.rootDir, 0, 0, numFiles-1) 125 defer s.close() 126 testutil.AssertNoError(t, err, "Error in constructing new block stream") 127 blockCount := 0 128 for { 129 blockBytes, err := s.nextBlockBytes() 130 testutil.AssertNoError(t, err, "Error in getting next block") 131 if blockBytes == nil { 132 break 133 } 134 blockCount++ 135 } 136 // After the stream has been exhausted, both blockBytes and err should be nil 137 blockBytes, err := s.nextBlockBytes() 138 testutil.AssertNil(t, blockBytes) 139 testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file") 140 testutil.AssertEquals(t, blockCount, numFiles*numBlocksInEachFile) 141 }