Skip to content

Commit 57854ba

Browse files
committed
allow inserting a custom function for replacing time.Now in hashing tests
1 parent 58654cd commit 57854ba

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

configtype/time.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ import (
1515
// when a time type is required. We wrap the time.Time type so that
1616
// the spec can be extended in the future to support other types of times
1717
type Time struct {
18-
input string
19-
time time.Time
20-
duration *timeDuration
18+
input string
19+
time time.Time
20+
duration *timeDuration
21+
hashNowFunc func() time.Time
2122
}
2223

2324
func ParseTime(s string) (Time, error) {
@@ -131,10 +132,18 @@ func (t Time) String() string {
131132
}
132133

133134
func (t Time) Hash() (uint64, error) {
134-
at := t.AsTime(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
135+
nowFunc := t.hashNowFunc
136+
if nowFunc == nil {
137+
nowFunc = time.Now
138+
}
139+
at := t.AsTime(nowFunc())
135140
return uint64(at.UnixNano()), nil
136141
}
137142

143+
func (t *Time) SetHashNowFunc(f func() time.Time) {
144+
t.hashNowFunc = f
145+
}
146+
138147
type timeDuration struct {
139148
input string
140149

configtype/time_test.go

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -154,40 +154,54 @@ func TestTime_JSONSchema(t *testing.T) {
154154
}
155155

156156
func TestTime_Hashing(t *testing.T) {
157+
hnf := func() time.Time { return time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) }
157158
cases := []struct {
158-
a any
159-
b any
160-
equal bool
159+
a configtype.Time
160+
b configtype.Time
161+
hashNowFunc func() time.Time
162+
equal bool
161163
}{
162164
{
163-
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
164-
b: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
165-
equal: true,
165+
a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(),
166+
b: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(),
167+
hashNowFunc: hnf,
168+
equal: true,
166169
},
167170
{
168-
a: func() any { ct, _ := configtype.ParseTime("10m"); return ct }(),
169-
b: func() any { ct, _ := configtype.ParseTime("1m"); return ct }(),
171+
a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(),
172+
b: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(),
170173
equal: false,
171174
},
172175
{
173-
a: func() any { ct, _ := configtype.ParseTime("2021-09-01T00:00:00Z"); return ct }(),
174-
b: func() any { ct, _ := configtype.ParseTime("2012-01-02T01:02:03Z"); return ct }(),
176+
a: func() configtype.Time { ct, _ := configtype.ParseTime("10m"); return ct }(),
177+
b: func() configtype.Time { ct, _ := configtype.ParseTime("1m"); return ct }(),
175178
equal: false,
176179
},
177180
{
178-
a: func() any { ct, _ := configtype.ParseTime("-50m30s"); return ct }(),
179-
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds ago"); return ct }(),
180-
equal: true,
181+
a: func() configtype.Time { ct, _ := configtype.ParseTime("2021-09-01T00:00:00Z"); return ct }(),
182+
b: func() configtype.Time { ct, _ := configtype.ParseTime("2012-01-02T01:02:03Z"); return ct }(),
183+
hashNowFunc: hnf,
184+
equal: false,
181185
},
182186
{
183-
a: func() any { ct, _ := configtype.ParseTime("50m30s"); return ct }(),
184-
b: func() any { ct, _ := configtype.ParseTime("50 minutes 30 seconds from now"); return ct }(),
185-
equal: true,
187+
a: func() configtype.Time { ct, _ := configtype.ParseTime("-50m30s"); return ct }(),
188+
b: func() configtype.Time { ct, _ := configtype.ParseTime("50 minutes 30 seconds ago"); return ct }(),
189+
hashNowFunc: hnf,
190+
equal: true,
191+
},
192+
{
193+
a: func() configtype.Time { ct, _ := configtype.ParseTime("50m30s"); return ct }(),
194+
b: func() configtype.Time { ct, _ := configtype.ParseTime("50 minutes 30 seconds from now"); return ct }(),
195+
hashNowFunc: hnf,
196+
equal: true,
186197
},
187198
}
188199
for _, tc := range cases {
200+
tc.a.SetHashNowFunc(tc.hashNowFunc)
189201
hashA, err := hashstructure.Hash(tc.a, hashstructure.FormatV2, nil)
190202
require.NoError(t, err)
203+
time.Sleep(1 * time.Second)
204+
tc.b.SetHashNowFunc(tc.hashNowFunc)
191205
hashB, err := hashstructure.Hash(tc.b, hashstructure.FormatV2, nil)
192206
require.NoError(t, err)
193207

0 commit comments

Comments
 (0)