github.com/imran-kn/cilium-fork@v1.6.9/pkg/spanstat/spanstat_test.go (about)

     1  // Copyright 2018 Authors of Cilium
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package spanstat
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	. "gopkg.in/check.v1"
    24  )
    25  
    26  func Test(t *testing.T) {
    27  	TestingT(t)
    28  }
    29  
    30  type SpanStatTestSuite struct{}
    31  
    32  var _ = Suite(&SpanStatTestSuite{})
    33  
    34  func (s *SpanStatTestSuite) TestSpanStatStart(c *C) {
    35  	span1 := Start()
    36  	span1.EndError(nil)
    37  	c.Assert(span1.Total(), Not(Equals), time.Duration(0))
    38  }
    39  
    40  func (s *SpanStatTestSuite) TestSpanStat(c *C) {
    41  	span1 := SpanStat{}
    42  
    43  	// no spans measured yet
    44  	c.Assert(span1.Total(), Equals, time.Duration(0))
    45  	c.Assert(span1.SuccessTotal(), Equals, time.Duration(0))
    46  	c.Assert(span1.FailureTotal(), Equals, time.Duration(0))
    47  
    48  	// End() without Start()
    49  	span1.End(true)
    50  	c.Assert(span1.Total(), Equals, time.Duration(0))
    51  	c.Assert(span1.SuccessTotal(), Equals, time.Duration(0))
    52  	c.Assert(span1.FailureTotal(), Equals, time.Duration(0))
    53  
    54  	// Start() but no end yet
    55  	span1.Start()
    56  	c.Assert(span1.Total(), Equals, time.Duration(0))
    57  	c.Assert(span1.SuccessTotal(), Equals, time.Duration(0))
    58  	c.Assert(span1.FailureTotal(), Equals, time.Duration(0))
    59  
    60  	// First span measured with End()
    61  	span1.End(true)
    62  	spanTotal1 := span1.Total()
    63  	spanSuccessTotal1 := span1.SuccessTotal()
    64  	spanFailureTotal1 := span1.FailureTotal()
    65  	c.Assert(span1.Total(), Not(Equals), time.Duration(0))
    66  	c.Assert(span1.SuccessTotal(), Not(Equals), time.Duration(0))
    67  	c.Assert(span1.FailureTotal(), Equals, time.Duration(0))
    68  	c.Assert(span1.Total(), Equals, span1.SuccessTotal()+span1.FailureTotal())
    69  
    70  	// End() without a prior Start(), no change
    71  	span1.End(true)
    72  	c.Assert(span1.Total(), Equals, spanTotal1)
    73  	c.Assert(span1.SuccessTotal(), Equals, spanSuccessTotal1)
    74  	c.Assert(span1.FailureTotal(), Equals, spanFailureTotal1)
    75  
    76  	span1.Start()
    77  	span1.End(false)
    78  	c.Assert(span1.Total(), Not(Equals), spanTotal1)
    79  	c.Assert(span1.SuccessTotal(), Equals, spanSuccessTotal1)
    80  	c.Assert(span1.FailureTotal(), Not(Equals), spanFailureTotal1)
    81  	c.Assert(span1.Total(), Equals, span1.SuccessTotal()+span1.FailureTotal())
    82  
    83  	span1.Reset()
    84  	c.Assert(span1.Total(), Equals, time.Duration(0))
    85  	c.Assert(span1.SuccessTotal(), Equals, time.Duration(0))
    86  	c.Assert(span1.FailureTotal(), Equals, time.Duration(0))
    87  }
    88  
    89  func (s *SpanStatTestSuite) TestSpanStatSeconds(c *C) {
    90  	span1 := Start()
    91  	c.Assert(span1.Seconds(), Not(Equals), float64(0))
    92  }