github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/flogging/metrics/observer_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package metrics_test 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/common/flogging/metrics" 13 commonmetrics "github.com/hyperledger/fabric/common/metrics" 14 "github.com/hyperledger/fabric/common/metrics/metricsfakes" 15 "github.com/stretchr/testify/assert" 16 "go.uber.org/zap/zapcore" 17 ) 18 19 func TestNewObserver(t *testing.T) { 20 provider := &metricsfakes.Provider{} 21 checkedCounter := &metricsfakes.Counter{} 22 writtenCounter := &metricsfakes.Counter{} 23 provider.NewCounterStub = func(c commonmetrics.CounterOpts) commonmetrics.Counter { 24 switch c.Name { 25 case "entries_checked": 26 assert.Equal(t, metrics.CheckedCountOpts, c) 27 return checkedCounter 28 case "entries_written": 29 assert.Equal(t, metrics.WriteCountOpts, c) 30 return writtenCounter 31 default: 32 return nil 33 } 34 } 35 36 expectedObserver := &metrics.Observer{ 37 CheckedCounter: checkedCounter, 38 WrittenCounter: writtenCounter, 39 } 40 m := metrics.NewObserver(provider) 41 assert.Equal(t, expectedObserver, m) 42 assert.Equal(t, 2, provider.NewCounterCallCount()) 43 } 44 45 func TestCheck(t *testing.T) { 46 counter := &metricsfakes.Counter{} 47 counter.WithReturns(counter) 48 49 m := metrics.Observer{CheckedCounter: counter} 50 entry := zapcore.Entry{Level: zapcore.DebugLevel} 51 checkedEntry := &zapcore.CheckedEntry{} 52 m.Check(entry, checkedEntry) 53 54 assert.Equal(t, 1, counter.WithCallCount()) 55 assert.Equal(t, []string{"level", "debug"}, counter.WithArgsForCall(0)) 56 57 assert.Equal(t, 1, counter.AddCallCount()) 58 assert.Equal(t, float64(1), counter.AddArgsForCall(0)) 59 } 60 61 func TestWrite(t *testing.T) { 62 counter := &metricsfakes.Counter{} 63 counter.WithReturns(counter) 64 65 m := metrics.Observer{WrittenCounter: counter} 66 entry := zapcore.Entry{Level: zapcore.DebugLevel} 67 m.WriteEntry(entry, nil) 68 69 assert.Equal(t, 1, counter.WithCallCount()) 70 assert.Equal(t, []string{"level", "debug"}, counter.WithArgsForCall(0)) 71 72 assert.Equal(t, 1, counter.AddCallCount()) 73 assert.Equal(t, float64(1), counter.AddArgsForCall(0)) 74 }