github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/blocks_itr_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 "time" 22 23 "github.com/hyperledger/fabric/common/ledger/testutil" 24 "github.com/hyperledger/fabric/protos/common" 25 ) 26 27 func TestBlocksItrBlockingNext(t *testing.T) { 28 env := newTestEnv(t, NewConf(testPath(), 0)) 29 defer env.Cleanup() 30 blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger") 31 defer blkfileMgrWrapper.close() 32 blkfileMgr := blkfileMgrWrapper.blockfileMgr 33 34 blocks := testutil.ConstructTestBlocks(t, 10) 35 blkfileMgrWrapper.addBlocks(blocks[:5]) 36 37 itr, err := blkfileMgr.retrieveBlocks(1) 38 defer itr.Close() 39 testutil.AssertNoError(t, err, "") 40 doneChan := make(chan bool) 41 go testIterateAndVerify(t, itr, blocks[1:], doneChan) 42 for { 43 if itr.blockNumToRetrieve == 5 { 44 break 45 } 46 time.Sleep(time.Millisecond * 10) 47 } 48 testAppendBlocks(blkfileMgrWrapper, blocks[5:7]) 49 blkfileMgr.moveToNextFile() 50 time.Sleep(time.Millisecond * 10) 51 testAppendBlocks(blkfileMgrWrapper, blocks[7:]) 52 <-doneChan 53 } 54 55 func testIterateAndVerify(t *testing.T, itr *blocksItr, blocks []*common.Block, doneChan chan bool) { 56 blocksIterated := 0 57 for { 58 logger.Infof("blocksIterated: %v", blocksIterated) 59 bh, err := itr.Next() 60 testutil.AssertNoError(t, err, "") 61 testutil.AssertEquals(t, bh.(*blockHolder).GetBlock(), blocks[blocksIterated]) 62 blocksIterated++ 63 if blocksIterated == len(blocks) { 64 break 65 } 66 } 67 doneChan <- true 68 } 69 70 func testAppendBlocks(blkfileMgrWrapper *testBlockfileMgrWrapper, blocks []*common.Block) { 71 blkfileMgrWrapper.addBlocks(blocks) 72 }