|
4 | 4 | "testing"
|
5 | 5 | "time"
|
6 | 6 |
|
| 7 | + gcmp "github.com/google/go-cmp/cmp" |
7 | 8 | "golang.org/x/sync/errgroup"
|
8 | 9 | "gotest.tools/v3/assert"
|
9 | 10 | "gotest.tools/v3/assert/cmp"
|
@@ -132,7 +133,11 @@ func TestProvider_Record(t *testing.T) {
|
132 | 133 | m.record(aMet.Type, aMet.Name, aMet.Value, aMet.Tags)
|
133 | 134 | }
|
134 | 135 |
|
135 |
| - assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData)) |
| 136 | + for i := range tt.expectedMetricsData { |
| 137 | + tt.expectedMetricsData[i].Timestamp = time.Now().Unix() |
| 138 | + } |
| 139 | + |
| 140 | + assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData, equateApproxUnixTime(1))) |
136 | 141 | })
|
137 | 142 | }
|
138 | 143 | }
|
@@ -255,7 +260,11 @@ func TestProvider_Publish(t *testing.T) {
|
255 | 260 | err := eg.Wait()
|
256 | 261 | assert.NilError(t, err)
|
257 | 262 |
|
258 |
| - assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData)) |
| 263 | + for i := range tt.expectedMetricsData { |
| 264 | + tt.expectedMetricsData[i].Timestamp = time.Now().Unix() |
| 265 | + } |
| 266 | + |
| 267 | + assert.Check(t, cmp.DeepEqual(m.data, tt.expectedMetricsData, equateApproxUnixTime(1))) |
259 | 268 | })
|
260 | 269 | }
|
261 | 270 | }
|
@@ -353,3 +362,28 @@ func TestProvider_New(t *testing.T) {
|
353 | 362 | })
|
354 | 363 | }
|
355 | 364 | }
|
| 365 | + |
| 366 | +func equateApproxUnixTime(marginSec int64) gcmp.Option { |
| 367 | + if marginSec < 0 { |
| 368 | + panic("margin must be a non-negative number") |
| 369 | + } |
| 370 | + a := timeApproximator{marginSec} |
| 371 | + return gcmp.FilterValues(areNonZeroTimes, gcmp.Comparer(a.compare)) |
| 372 | +} |
| 373 | + |
| 374 | +func areNonZeroTimes(x, y int64) bool { |
| 375 | + return x > 0 && y > 0 |
| 376 | +} |
| 377 | + |
| 378 | +type timeApproximator struct { |
| 379 | + marginSec int64 |
| 380 | +} |
| 381 | + |
| 382 | +func (a timeApproximator) compare(x, y int64) bool { |
| 383 | + if x > y { |
| 384 | + // Ensure x is always before y |
| 385 | + x, y = y, x |
| 386 | + } |
| 387 | + // We're within the margin if x+margin >= y. |
| 388 | + return x+a.marginSec >= y |
| 389 | +} |
0 commit comments