storj/satellite/satellitedb/orders_test.go
Michal Niewrzal 3b6e1123b8 satellite/orders: fix sorting rollups before inserting
Sorting by primary key before inserting data into DB is fixed.
Earlier we were sorting input slice of BucketBandwidthRollup but then
we were putting all entries into map to rollup input data. Iteration
over map with a range loop doesn't guarantee any specific order so we
were loosing sorted order when we were creating with this map slices to
use with DB insert.

New code is also using map but when map is full its sorting map keys
separately and iterates over them to get data from map.

https://github.com/storj/storj/issues/5332

Change-Id: I5bf09489b0eecb6858bf854ab387b660124bf53f
2023-02-01 12:17:25 +00:00

88 lines
1.7 KiB
Go

// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"storj.io/common/pb"
"storj.io/common/uuid"
)
func TestSortRollupKeys(t *testing.T) {
rollups := []bandwidthRollupKey{
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 1,
Action: pb.PieceAction_GET, // GET is 2
},
{
ProjectID: uuid.UUID{2},
BucketName: "a",
IntervalStart: 2,
Action: pb.PieceAction_GET,
},
{
ProjectID: uuid.UUID{1},
BucketName: "b",
IntervalStart: 3,
Action: pb.PieceAction_GET,
},
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 4,
Action: pb.PieceAction_GET_AUDIT,
},
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 5,
Action: pb.PieceAction_GET,
},
}
expRollups := []bandwidthRollupKey{
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 1,
Action: pb.PieceAction_GET, // GET is 2
},
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 4,
Action: pb.PieceAction_GET_AUDIT,
},
{
ProjectID: uuid.UUID{1},
BucketName: "a",
IntervalStart: 5,
Action: pb.PieceAction_GET,
},
{
ProjectID: uuid.UUID{2},
BucketName: "a",
IntervalStart: 2,
Action: pb.PieceAction_GET,
},
{
ProjectID: uuid.UUID{1},
BucketName: "b",
IntervalStart: 3,
Action: pb.PieceAction_GET,
},
}
assert.NotEqual(t, expRollups, rollups)
sortBandwidthRollupKeys(rollups)
assert.Empty(t, cmp.Diff(expRollups, rollups))
}