github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/lightning/metric/metric_test.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package metric_test
    15  
    16  import (
    17  	"errors"
    18  	"testing"
    19  
    20  	. "github.com/pingcap/check"
    21  
    22  	"github.com/prometheus/client_golang/prometheus"
    23  
    24  	"github.com/pingcap/tidb-lightning/lightning/metric"
    25  )
    26  
    27  type testMetricSuite struct{}
    28  
    29  func (s *testMetricSuite) SetUpSuite(c *C)    {}
    30  func (s *testMetricSuite) TearDownSuite(c *C) {}
    31  
    32  var _ = Suite(&testMetricSuite{})
    33  
    34  func TestMetric(t *testing.T) {
    35  	TestingT(t)
    36  }
    37  
    38  func (s *testMetricSuite) TestReadCounter(c *C) {
    39  	counter := prometheus.NewCounter(prometheus.CounterOpts{})
    40  	counter.Add(1256.0)
    41  	counter.Add(2214.0)
    42  	c.Assert(metric.ReadCounter(counter), Equals, 3470.0)
    43  }
    44  
    45  func (s *testMetricSuite) TestReadHistogramSum(c *C) {
    46  	histogram := prometheus.NewHistogram(prometheus.HistogramOpts{})
    47  	histogram.Observe(11131.5)
    48  	histogram.Observe(15261.0)
    49  	c.Assert(metric.ReadHistogramSum(histogram), Equals, 26392.5)
    50  }
    51  
    52  func (s *testMetricSuite) TestRecordEngineCount(c *C) {
    53  	metric.RecordEngineCount("table1", nil)
    54  	metric.RecordEngineCount("table1", errors.New("mock error"))
    55  	successCounter, err := metric.ProcessedEngineCounter.GetMetricWithLabelValues("table1", "success")
    56  	c.Assert(err, IsNil)
    57  	c.Assert(metric.ReadCounter(successCounter), Equals, 1.0)
    58  	failureCount, err := metric.ProcessedEngineCounter.GetMetricWithLabelValues("table1", "failure")
    59  	c.Assert(err, IsNil)
    60  	c.Assert(metric.ReadCounter(failureCount), Equals, 1.0)
    61  }