github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/common/multichannel/metrics_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package multichannel 8 9 import ( 10 "github.com/hechain20/hechain/common/metrics/metricsfakes" 11 "github.com/hechain20/hechain/orderer/common/types" 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 ) 15 16 var _ = Describe("Metrics", func() { 17 Context("NewMetrics", func() { 18 var ( 19 fakeProvider *metricsfakes.Provider 20 fakeGauge *metricsfakes.Gauge 21 ) 22 23 BeforeEach(func() { 24 fakeProvider = &metricsfakes.Provider{} 25 fakeGauge = &metricsfakes.Gauge{} 26 27 fakeProvider.NewGaugeReturns(fakeGauge) 28 }) 29 30 It("uses the provider to initialize a new Metrics object", func() { 31 metrics := NewMetrics(fakeProvider) 32 33 Expect(metrics).NotTo(BeNil()) 34 Expect(fakeProvider.NewGaugeCallCount()).To(Equal(2)) 35 36 Expect(metrics.ConsensusRelation).To(Equal(fakeGauge)) 37 Expect(metrics.Status).To(Equal(fakeGauge)) 38 }) 39 }) 40 41 Context("reportStatus", func() { 42 var ( 43 fakeProvider *metricsfakes.Provider 44 fakeGauge *metricsfakes.Gauge 45 metrics *Metrics 46 ) 47 48 BeforeEach(func() { 49 fakeProvider = &metricsfakes.Provider{} 50 fakeGauge = &metricsfakes.Gauge{} 51 52 fakeProvider.NewGaugeReturns(fakeGauge) 53 fakeGauge.WithReturns(fakeGauge) 54 55 metrics = NewMetrics(fakeProvider) 56 Expect(metrics.Status).To(Equal(fakeGauge)) 57 }) 58 59 It("reports status inactive as a gauge", func() { 60 metrics.reportStatus("fake-channel", types.StatusInactive) 61 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 62 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 63 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 64 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(0))) 65 }) 66 67 It("reports status active as a gauge", func() { 68 metrics.reportStatus("fake-channel", types.StatusActive) 69 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 70 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 71 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 72 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(1))) 73 }) 74 75 It("reports status onboarding as a gauge", func() { 76 metrics.reportStatus("fake-channel", types.StatusOnBoarding) 77 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 78 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 79 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 80 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(2))) 81 }) 82 83 It("reports status failure as a gauge", func() { 84 metrics.reportStatus("fake-channel", types.StatusFailed) 85 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 86 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 87 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 88 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(3))) 89 }) 90 91 It("panics when reporting an unknown cluster status", func() { 92 Expect(func() { metrics.reportStatus("fake-channel", "unknown") }).To(Panic()) 93 }) 94 }) 95 96 Context("reportConsensusRelation", func() { 97 var ( 98 fakeProvider *metricsfakes.Provider 99 fakeGauge *metricsfakes.Gauge 100 metrics *Metrics 101 ) 102 103 BeforeEach(func() { 104 fakeProvider = &metricsfakes.Provider{} 105 fakeGauge = &metricsfakes.Gauge{} 106 107 fakeProvider.NewGaugeReturns(fakeGauge) 108 fakeGauge.WithReturns(fakeGauge) 109 110 metrics = NewMetrics(fakeProvider) 111 Expect(metrics.ConsensusRelation).To(Equal(fakeGauge)) 112 }) 113 114 It("reports consensus relation other as a gauge", func() { 115 metrics.reportConsensusRelation("fake-channel", types.ConsensusRelationOther) 116 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 117 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 118 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 119 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(0))) 120 }) 121 122 It("reports consensus relation consenter as a gauge", func() { 123 metrics.reportConsensusRelation("fake-channel", types.ConsensusRelationConsenter) 124 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 125 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 126 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 127 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(1))) 128 }) 129 130 It("reports consensus relation follower as a gauge", func() { 131 metrics.reportConsensusRelation("fake-channel", types.ConsensusRelationFollower) 132 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 133 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 134 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 135 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(2))) 136 }) 137 138 It("reports consensus relation config-tracker as a gauge", func() { 139 metrics.reportConsensusRelation("fake-channel", types.ConsensusRelationConfigTracker) 140 Expect(fakeGauge.WithCallCount()).To(Equal(1)) 141 Expect(fakeGauge.WithArgsForCall(0)).To(Equal([]string{"channel", "fake-channel"})) 142 Expect(fakeGauge.SetCallCount()).To(Equal(1)) 143 Expect(fakeGauge.SetArgsForCall(0)).To(Equal(float64(3))) 144 }) 145 146 It("panics when reporting an unknown consensus relation", func() { 147 Expect(func() { metrics.reportConsensusRelation("fake-channel", "unknown") }).To(Panic()) 148 }) 149 }) 150 }) 151 152 func newFakeMetrics(fakeFields *fakeMetricsFields) *Metrics { 153 return &Metrics{ 154 ConsensusRelation: fakeFields.fakeConsensusRelation, 155 Status: fakeFields.fakeStatus, 156 } 157 } 158 159 type fakeMetricsFields struct { 160 fakeConsensusRelation *metricsfakes.Gauge 161 fakeStatus *metricsfakes.Gauge 162 } 163 164 func newFakeMetricsFields() *fakeMetricsFields { 165 return &fakeMetricsFields{ 166 fakeConsensusRelation: newFakeGauge(), 167 fakeStatus: newFakeGauge(), 168 } 169 } 170 171 func newFakeGauge() *metricsfakes.Gauge { 172 fakeGauge := &metricsfakes.Gauge{} 173 fakeGauge.WithReturns(fakeGauge) 174 return fakeGauge 175 }