github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/common/ledger/blockledger/util_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package blockledger_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/hyperledger/fabric-protos-go/common"
    13  	"github.com/osdi23p228/fabric/common/deliver/mock"
    14  	"github.com/osdi23p228/fabric/common/ledger/blockledger"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestClose(t *testing.T) {
    19  	for _, testCase := range []struct {
    20  		name               string
    21  		status             common.Status
    22  		isIteratorNil      bool
    23  		expectedCloseCount int
    24  	}{
    25  		{
    26  			name:          "nil iterator",
    27  			isIteratorNil: true,
    28  		},
    29  		{
    30  			name:               "Next() fails",
    31  			status:             common.Status_INTERNAL_SERVER_ERROR,
    32  			expectedCloseCount: 1,
    33  		},
    34  		{
    35  			name:               "Next() succeeds",
    36  			status:             common.Status_SUCCESS,
    37  			expectedCloseCount: 1,
    38  		},
    39  	} {
    40  		t.Run(testCase.name, func(t *testing.T) {
    41  			var iterator *mock.BlockIterator
    42  			reader := &mock.BlockReader{}
    43  			if !testCase.isIteratorNil {
    44  				iterator = &mock.BlockIterator{}
    45  				iterator.NextReturns(&common.Block{}, testCase.status)
    46  				reader.IteratorReturns(iterator, 1)
    47  			}
    48  
    49  			blockledger.GetBlock(reader, 1)
    50  			if !testCase.isIteratorNil {
    51  				assert.Equal(t, testCase.expectedCloseCount, iterator.CloseCallCount())
    52  			}
    53  		})
    54  	}
    55  }