Debian Codegraph @

timestamp_test.go

raw
1 package uuid
2
3 import (
4 "github.com/stretchr/testify/assert"
5 "sync"
6 "sync/atomic"
7 "testing"
8 "time"
9 )
10
11 func TestEpoch(t *testing.T) {
12 assert.True(t, gregorianToUNIXOffset == 0x01B21DD213814000)
13 assert.True(t, gregorianToUNIXOffset == 122192928000000000)
14 }
15
16 func TestNow(t *testing.T) {
17 assert.True(t, Now() > gregorianToUNIXOffset)
18 }
19
20 func TestTimestamp_Add(t *testing.T) {
21 now := Now()
22 assert.True(t, now.Add(time.Second) == now+Timestamp((time.Second/100)), "The times should be equal")
23 }
24
25 func TestTimestamp_String(t *testing.T) {
26 now := Now()
27 nowTime := now.Time()
28 assert.Equal(t, now.String(), nowTime.String(), "The time strings should be equal")
29 }
30
31 func TestTimestamp_Sub(t *testing.T) {
32 now := Now()
33 assert.True(t, now.Sub(time.Second) == now-Timestamp((time.Second/100)), "The times should be equal")
34 }
35
36 func TestTimestamp_Time(t *testing.T) {
37
38 assert.True(t, Now().Time().Location() == time.UTC)
39
40 }
41
42 func TestSpinnerNext(t *testing.T) {
43 size := defaultSpinResolution * 1
44
45 spin := spinner{}
46 spin.Resolution = defaultSpinResolution
47
48 times := make([]Timestamp, size)
49
50 for i := 0; i < size; i++ {
51 times[i] = spin.next()
52 }
53
54 for j := size - 1; j >= 0; j-- {
55 for k := 0; k < size; k++ {
56 if k == j {
57 continue
58 }
59 assert.NotEqual(t, "Timestamps should never be equal", times[j], times[k])
60 }
61 }
62
63 spin = spinner{
64 Count: defaultSpinResolution - 1,
65 Timestamp: Now(),
66 Resolution: 1024,
67 }
68
69 for i := 0; i < size; i++ {
70 times[i] = spin.next()
71 }
72
73 for j := size - 1; j >= 0; j-- {
74 for k := 0; k < size; k++ {
75 if k == j {
76 continue
77 }
78 assert.NotEqual(t, "Timestamps should never be equal", times[j], times[k])
79 }
80 }
81
82 spin = spinner{
83 Count: 0,
84 Timestamp: Now(),
85 Resolution: 1,
86 }
87
88 for i := 0; i < size; i++ {
89 times[i] = spin.next()
90 }
91
92 for j := size - 1; j >= 0; j-- {
93 for k := 0; k < size; k++ {
94 if k == j {
95 continue
96 }
97 assert.NotEqual(t, "Timestamps should never be equal", times[j], times[k])
98 }
99 }
100
101 waitSize := 3
102
103 spin = spinner{}
104 spin.Resolution = defaultSpinResolution
105
106 times = make([]Timestamp, size)
107
108 var wg sync.WaitGroup
109 wg.Add(waitSize)
110 mutex := &sync.Mutex{}
111
112 var index int32
113
114 for i := 0; i < waitSize; i++ {
115 go func() {
116 defer wg.Done()
117 for {
118 mutex.Lock()
119 j := atomic.LoadInt32(&index)
120 atomic.AddInt32(&index, 1)
121 if j >= int32(size) {
122 mutex.Unlock()
123 break
124 }
125 times[j] = spin.next()
126 mutex.Unlock()
127 }
128 }()
129 }
130 wg.Wait()
131
132 for j := size - 1; j >= 0; j-- {
133 for k := 0; k < size; k++ {
134 if k == j {
135 continue
136 }
137 assert.NotEqual(t, "Timestamps should never be equal", times[j], times[k])
138 }
139 }
140 }