github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/ledger/ram/ramledger_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 ramledger
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    23  	cb "github.com/hyperledger/fabric/protos/common"
    24  
    25  	logging "github.com/op/go-logging"
    26  )
    27  
    28  var genesisBlock = cb.NewBlock(0, nil)
    29  
    30  func init() {
    31  	logging.SetLevel(logging.DEBUG, "")
    32  }
    33  
    34  func NewTestChain(maxSize int) *ramLedger {
    35  	rlf := New(maxSize)
    36  	chain, err := rlf.GetOrCreate(provisional.TestChainID)
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	chain.Append(genesisBlock)
    41  	return chain.(*ramLedger)
    42  }
    43  
    44  // TestAppend ensures that appending blocks stores only the maxSize most recent blocks
    45  // Note that 'only' is applicable because the genesis block will be discarded
    46  func TestAppend(t *testing.T) {
    47  	maxSize := 3
    48  	rl := NewTestChain(maxSize)
    49  	var blocks []*cb.Block
    50  	for i := 0; i < 3; i++ {
    51  		blocks = append(blocks, &cb.Block{Header: &cb.BlockHeader{Number: uint64(i + 1)}})
    52  		rl.appendBlock(blocks[i])
    53  	}
    54  	item := rl.oldest
    55  	for i := 0; i < 3; i++ {
    56  		if item.block == nil {
    57  			t.Fatalf("Block for item %d should not be nil", i)
    58  		}
    59  		if item.block.Header.Number != blocks[i].Header.Number {
    60  			t.Errorf("Expected block %d to be %d but got %d", i, blocks[i].Header.Number, item.block.Header.Number)
    61  		}
    62  		if i != 2 && item.next == nil {
    63  			t.Fatalf("Next item should not be nil")
    64  		} else {
    65  			item = item.next
    66  		}
    67  	}
    68  }
    69  
    70  // TestSignal checks if the signal channel closes when an item is appended
    71  func TestSignal(t *testing.T) {
    72  	maxSize := 3
    73  	rl := NewTestChain(maxSize)
    74  	item := rl.newest
    75  	select {
    76  	case <-item.signal:
    77  		t.Fatalf("There is no successor, there should be no signal to continue")
    78  	default:
    79  	}
    80  	rl.appendBlock(&cb.Block{Header: &cb.BlockHeader{Number: 1}})
    81  	select {
    82  	case <-item.signal:
    83  	default:
    84  		t.Fatalf("There is a successor, there should be a signal to continue")
    85  	}
    86  }
    87  
    88  // TestTruncatingSafety is intended to simulate a reader who fetches a reference to the oldest list item
    89  // which is then pushed off the history by appending greater than the history size (here, 10 appends with
    90  // a maxSize of 3).  We let the go garbage collector ensure the references still exist
    91  func TestTruncationSafety(t *testing.T) {
    92  	maxSize := 3
    93  	newBlocks := 10
    94  	rl := NewTestChain(maxSize)
    95  	item := rl.newest
    96  	for i := 0; i < newBlocks; i++ {
    97  		rl.appendBlock(&cb.Block{Header: &cb.BlockHeader{Number: uint64(i + 1)}})
    98  	}
    99  	count := 0
   100  	for item.next != nil {
   101  		item = item.next
   102  		count++
   103  	}
   104  
   105  	if count != newBlocks {
   106  		t.Fatalf("The iterator should have found %d new blocks but found %d", newBlocks, count)
   107  	}
   108  }