1// Copyright (c) 2021 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 21package atomic 22 23import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 "github.com/stretchr/testify/require" 29) 30 31func TestTime(t *testing.T) { 32 start := time.Date(2021, 6, 17, 9, 10, 0, 0, time.UTC) 33 atom := NewTime(start) 34 35 require.Equal(t, start, atom.Load(), "Load didn't work") 36 require.Equal(t, time.Time{}, NewTime(time.Time{}).Load(), "Default time value is wrong") 37} 38 39func TestTimeLocation(t *testing.T) { 40 // Check TZ data hasn't been lost from load/store. 41 ny, err := time.LoadLocation("America/New_York") 42 require.NoError(t, err, "Failed to load location") 43 nyTime := NewTime(time.Date(2021, 1, 1, 0, 0, 0, 0, ny)) 44 45 var atom Time 46 atom.Store(nyTime.Load()) 47 48 assert.Equal(t, ny, atom.Load().Location(), "Location information is wrong") 49} 50 51func TestLargeTime(t *testing.T) { 52 // Check "large/small" time that are beyond int64 ns 53 // representation (< year 1678 or > year 2262) can be 54 // correctly load/store'd. 55 t.Parallel() 56 57 t.Run("future", func(t *testing.T) { 58 future := time.Date(2262, 12, 31, 0, 0, 0, 0, time.UTC) 59 atom := NewTime(future) 60 dayAfterFuture := atom.Load().AddDate(0, 1, 0) 61 62 atom.Store(dayAfterFuture) 63 assert.Equal(t, 2263, atom.Load().Year()) 64 }) 65 66 t.Run("past", func(t *testing.T) { 67 past := time.Date(1678, 1, 1, 0, 0, 0, 0, time.UTC) 68 atom := NewTime(past) 69 dayBeforePast := atom.Load().AddDate(0, -1, 0) 70 71 atom.Store(dayBeforePast) 72 assert.Equal(t, 1677, atom.Load().Year()) 73 }) 74} 75 76func TestMonotonic(t *testing.T) { 77 before := NewTime(time.Now()) 78 time.Sleep(15 * time.Millisecond) 79 after := NewTime(time.Now()) 80 81 // try loading/storing before and test monotonic clock value hasn't been lost 82 bt := before.Load() 83 before.Store(bt) 84 d := after.Load().Sub(before.Load()) 85 assert.True(t, 15 <= d.Milliseconds()) 86} 87