github.com/m3db/m3@v1.5.0/src/x/time/duration_test.go (about)

     1  // Copyright (c) 2019 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  // Permission is hereby granted, free of charge, to any person obtaining a copy
    22  // of this software and associated documentation files (the "Software"), to deal
    23  // in the Software without restriction, including without limitation the rights
    24  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    25  // copies of the Software, and to permit persons to whom the Software is
    26  // furnished to do so, subject to the following conditions:
    27  //
    28  // The above copyright notice and this permission notice shall be included in
    29  // all copies or substantial portions of the Software.
    30  //
    31  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    32  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    33  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    34  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    35  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    36  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    37  // THE SOFTWARE.
    38  
    39  package time
    40  
    41  import (
    42  	"testing"
    43  	"time"
    44  
    45  	"github.com/stretchr/testify/assert"
    46  	"github.com/stretchr/testify/require"
    47  )
    48  
    49  func TestParseExtendedDuration(t *testing.T) {
    50  	const day = 24 * time.Hour
    51  	tests := []struct {
    52  		text string
    53  		d    time.Duration
    54  	}{
    55  		{"6mon1w3d5min2s", day*180 + day*7 + day*3 + 5*time.Minute + 2*time.Second},
    56  		{"334us", 334 * time.Microsecond},
    57  		{"5m4ms", 5*time.Minute + 4*time.Millisecond},
    58  		{"1y6mon", 365*day + 180*day},
    59  		{"245ns", 245 * time.Nanosecond},
    60  		{"-6mon1w3d5min2s", -(day*180 + day*7 + day*3 + 5*time.Minute + 2*time.Second)},
    61  		{"-334us", -(334 * time.Microsecond)},
    62  		{"-5m4ms", -(5*time.Minute + 4*time.Millisecond)},
    63  		{"-1y6mon", -(365*day + 180*day)},
    64  		{"-245ns", -(245 * time.Nanosecond)},
    65  	}
    66  
    67  	for _, test := range tests {
    68  		d, err := ParseExtendedDuration(test.text)
    69  		require.NoError(t, err, "encountered parse error for %s", test.text)
    70  		assert.Equal(t, test.d, d, "incorrect duration for %s", test.text)
    71  	}
    72  }
    73  
    74  func TestParseExtendedDurationErrors(t *testing.T) {
    75  	tests := []struct {
    76  		text string
    77  		err  string
    78  	}{
    79  		{"4", "could not parse duration: duration='4', err='no unit'"},
    80  		{"", "duration empty"},
    81  		{"4minutes", "could not parse duration: duration='4minutes', err_unknown_unit='minutes'"},
    82  		{"4d3", "could not parse duration: duration='4d3', err='no unit'"},
    83  		{"--4d", "could not parse duration: duration='-4d', err='no value'"},
    84  		{",3490", "could not parse duration: duration=',3490', err='no value'"},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		_, err := ParseExtendedDuration(test.text)
    89  		require.Error(t, err, "expected error for '%s'", test.text)
    90  		assert.Equal(t, test.err, err.Error(), "invalid error for %s", test.text)
    91  	}
    92  }
    93  
    94  func TestToExtendedString(t *testing.T) {
    95  	tests := []struct {
    96  		d      time.Duration
    97  		result string
    98  	}{
    99  		{0, "0s"},
   100  		{time.Nanosecond, "1ns"},
   101  		{30 * time.Nanosecond, "30ns"},
   102  		{60 * time.Microsecond, "60us"},
   103  		{999 * time.Millisecond, "999ms"},
   104  		{20 * time.Second, "20s"},
   105  		{70 * time.Second, "1m10s"},
   106  		{120 * time.Second, "2m"},
   107  		{10 * time.Minute, "10m"},
   108  		{20 * time.Hour, "20h"},
   109  		{24 * time.Hour, "1d"},
   110  		{25 * time.Hour, "1d1h"},
   111  		{24 * 7 * time.Hour, "7d"},
   112  		{24 * 8 * time.Hour, "8d"},
   113  		{24 * 30 * time.Hour, "30d"},
   114  		{24 * 31 * time.Hour, "31d"},
   115  		{24 * 300 * time.Hour, "300d"},
   116  		{-time.Nanosecond, "-1ns"},
   117  		{-30 * time.Nanosecond, "-30ns"},
   118  		{-60 * time.Microsecond, "-60us"},
   119  		{-999 * time.Millisecond, "-999ms"},
   120  		{-20 * time.Second, "-20s"},
   121  		{-70 * time.Second, "-1m10s"},
   122  		{-120 * time.Second, "-2m"},
   123  		{-10 * time.Minute, "-10m"},
   124  		{-20 * time.Hour, "-20h"},
   125  		{-24 * time.Hour, "-1d"},
   126  		{-25 * time.Hour, "-1d1h"},
   127  		{-24 * 7 * time.Hour, "-7d"},
   128  		{-24 * 8 * time.Hour, "-8d"},
   129  		{-24 * 30 * time.Hour, "-30d"},
   130  		{-24 * 31 * time.Hour, "-31d"},
   131  		{-24 * 300 * time.Hour, "-300d"},
   132  	}
   133  
   134  	for _, test := range tests {
   135  		require.Equal(t, test.result, ToExtendedString(test.d))
   136  	}
   137  }
   138  
   139  func TestExtendDurationRoundTrip(t *testing.T) {
   140  	inputs := []string{
   141  		"0s",
   142  		"1ns",
   143  		"30ns",
   144  		"60us",
   145  		"999ms",
   146  		"20s",
   147  		"1m10s",
   148  		"2m",
   149  		"10m",
   150  		"20h",
   151  		"1d",
   152  		"1d1h",
   153  		"7d",
   154  		"8d",
   155  		"30d",
   156  		"31d",
   157  		"300d",
   158  		"-1ns",
   159  		"-30ns",
   160  		"-60us",
   161  		"-999ms",
   162  		"-20s",
   163  		"-1m10s",
   164  		"-2m",
   165  		"-10m",
   166  		"-20h",
   167  		"-1d",
   168  		"-1d1h",
   169  		"-7d",
   170  		"-8d",
   171  		"-30d",
   172  		"-31d",
   173  		"-300d",
   174  	}
   175  
   176  	for _, input := range inputs {
   177  		d, err := ParseExtendedDuration(input)
   178  		require.NoError(t, err)
   179  		res := ToExtendedString(d)
   180  		require.Equal(t, input, res)
   181  	}
   182  }