Skip to content

Commit

Permalink
More false positive prevention in new time tests.
Browse files Browse the repository at this point in the history
This commit extends the same logic in the previous commit to the
comparison of offsets returned from the median time source in the tests.

In particular it modifies the tests to allow for the offsets to differ by
a second due to a boundary condition to prevent false positives.
  • Loading branch information
davecgh committed Oct 11, 2014
1 parent b4c55ae commit 6af9302
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 2 additions & 1 deletion mediantime.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ func (m *medianTime) AddTimeSample(sourceID string, timeVal time.Time) {
// of offsets while respecting the maximum number of allowed entries by
// replacing the oldest entry with the new entry once the maximum number
// of entries is reached.
offsetSecs := int64(time.Now().Sub(timeVal).Seconds())
now := time.Unix(time.Now().Unix(), 0)
offsetSecs := int64(now.Sub(timeVal).Seconds())
numOffsets := len(m.offsets)
if numOffsets == maxMedianTimeEntries && maxMedianTimeEntries > 0 {
m.offsets = m.offsets[1:]
Expand Down
13 changes: 9 additions & 4 deletions mediantime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func TestMedianTime(t *testing.T) {
filter := btcchain.NewMedianTime()
for j, offset := range test.in {
id := strconv.Itoa(j)
tOffset := time.Now().Add(time.Duration(offset) *
time.Second)
now := time.Unix(time.Now().Unix(), 0)
tOffset := now.Add(time.Duration(offset) * time.Second)
filter.AddTimeSample(id, tOffset)

// Ensure the duplicate IDs are ignored.
Expand All @@ -76,11 +76,16 @@ func TestMedianTime(t *testing.T) {
}
}

// Since it is possible that the time.Now call in AddTimeSample
// and the time.Now calls here in the tests will be off by one
// second, allow a fudge factor to compensate.
gotOffset := filter.Offset()
wantOffset := time.Duration(test.wantOffset) * time.Second
if gotOffset != wantOffset {
wantOffset2 := time.Duration(test.wantOffset-1) * time.Second
if gotOffset != wantOffset && gotOffset != wantOffset2 {
t.Errorf("Offset #%d: unexpected offset -- got %v, "+
"want %v", i, gotOffset, wantOffset)
"want %v or %v", i, gotOffset, wantOffset,
wantOffset2)
continue
}

Expand Down

0 comments on commit 6af9302

Please sign in to comment.