github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/ledger/blkstorage/fsblkstorage/fs_blockstore_provider_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  	"fmt"
    23  
    24  	"github.com/hyperledger/fabric/common/ledger"
    25  	"github.com/hyperledger/fabric/common/ledger/blkstorage"
    26  	"github.com/hyperledger/fabric/common/ledger/testutil"
    27  	"github.com/hyperledger/fabric/protos/common"
    28  )
    29  
    30  func TestMultipleBlockStores(t *testing.T) {
    31  	env := newTestEnv(t, NewConf(testPath(), 0))
    32  	defer env.Cleanup()
    33  
    34  	provider := env.provider
    35  	store1, _ := provider.OpenBlockStore("ledger1")
    36  	defer store1.Shutdown()
    37  
    38  	store2, _ := provider.OpenBlockStore("ledger2")
    39  	defer store2.Shutdown()
    40  
    41  	blocks1 := testutil.ConstructTestBlocks(t, 5)
    42  	for _, b := range blocks1 {
    43  		store1.AddBlock(b)
    44  	}
    45  
    46  	blocks2 := testutil.ConstructTestBlocks(t, 10)
    47  	for _, b := range blocks2 {
    48  		store2.AddBlock(b)
    49  	}
    50  	checkBlocks(t, blocks1, store1)
    51  	checkBlocks(t, blocks2, store2)
    52  }
    53  
    54  func checkBlocks(t *testing.T, expectedBlocks []*common.Block, store blkstorage.BlockStore) {
    55  	bcInfo, _ := store.GetBlockchainInfo()
    56  	testutil.AssertEquals(t, bcInfo.Height, uint64(len(expectedBlocks)))
    57  	testutil.AssertEquals(t, bcInfo.CurrentBlockHash, expectedBlocks[len(expectedBlocks)-1].GetHeader().Hash())
    58  
    59  	itr, _ := store.RetrieveBlocks(0)
    60  	for i := 0; i < len(expectedBlocks); i++ {
    61  		blockHolder, _ := itr.Next()
    62  		block := blockHolder.(ledger.BlockHolder).GetBlock()
    63  		testutil.AssertEquals(t, block, expectedBlocks[i])
    64  	}
    65  }
    66  
    67  func TestBlockStoreProvider(t *testing.T) {
    68  	env := newTestEnv(t, NewConf(testPath(), 0))
    69  	defer env.Cleanup()
    70  
    71  	provider := env.provider
    72  	stores := []blkstorage.BlockStore{}
    73  	numStores := 10
    74  	for i := 0; i < numStores; i++ {
    75  		store, _ := provider.OpenBlockStore(constructLedgerid(i))
    76  		defer store.Shutdown()
    77  		stores = append(stores, store)
    78  	}
    79  
    80  	storeNames, _ := provider.List()
    81  	testutil.AssertEquals(t, len(storeNames), numStores)
    82  
    83  	for i := 0; i < numStores; i++ {
    84  		exists, err := provider.Exists(constructLedgerid(i))
    85  		testutil.AssertNoError(t, err, "")
    86  		testutil.AssertEquals(t, exists, true)
    87  	}
    88  
    89  	exists, err := provider.Exists(constructLedgerid(numStores + 1))
    90  	testutil.AssertNoError(t, err, "")
    91  	testutil.AssertEquals(t, exists, false)
    92  
    93  }
    94  
    95  func constructLedgerid(id int) string {
    96  	return fmt.Sprintf("ledger_%d", id)
    97  }