2019-04-01 21:14:58 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-04-23 09:47:16 +01:00
|
|
|
package orders_test
|
2019-04-01 21:14:58 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
2019-04-01 21:14:58 +01:00
|
|
|
"storj.io/storj/satellite"
|
2019-04-04 15:42:01 +01:00
|
|
|
"storj.io/storj/satellite/orders"
|
2019-04-01 21:14:58 +01:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSerialNumbers(t *testing.T) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
2019-04-04 15:42:01 +01:00
|
|
|
ordersDB := db.Orders()
|
2019-04-01 21:14:58 +01:00
|
|
|
|
|
|
|
expectedBucket := []byte("bucketID")
|
2020-03-10 22:05:01 +00:00
|
|
|
err := ordersDB.CreateSerialInfo(ctx, storj.SerialNumber{1}, expectedBucket, time.Now())
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
bucketID, err := ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket, bucketID)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
// try to use used serial number
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.Error(t, err)
|
2019-04-04 15:42:01 +01:00
|
|
|
require.True(t, orders.ErrUsingSerialNumber.Has(err))
|
2019-04-01 21:14:58 +01:00
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
err = ordersDB.UnuseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-04-04 15:42:01 +01:00
|
|
|
bucketID, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
2019-04-01 21:14:58 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectedBucket, bucketID)
|
2019-04-04 15:42:01 +01:00
|
|
|
|
|
|
|
// not existing serial number
|
|
|
|
bucketID, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{99}, storj.NodeID{1})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, orders.ErrUsingSerialNumber.Has(err))
|
|
|
|
require.Empty(t, bucketID)
|
2019-08-27 18:12:38 +01:00
|
|
|
|
2020-11-17 16:00:56 +00:00
|
|
|
deleted, err := ordersDB.DeleteExpiredSerials(ctx, time.Now(), nil)
|
2019-08-27 18:12:38 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, deleted, 1)
|
|
|
|
|
|
|
|
// check serial number has been deleted from serial_numbers and used_serials
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
|
|
|
require.EqualError(t, err, "serial number: serial number not found")
|
2019-04-01 21:14:58 +01:00
|
|
|
})
|
|
|
|
}
|
2020-11-17 16:00:56 +00:00
|
|
|
|
|
|
|
func TestDeleteExpiredWithOptions(t *testing.T) {
|
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
|
|
|
ordersDB := db.Orders()
|
|
|
|
|
|
|
|
err := ordersDB.CreateSerialInfo(ctx, storj.SerialNumber{1}, []byte("bucketID1"), time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ordersDB.CreateSerialInfo(ctx, storj.SerialNumber{2}, []byte("bucketID2"), time.Now())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
options := &orders.SerialDeleteOptions{
|
|
|
|
BatchSize: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
deleted, err := ordersDB.DeleteExpiredSerials(ctx, time.Now(), options)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, 2, deleted)
|
|
|
|
|
|
|
|
// check serial number has been deleted from serial_numbers and used_serials
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{1}, storj.NodeID{1})
|
|
|
|
require.EqualError(t, err, "serial number: serial number not found")
|
|
|
|
|
|
|
|
_, err = ordersDB.UseSerialNumber(ctx, storj.SerialNumber{2}, storj.NodeID{1})
|
|
|
|
require.EqualError(t, err, "serial number: serial number not found")
|
|
|
|
})
|
|
|
|
}
|