diff --git a/CHANGELOG.md b/CHANGELOG.md index fd57533..d082565 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Added - Add `Float64.Swap` to match int atomic operations. +- Add `atomic.Time` type for atomic operations on `time.Time` values. ## [1.8.0] - 2021-06-09 ### Added diff --git a/stress_test.go b/stress_test.go index da9969b..0ac7ac5 100644 --- a/stress_test.go +++ b/stress_test.go @@ -27,6 +27,7 @@ import ( "sync" "sync/atomic" "testing" + "time" ) const ( @@ -48,6 +49,7 @@ var _stressTests = map[string]func() func(){ "string": stressString, "duration": stressDuration, "error": stressError, + "time": stressTime, } func TestStress(t *testing.T) { @@ -272,3 +274,16 @@ func stressError() func() { atom.Store(nil) } } + +func stressTime() func() { + var atom = NewTime(time.Date(2021, 6, 17, 9, 0, 0, 0, time.UTC)) + var dayAgo = time.Date(2021, 6, 16, 9, 0, 0, 0, time.UTC) + var weekAgo = time.Date(2021, 6, 10, 9, 0, 0, 0, time.UTC) + return func() { + atom.Load() + atom.Store(dayAgo) + atom.Load() + atom.Store(weekAgo) + atom.Store(time.Time{}) + } +} diff --git a/time.go b/time.go new file mode 100644 index 0000000..33460fc --- /dev/null +++ b/time.go @@ -0,0 +1,55 @@ +// @generated Code generated by gen-atomicwrapper. + +// Copyright (c) 2020-2021 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package atomic + +import ( + "time" +) + +// Time is an atomic type-safe wrapper for time.Time values. +type Time struct { + _ nocmp // disallow non-atomic comparison + + v Value +} + +var _zeroTime time.Time + +// NewTime creates a new Time. +func NewTime(val time.Time) *Time { + x := &Time{} + if val != _zeroTime { + x.Store(val) + } + return x +} + +// Load atomically loads the wrapped time.Time. +func (x *Time) Load() time.Time { + return unpackTime(x.v.Load()) +} + +// Store atomically stores the passed time.Time. +func (x *Time) Store(val time.Time) { + x.v.Store(packTime(val)) +} diff --git a/time_ext.go b/time_ext.go new file mode 100644 index 0000000..1e3dc97 --- /dev/null +++ b/time_ext.go @@ -0,0 +1,36 @@ +// Copyright (c) 2021 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package atomic + +import "time" + +//go:generate bin/gen-atomicwrapper -name=Time -type=time.Time -wrapped=Value -pack=packTime -unpack=unpackTime -imports time -file=time.go + +func packTime(t time.Time) interface{} { + return t +} + +func unpackTime(v interface{}) time.Time { + if t, ok := v.(time.Time); ok { + return t + } + return time.Time{} +} diff --git a/time_test.go b/time_test.go new file mode 100644 index 0000000..83ac022 --- /dev/null +++ b/time_test.go @@ -0,0 +1,86 @@ +// Copyright (c) 2021 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package atomic + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestTime(t *testing.T) { + start := time.Date(2021, 6, 17, 9, 10, 0, 0, time.UTC) + atom := NewTime(start) + + require.Equal(t, start, atom.Load(), "Load didn't work") + require.Equal(t, time.Time{}, NewTime(time.Time{}).Load(), "Default time value is wrong") +} + +func TestTimeLocation(t *testing.T) { + // Check TZ data hasn't been lost from load/store. + ny, err := time.LoadLocation("America/New_York") + require.NoError(t, err, "Failed to load location") + nyTime := NewTime(time.Date(2021, 1, 1, 0, 0, 0, 0, ny)) + + var atom Time + atom.Store(nyTime.Load()) + + assert.Equal(t, ny, atom.Load().Location(), "Location information is wrong") +} + +func TestLargeTime(t *testing.T) { + // Check "large/small" time that are beyond int64 ns + // representation (< year 1678 or > year 2262) can be + // correctly load/store'd. + t.Parallel() + + t.Run("future", func(t *testing.T) { + future := time.Date(2262, 12, 31, 0, 0, 0, 0, time.UTC) + atom := NewTime(future) + dayAfterFuture := atom.Load().AddDate(0, 1, 0) + + atom.Store(dayAfterFuture) + assert.Equal(t, 2263, atom.Load().Year()) + }) + + t.Run("past", func(t *testing.T) { + past := time.Date(1678, 1, 1, 0, 0, 0, 0, time.UTC) + atom := NewTime(past) + dayBeforePast := atom.Load().AddDate(0, -1, 0) + + atom.Store(dayBeforePast) + assert.Equal(t, 1677, atom.Load().Year()) + }) +} + +func TestMonotonic(t *testing.T) { + before := NewTime(time.Now()) + time.Sleep(15 * time.Millisecond) + after := NewTime(time.Now()) + + // try loading/storing before and test monotonic clock value hasn't been lost + bt := before.Load() + before.Store(bt) + d := after.Load().Sub(before.Load()) + assert.True(t, 15 <= d.Milliseconds()) +}