storj/satellite/reputation/writecache_unit_test.go

52 lines
1.7 KiB
Go
Raw Normal View History

// 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)
startOfHour := time.Now().Truncate(time.Hour)
now := startOfHour.Add(15 * time.Minute)
nextTime := nextTimeForSync(zeroesID, halfwayID, now, time.Hour)
requireInDeltaTime(t, startOfHour.Add(30*time.Minute), nextTime, time.Second)
nextTime = nextTimeForSync(halfwayID, zeroesID, now, time.Hour)
requireInDeltaTime(t, startOfHour.Add(30*time.Minute), nextTime, time.Second)
nextTime = nextTimeForSync(zeroesID, zeroesID, now, time.Hour)
requireInDeltaTime(t, startOfHour.Add(time.Hour), nextTime, time.Second)
nextTime = nextTimeForSync(halfwayID, halfwayID, now, time.Hour)
requireInDeltaTime(t, startOfHour.Add(time.Hour), nextTime, time.Second)
nextTime = nextTimeForSync(quarterwayID, halfwayID, now, time.Hour)
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)
}