github.com/thanos-io/thanos@v0.32.5/internal/cortex/util/time_test.go (about)

     1  // Copyright (c) The Cortex Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package util
     5  
     6  import (
     7  	"fmt"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestTimeFromMillis(t *testing.T) {
    16  	var testExpr = []struct {
    17  		input    int64
    18  		expected time.Time
    19  	}{
    20  		{input: 1000, expected: time.Unix(1, 0)},
    21  		{input: 1500, expected: time.Unix(1, 500*nanosecondsInMillisecond)},
    22  	}
    23  
    24  	for i, c := range testExpr {
    25  		t.Run(fmt.Sprint(i), func(t *testing.T) {
    26  			res := TimeFromMillis(c.input)
    27  			require.Equal(t, c.expected, res)
    28  		})
    29  	}
    30  }
    31  
    32  func TestDurationWithJitter(t *testing.T) {
    33  	const numRuns = 1000
    34  
    35  	for i := 0; i < numRuns; i++ {
    36  		actual := DurationWithJitter(time.Minute, 0.5)
    37  		assert.GreaterOrEqual(t, int64(actual), int64(30*time.Second))
    38  		assert.LessOrEqual(t, int64(actual), int64(90*time.Second))
    39  	}
    40  }
    41  
    42  func TestDurationWithJitter_ZeroInputDuration(t *testing.T) {
    43  	assert.Equal(t, time.Duration(0), DurationWithJitter(time.Duration(0), 0.5))
    44  }
    45  
    46  func TestDurationWithPositiveJitter(t *testing.T) {
    47  	const numRuns = 1000
    48  
    49  	for i := 0; i < numRuns; i++ {
    50  		actual := DurationWithPositiveJitter(time.Minute, 0.5)
    51  		assert.GreaterOrEqual(t, int64(actual), int64(60*time.Second))
    52  		assert.LessOrEqual(t, int64(actual), int64(90*time.Second))
    53  	}
    54  }
    55  
    56  func TestDurationWithPositiveJitter_ZeroInputDuration(t *testing.T) {
    57  	assert.Equal(t, time.Duration(0), DurationWithPositiveJitter(time.Duration(0), 0.5))
    58  }
    59  
    60  func TestParseTime(t *testing.T) {
    61  	var tests = []struct {
    62  		input  string
    63  		fail   bool
    64  		result time.Time
    65  	}{
    66  		{
    67  			input: "",
    68  			fail:  true,
    69  		}, {
    70  			input: "abc",
    71  			fail:  true,
    72  		}, {
    73  			input: "30s",
    74  			fail:  true,
    75  		}, {
    76  			input:  "123",
    77  			result: time.Unix(123, 0),
    78  		}, {
    79  			input:  "123.123",
    80  			result: time.Unix(123, 123000000),
    81  		}, {
    82  			input:  "2015-06-03T13:21:58.555Z",
    83  			result: time.Unix(1433337718, 555*time.Millisecond.Nanoseconds()),
    84  		}, {
    85  			input:  "2015-06-03T14:21:58.555+01:00",
    86  			result: time.Unix(1433337718, 555*time.Millisecond.Nanoseconds()),
    87  		}, {
    88  			// Test nanosecond rounding.
    89  			input:  "2015-06-03T13:21:58.56789Z",
    90  			result: time.Unix(1433337718, 567*1e6),
    91  		}, {
    92  			// Test float rounding.
    93  			input:  "1543578564.705",
    94  			result: time.Unix(1543578564, 705*1e6),
    95  		},
    96  	}
    97  
    98  	for _, test := range tests {
    99  		ts, err := ParseTime(test.input)
   100  		if test.fail {
   101  			require.Error(t, err)
   102  			continue
   103  		}
   104  
   105  		require.NoError(t, err)
   106  		assert.Equal(t, TimeToMillis(test.result), ts)
   107  	}
   108  }
   109  
   110  func TestNewDisableableTicker_Enabled(t *testing.T) {
   111  	stop, ch := NewDisableableTicker(10 * time.Millisecond)
   112  	defer stop()
   113  
   114  	time.Sleep(100 * time.Millisecond)
   115  
   116  	select {
   117  	case <-ch:
   118  		break
   119  	default:
   120  		t.Error("ticker should have ticked when enabled")
   121  	}
   122  }
   123  
   124  func TestNewDisableableTicker_Disabled(t *testing.T) {
   125  	stop, ch := NewDisableableTicker(0)
   126  	defer stop()
   127  
   128  	time.Sleep(100 * time.Millisecond)
   129  
   130  	select {
   131  	case <-ch:
   132  		t.Error("ticker should not have ticked when disabled")
   133  	default:
   134  		break
   135  	}
   136  }