github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  )
    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, NewConf(testPath(), 0))
    34  	defer env.Cleanup()
    35  	ledgerid := "testledger"
    36  	w := newTestBlockfileWrapper(env, ledgerid)
    37  	blocks := testutil.ConstructTestBlocks(t, numBlocks)
    38  	w.addBlocks(blocks)
    39  	w.close()
    40  
    41  	s, err := newBlockfileStream(w.blockfileMgr.rootDir, 0, 0)
    42  	defer s.close()
    43  	testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
    44  
    45  	blockCount := 0
    46  	for {
    47  		blockBytes, err := s.nextBlockBytes()
    48  		testutil.AssertNoError(t, err, "Error in getting next block")
    49  		if blockBytes == nil {
    50  			break
    51  		}
    52  		blockCount++
    53  	}
    54  	// After the stream has been exhausted, both blockBytes and err should be nil
    55  	blockBytes, err := s.nextBlockBytes()
    56  	testutil.AssertNil(t, blockBytes)
    57  	testutil.AssertNoError(t, err, "Error in getting next block after exhausting the file")
    58  	testutil.AssertEquals(t, blockCount, numBlocks)
    59  }
    60  
    61  func TestBlockFileStreamUnexpectedEOF(t *testing.T) {
    62  	partialBlockBytes := []byte{}
    63  	dummyBlockBytes := testutil.ConstructRandomBytes(t, 100)
    64  	lenBytes := proto.EncodeVarint(uint64(len(dummyBlockBytes)))
    65  	partialBlockBytes = append(partialBlockBytes, lenBytes...)
    66  	partialBlockBytes = append(partialBlockBytes, dummyBlockBytes...)
    67  	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:1])
    68  	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:2])
    69  	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:5])
    70  	testBlockFileStreamUnexpectedEOF(t, 10, partialBlockBytes[:20])
    71  }
    72  
    73  func testBlockFileStreamUnexpectedEOF(t *testing.T, numBlocks int, partialBlockBytes []byte) {
    74  	env := newTestEnv(t, NewConf(testPath(), 0))
    75  	defer env.Cleanup()
    76  	w := newTestBlockfileWrapper(env, "testLedger")
    77  	blockfileMgr := w.blockfileMgr
    78  	blocks := testutil.ConstructTestBlocks(t, numBlocks)
    79  	w.addBlocks(blocks)
    80  	blockfileMgr.currentFileWriter.append(partialBlockBytes, true)
    81  	w.close()
    82  	s, err := newBlockfileStream(blockfileMgr.rootDir, 0, 0)
    83  	defer s.close()
    84  	testutil.AssertNoError(t, err, "Error in constructing blockfile stream")
    85  
    86  	for i := 0; i < numBlocks; i++ {
    87  		blockBytes, err := s.nextBlockBytes()
    88  		testutil.AssertNotNil(t, blockBytes)
    89  		testutil.AssertNoError(t, err, "Error in getting next block")
    90  	}
    91  	blockBytes, err := s.nextBlockBytes()
    92  	testutil.AssertNil(t, blockBytes)
    93  	testutil.AssertSame(t, err, ErrUnexpectedEndOfBlockfile)
    94  }
    95  
    96  func TestBlockStream(t *testing.T) {
    97  	testBlockStream(t, 1)
    98  	testBlockStream(t, 2)
    99  	testBlockStream(t, 10)
   100  }
   101  
   102  func testBlockStream(t *testing.T, numFiles int) {
   103  	env := newTestEnv(t, NewConf(testPath(), 0))
   104  	defer env.Cleanup()
   105  	w := newTestBlockfileWrapper(env, "testLedger")
   106  	defer w.close()
   107  	blockfileMgr := w.blockfileMgr
   108  
   109  	numBlocksInEachFile := 10
   110  	bg := testutil.NewBlockGenerator(t)
   111  	for i := 0; i < numFiles; i++ {
   112  		blocks := bg.NextTestBlocks(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  }