satellite/accounting: fix build - time rounding

We were seeing error on the last day of the month with TestProjectAllocatedBandwidthRetainTwo.
This is due to AddDate normalizes its result in the same way that Date does, so, for example,
adding one month to October 31 yields December 1, the normalized form for November 31."

I also fixed a minor UTC issue with this test as well.

Change-Id: I0157873e7befa57810e5f264a922b188890fa46a
This commit is contained in:
Bill Thorp 2020-08-31 09:37:13 -04:00
parent e072febbcc
commit 328004c0ef
2 changed files with 10 additions and 10 deletions

View File

@ -66,8 +66,8 @@ func (chore *Chore) RunOnce(ctx context.Context) (err error) {
return errs.New("retain months cannot be less than 0")
}
beforeTime := time.Now().UTC().AddDate(0, -chore.config.RetainMonths, 0)
beforeMonth := time.Date(beforeTime.Year(), beforeTime.Month(), 1, 0, 0, 0, 0, time.UTC)
now := time.Now().UTC()
beforeMonth := time.Date(now.Year(), now.Month()-time.Month(chore.config.RetainMonths), 1, 0, 0, 0, 0, time.UTC)
return chore.db.DeleteProjectAllocatedBandwidthBefore(ctx, beforeMonth)
}

View File

@ -54,16 +54,16 @@ func testProjectAllocatedBandwidthRetain(t *testing.T, retain int) {
projectID := testrand.UUID()
bucketName := testrand.BucketName()
now := time.Now()
interval := time.Date(now.Year(), now.Month(), 15, 12, 0, 0, 0, time.UTC)
now := time.Now().UTC()
for i := 0; i <= months; i++ {
err := ordersDB.UpdateBucketBandwidthAllocation(ctx, projectID, []byte(bucketName), pb.PieceAction_GET, testBytes, interval.AddDate(0, -i, 0))
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
err := ordersDB.UpdateBucketBandwidthAllocation(ctx, projectID, []byte(bucketName), pb.PieceAction_GET, testBytes, newDate)
require.NoError(t, err)
}
for i := 0; i <= months; i++ {
m := interval.AddDate(0, -i, 0)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, m.Year(), m.Month())
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, newDate.Year(), newDate.Month())
require.NoError(t, err)
require.EqualValues(t, testBytes, bytes)
}
@ -71,12 +71,12 @@ func testProjectAllocatedBandwidthRetain(t *testing.T, retain int) {
satellite.Accounting.ProjectBWCleanup.Loop.TriggerWait()
for i := 0; i <= months; i++ {
m := interval.AddDate(0, -i, 0)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, m.Year(), m.Month())
newDate := time.Date(now.Year(), now.Month()-time.Month(i), 15, 12, 0, 0, 0, time.UTC)
bytes, err := satellite.Accounting.ProjectUsage.GetProjectAllocatedBandwidth(ctx, projectID, newDate.Year(), newDate.Month())
if i < months || retain < 0 { // there should always be the current month
require.NoError(t, err)
require.EqualValues(t, testBytes, bytes)
require.EqualValues(t, testBytes, bytes, "Month: %d", i)
} else {
require.NoError(t, err)
require.EqualValues(t, 0, bytes)