2022-05-07 20:04:12 +01:00
|
|
|
// Copyright (C) 2022 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"storj.io/common/storj"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNextTimeForSync(t *testing.T) {
|
|
|
|
var zeroesID storj.NodeID
|
|
|
|
binary.BigEndian.PutUint64(zeroesID[:8], 0) // unnecessary, but for clarity
|
|
|
|
|
|
|
|
var halfwayID storj.NodeID
|
|
|
|
binary.BigEndian.PutUint64(halfwayID[:8], 1<<63)
|
|
|
|
|
|
|
|
var quarterwayID storj.NodeID
|
|
|
|
binary.BigEndian.PutUint64(quarterwayID[:8], 1<<62)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
const (
|
|
|
|
zeroOffset = 0
|
|
|
|
halfwayOffset = 1 << 63
|
|
|
|
quarterwayOffset = 1 << 62
|
|
|
|
)
|
|
|
|
|
2022-05-07 20:04:12 +01:00
|
|
|
startOfHour := time.Now().Truncate(time.Hour)
|
|
|
|
now := startOfHour.Add(15 * time.Minute)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
nextTime := nextTimeForSync(zeroOffset, halfwayID, now, time.Hour)
|
2022-05-07 20:04:12 +01:00
|
|
|
requireInDeltaTime(t, startOfHour.Add(30*time.Minute), nextTime, time.Second)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
nextTime = nextTimeForSync(halfwayOffset, zeroesID, now, time.Hour)
|
2022-05-07 20:04:12 +01:00
|
|
|
requireInDeltaTime(t, startOfHour.Add(30*time.Minute), nextTime, time.Second)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
nextTime = nextTimeForSync(zeroOffset, zeroesID, now, time.Hour)
|
2022-05-07 20:04:12 +01:00
|
|
|
requireInDeltaTime(t, startOfHour.Add(time.Hour), nextTime, time.Second)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
nextTime = nextTimeForSync(halfwayOffset, halfwayID, now, time.Hour)
|
2022-05-07 20:04:12 +01:00
|
|
|
requireInDeltaTime(t, startOfHour.Add(time.Hour), nextTime, time.Second)
|
|
|
|
|
2022-07-28 00:43:02 +01:00
|
|
|
nextTime = nextTimeForSync(quarterwayOffset, halfwayID, now, time.Hour)
|
2022-05-07 20:04:12 +01:00
|
|
|
requireInDeltaTime(t, startOfHour.Add(45*time.Minute), nextTime, time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func requireInDeltaTime(t *testing.T, expected time.Time, actual time.Time, delta time.Duration) {
|
|
|
|
if delta < 0 {
|
|
|
|
delta = -delta
|
|
|
|
}
|
|
|
|
require.Falsef(t, actual.Before(expected.Add(-delta)), "%s is not within %s of %s", actual, delta, expected)
|
|
|
|
require.Falsef(t, actual.After(expected.Add(delta)), "%s is not within %s of %s", actual, delta, expected)
|
|
|
|
}
|