github.com/codingfuture/orig-energi3@v0.8.4/metrics/gauge_float64_test.go (about)

     1  // Copyright 2018 The Energi Core Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the Energi Core library.
     4  //
     5  // The Energi Core library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The Energi Core library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the Energi Core library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package metrics
    19  
    20  import "testing"
    21  
    22  func BenchmarkGuageFloat64(b *testing.B) {
    23  	g := NewGaugeFloat64()
    24  	b.ResetTimer()
    25  	for i := 0; i < b.N; i++ {
    26  		g.Update(float64(i))
    27  	}
    28  }
    29  
    30  func TestGaugeFloat64(t *testing.T) {
    31  	g := NewGaugeFloat64()
    32  	g.Update(float64(47.0))
    33  	if v := g.Value(); float64(47.0) != v {
    34  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    35  	}
    36  }
    37  
    38  func TestGaugeFloat64Snapshot(t *testing.T) {
    39  	g := NewGaugeFloat64()
    40  	g.Update(float64(47.0))
    41  	snapshot := g.Snapshot()
    42  	g.Update(float64(0))
    43  	if v := snapshot.Value(); float64(47.0) != v {
    44  		t.Errorf("g.Value(): 47.0 != %v\n", v)
    45  	}
    46  }
    47  
    48  func TestGetOrRegisterGaugeFloat64(t *testing.T) {
    49  	r := NewRegistry()
    50  	NewRegisteredGaugeFloat64("foo", r).Update(float64(47.0))
    51  	t.Logf("registry: %v", r)
    52  	if g := GetOrRegisterGaugeFloat64("foo", r); float64(47.0) != g.Value() {
    53  		t.Fatal(g)
    54  	}
    55  }
    56  
    57  func TestFunctionalGaugeFloat64(t *testing.T) {
    58  	var counter float64
    59  	fg := NewFunctionalGaugeFloat64(func() float64 {
    60  		counter++
    61  		return counter
    62  	})
    63  	fg.Value()
    64  	fg.Value()
    65  	if counter != 2 {
    66  		t.Error("counter != 2")
    67  	}
    68  }
    69  
    70  func TestGetOrRegisterFunctionalGaugeFloat64(t *testing.T) {
    71  	r := NewRegistry()
    72  	NewRegisteredFunctionalGaugeFloat64("foo", r, func() float64 { return 47 })
    73  	if g := GetOrRegisterGaugeFloat64("foo", r); 47 != g.Value() {
    74  		t.Fatal(g)
    75  	}
    76  }