github.com/ewagmig/fabric@v2.1.1+incompatible/core/ledger/util/couchdb/metrics_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package couchdb
     8  
     9  import (
    10  	"context"
    11  	"net/http"
    12  	"net/url"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric/common/metrics/disabled"
    16  	"github.com/hyperledger/fabric/common/metrics/metricsfakes"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  func TestAPIProcessTimeMetric(t *testing.T) {
    21  	gt := NewGomegaWithT(t)
    22  	fakeHistogram := &metricsfakes.Histogram{}
    23  	fakeHistogram.WithReturns(fakeHistogram)
    24  
    25  	// create a new couch instance
    26  	config := testConfig()
    27  	config.MaxRetries = 0
    28  	couchInstance, err := CreateCouchInstance(config, &disabled.Provider{})
    29  	gt.Expect(err).NotTo(HaveOccurred(), "Error when trying to create couch instance")
    30  
    31  	couchInstance.stats = &stats{
    32  		apiProcessingTime: fakeHistogram,
    33  	}
    34  
    35  	url, err := url.Parse("http://locahost:0")
    36  	gt.Expect(err).NotTo(HaveOccurred(), "Error when trying to parse URL")
    37  
    38  	couchInstance.handleRequest(context.Background(), http.MethodGet, "db_name", "function_name", url, nil, "", "", 0, true, nil)
    39  	gt.Expect(fakeHistogram.ObserveCallCount()).To(Equal(1))
    40  	gt.Expect(fakeHistogram.ObserveArgsForCall(0)).NotTo(BeZero())
    41  	gt.Expect(fakeHistogram.WithArgsForCall(0)).To(Equal([]string{
    42  		"database", "db_name",
    43  		"function_name", "function_name",
    44  		"result", "0",
    45  	}))
    46  }