storj/satellite/reputation/audithistory_test.go
paul cannon 2ee9463e34 satellite/reputation: don't need 3 identical AuditHistory types
The two protobuf types are identical except that one is in our common/pb
package, and the other is in internalpb. Since the type is public
already, and there is no difference in the internal one, it seems better
to use the public one for all satellite needs.

There is also another type which is essentially identical, but which is
not a protobuf type, also called "AuditHistory". It looks like we don't
ever actually need to have a separate type from the protobuf one.

This change makes us use "storj/common/pb".AuditHistory for all of our
AuditHistory needs.

Refs: https://github.com/storj/storj/issues/4601

Change-Id: If845fde21bb31c801db6d67ffc9a146d1617b991
2022-05-24 05:48:46 +00:00

79 lines
2.9 KiB
Go

// Copyright (C) 2021 Storj Labs, Inc.
// See LICENSE for copying information.
package reputation_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"storj.io/common/pb"
"storj.io/storj/satellite/reputation"
)
func TestAddAuditToHistory(t *testing.T) {
config := reputation.AuditHistoryConfig{
WindowSize: time.Hour,
TrackingPeriod: 2 * time.Hour,
GracePeriod: time.Hour,
OfflineThreshold: 0.6,
OfflineDQEnabled: true,
OfflineSuspensionEnabled: true,
}
startingWindow := time.Now().Truncate(time.Hour)
windowsInTrackingPeriod := int(config.TrackingPeriod.Seconds() / config.WindowSize.Seconds())
currentWindow := startingWindow
history := &pb.AuditHistory{}
// online score should be 1 until the first window is finished
err := reputation.AddAuditToHistory(history, false, currentWindow.Add(2*time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, 1, history.Score)
err = reputation.AddAuditToHistory(history, true, currentWindow.Add(20*time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, 1, history.Score)
// move to next window
currentWindow = currentWindow.Add(time.Hour)
// online score should be now be 0.5 since the first window is complete with one online audit and one offline audit
err = reputation.AddAuditToHistory(history, false, currentWindow.Add(2*time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, 0.5, history.Score)
err = reputation.AddAuditToHistory(history, true, currentWindow.Add(20*time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, 0.5, history.Score)
// move to next window
currentWindow = currentWindow.Add(time.Hour)
// try to add an audit for an old window, expect error
err = reputation.AddAuditToHistory(history, true, startingWindow, config)
require.Error(t, err)
// add another online audit for the latest window; score should still be 0.5
err = reputation.AddAuditToHistory(history, true, currentWindow, config)
require.NoError(t, err)
require.EqualValues(t, 0.5, history.Score)
// add another online audit for the latest window; score should still be 0.5
err = reputation.AddAuditToHistory(history, true, currentWindow.Add(45*time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, 0.5, history.Score)
currentWindow = currentWindow.Add(time.Hour)
// in the current state, there are windowsInTrackingPeriod windows with a score of 0.5
// and one window with a score of 1.0. The Math below calculates the new score when the latest
// window gets included in the tracking period, and the earliest 0.5 window gets dropped.
expectedScore := (0.5*float64(windowsInTrackingPeriod-1) + 1) / float64(windowsInTrackingPeriod)
// add online audit for next window; score should now be expectedScore
err = reputation.AddAuditToHistory(history, true, currentWindow.Add(time.Minute), config)
require.NoError(t, err)
require.EqualValues(t, expectedScore, history.Score)
}