github.com/m3db/m3@v1.5.0/src/x/instrument/build_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package instrument
    22  
    23  import (
    24  	"fmt"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/fortytw2/leaktest"
    30  	"github.com/stretchr/testify/require"
    31  	"github.com/uber-go/tally"
    32  )
    33  
    34  const (
    35  	testReportInterval = 10 * time.Millisecond
    36  )
    37  
    38  func newTestOptions() Options {
    39  	testScope := tally.NewTestScope("", nil)
    40  	return NewOptions().
    41  		SetMetricsScope(testScope).
    42  		SetReportInterval(testReportInterval)
    43  }
    44  
    45  func TestStartStop(t *testing.T) {
    46  	defer leaktest.Check(t)()
    47  
    48  	rep := NewBuildReporter(newTestOptions())
    49  	require.NoError(t, rep.Start())
    50  	require.NoError(t, rep.Stop())
    51  }
    52  
    53  func TestStartStart(t *testing.T) {
    54  	defer leaktest.Check(t)()
    55  
    56  	rep := NewBuildReporter(newTestOptions())
    57  	defer rep.Stop()
    58  	require.NoError(t, rep.Start())
    59  	require.Error(t, rep.Start())
    60  }
    61  
    62  func TestStopWithoutStart(t *testing.T) {
    63  	defer leaktest.Check(t)()
    64  
    65  	rep := NewBuildReporter(newTestOptions())
    66  	require.Error(t, rep.Stop())
    67  }
    68  
    69  func TestMultipleStop(t *testing.T) {
    70  	defer leaktest.Check(t)()
    71  
    72  	rep := NewBuildReporter(newTestOptions())
    73  	require.NoError(t, rep.Start())
    74  	go rep.Stop()
    75  	go rep.Stop()
    76  }
    77  
    78  func TestVersionReported(t *testing.T) {
    79  	defer leaktest.Check(t)()
    80  
    81  	opts := newTestOptions()
    82  	rep := NewBuildReporter(opts)
    83  	require.NoError(t, rep.Start())
    84  
    85  	testScope := opts.MetricsScope().(tally.TestScope)
    86  	notFound := true
    87  	for notFound {
    88  		snapshot := testScope.Snapshot().Gauges()
    89  		for key := range snapshot {
    90  			if strings.Contains(key, buildInfoMetricName) {
    91  				notFound = false
    92  				break
    93  			}
    94  		}
    95  	}
    96  
    97  	require.NoError(t, rep.Stop())
    98  }
    99  
   100  func TestAgeReported(t *testing.T) {
   101  	defer leaktest.Check(t)()
   102  
   103  	opts := newTestOptions()
   104  	rep := NewBuildReporter(opts)
   105  	require.NoError(t, rep.Start())
   106  
   107  	testScope := opts.MetricsScope().(tally.TestScope)
   108  	notFound := true
   109  	for notFound {
   110  		snapshot := testScope.Snapshot().Gauges()
   111  		for key := range snapshot {
   112  			if strings.Contains(key, buildAgeMetricName) {
   113  				notFound = false
   114  				break
   115  			}
   116  		}
   117  	}
   118  
   119  	require.NoError(t, rep.Stop())
   120  }
   121  
   122  func TestAgeReportedIsCorrect(t *testing.T) {
   123  	defer leaktest.Check(t)()
   124  
   125  	BuildTimeUnix = fmt.Sprintf("%d", time.Now().Add(-24*time.Hour).Unix())
   126  	opts := newTestOptions()
   127  	rep := NewBuildReporter(opts)
   128  	require.NoError(t, rep.Start())
   129  
   130  	testScope := opts.MetricsScope().(tally.TestScope)
   131  	notFound := true
   132  	age := float64(0.0)
   133  	for notFound {
   134  		snapshot := testScope.Snapshot().Gauges()
   135  		for key, value := range snapshot {
   136  			if strings.Contains(key, buildAgeMetricName) {
   137  				age = value.Value()
   138  				notFound = false
   139  				break
   140  			}
   141  		}
   142  	}
   143  
   144  	dayInNanos := float64(24 * time.Hour / time.Nanosecond)
   145  	require.True(t, age >= dayInNanos)
   146  
   147  	require.NoError(t, rep.Stop())
   148  }
   149  
   150  func TestBuildReporterFailsForWrongAge(t *testing.T) {
   151  	defer leaktest.Check(t)()
   152  
   153  	BuildTimeUnix = fmt.Sprintf("-%d", time.Now().Unix())
   154  	opts := newTestOptions()
   155  	rep := NewBuildReporter(opts)
   156  	require.Error(t, rep.Start())
   157  }