github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/node/grpc_limiters_test.go (about)

     1  /*
     2  Copyright Hitachi America, Ltd.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package node
     8  
     9  import (
    10  	"context"
    11  	"fmt"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/require"
    15  	"google.golang.org/grpc"
    16  
    17  	"github.com/hyperledger/fabric/core/peer"
    18  )
    19  
    20  func TestInitGrpcSemaphores(t *testing.T) {
    21  	config := peer.Config{
    22  		LimitsConcurrencyEndorserService: 5,
    23  		LimitsConcurrencyDeliverService:  5,
    24  	}
    25  	semaphores := initGrpcSemaphores(&config)
    26  	require.Equal(t, 2, len(semaphores))
    27  }
    28  
    29  func TestInitGrpcNoSemaphores(t *testing.T) {
    30  	config := peer.Config{
    31  		LimitsConcurrencyEndorserService: 0,
    32  		LimitsConcurrencyDeliverService:  0,
    33  	}
    34  	semaphores := initGrpcSemaphores(&config)
    35  	require.Equal(t, 0, len(semaphores))
    36  }
    37  
    38  func TestInitGrpcSemaphoresPanic(t *testing.T) {
    39  	config := peer.Config{
    40  		LimitsConcurrencyEndorserService: -1,
    41  	}
    42  	require.PanicsWithValue(t, "permits must be greater than 0", func() { initGrpcSemaphores(&config) })
    43  }
    44  
    45  func TestUnaryGrpcLimiterExceedLimit(t *testing.T) {
    46  	limit := 10
    47  	fullMethod := "/protos.Endorser/ProcessProposal"
    48  	semaphores := initGrpcSemaphores(&peer.Config{LimitsConcurrencyEndorserService: limit})
    49  	// acquire all permits so that semaphore buffer is full, expect error when calling interceptor
    50  	for i := 0; i < limit; i++ {
    51  		semaphores["/protos.Endorser"].TryAcquire()
    52  	}
    53  	interceptor := unaryGrpcLimiter(semaphores)
    54  	_, err := interceptor(context.Background(), nil, &grpc.UnaryServerInfo{FullMethod: fullMethod}, nil)
    55  	require.EqualError(t, err, fmt.Sprintf("too many requests for %s, exceeding concurrency limit (%d)", getServiceName(fullMethod), limit))
    56  }
    57  
    58  func TestStreamGrpcLimiterExceedLimit(t *testing.T) {
    59  	limit := 5
    60  	fullMethods := []string{"/protos.Deliver/Deliver", "/protos.Deliver/DeliverFiltered", "/protos.Deliver/DeliverWithPrivateData"}
    61  	semaphores := initGrpcSemaphores(&peer.Config{LimitsConcurrencyDeliverService: limit})
    62  	// acquire all permits so that semaphore buffer is full, expect error when calling interceptor
    63  	for i := 0; i < limit; i++ {
    64  		semaphores["/protos.Deliver"].TryAcquire()
    65  	}
    66  	interceptor := streamGrpcLimiter(semaphores)
    67  	for _, method := range fullMethods {
    68  		err := interceptor(nil, nil, &grpc.StreamServerInfo{FullMethod: method}, nil)
    69  		require.EqualError(t, err, fmt.Sprintf("too many requests for %s, exceeding concurrency limit (%d)", getServiceName(method), limit))
    70  	}
    71  }
    72  
    73  func TestGetServiceName(t *testing.T) {
    74  	require.Equal(t, "/protos.Endorser", getServiceName("/protos.Endorser/ProcessProposal"))
    75  	require.Equal(t, "/protos.Deliver", getServiceName("/protos.Deliver/Deliver"))
    76  	require.Equal(t, "/protos.Deliver", getServiceName("/protos.Deliver/DeliverFiltered"))
    77  	require.Equal(t, "/protos.Deliver", getServiceName("/protos.Deliver/DeliverWithPrivateData"))
    78  }