1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package atomic
22
23 import (
24 "testing"
25 "time"
26
27 "github.com/stretchr/testify/assert"
28 "github.com/stretchr/testify/require"
29 )
30
31 func 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
39 func TestTimeLocation(t *testing.T) {
40
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
51 func TestLargeTime(t *testing.T) {
52
53
54
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
76 func TestMonotonic(t *testing.T) {
77 before := NewTime(time.Now())
78 time.Sleep(15 * time.Millisecond)
79 after := NewTime(time.Now())
80
81
82 bt := before.Load()
83 before.Store(bt)
84 d := after.Load().Sub(before.Load())
85 assert.True(t, 15 <= d.Milliseconds())
86 }
87
View as plain text