Skip to content

Commit

Permalink
storage: migrate tests to testify
Browse files Browse the repository at this point in the history
Signed-off-by: David <8039876+AmoebaProtozoa@users.noreply.github.com>
  • Loading branch information
AmoebaProtozoa committed Jun 21, 2022
1 parent 9ff8090 commit ffe8a18
Showing 1 changed file with 123 additions and 131 deletions.
254 changes: 123 additions & 131 deletions server/storage/storage_gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,260 +22,215 @@ import (
"path"
"sort"
"strconv"
"testing"
"time"

. "github.com/pingcap/check"
"github.com/stretchr/testify/suite"
"github.com/tikv/pd/pkg/tempurl"
"github.com/tikv/pd/server/storage/endpoint"
"github.com/tikv/pd/server/storage/kv"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/embed"
)

var _ = Suite(&testStorageGCSuite{})
func TestStorageGCTestSuite(t *testing.T) {
suite.Run(t, new(StorageGCTestSuite))
}

type testStorageGCSuite struct {
type StorageGCTestSuite struct {
suite.Suite
cfg *embed.Config
etcd *embed.Etcd
storage Storage
}

func (s *testStorageGCSuite) SetUpTest(c *C) {
s.cfg = newTestSingleConfig()
s.etcd = mustNewEmbedEtcd(c, s.cfg)

ep := s.cfg.LCUrls[0].String()
func (suite *StorageGCTestSuite) SetupTest() {
var err error
suite.cfg = newTestSingleConfig()
suite.etcd, err = embed.StartEtcd(suite.cfg)
suite.Require().Nil(err)
ep := suite.cfg.LCUrls[0].String()
client, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
})
c.Assert(err, IsNil)
suite.Require().Nil(err)
rootPath := path.Join("/pd", strconv.FormatUint(100, 10))
s.storage = NewStorageWithEtcdBackend(client, rootPath)
}

func (s *testStorageGCSuite) TearDownTest(c *C) {
if s.etcd != nil {
s.etcd.Close()
}
c.Assert(cleanConfig(s.cfg), IsNil)
}

func testGCSafePoints() ([]uint32, []uint64) {
spaceIDs := []uint32{
100,
200,
300,
400,
500,
}
safePoints := []uint64{
0,
1,
4396,
23333333333,
math.MaxUint64,
}
return spaceIDs, safePoints
suite.storage = NewStorageWithEtcdBackend(client, rootPath)
}

func testServiceSafePoints() ([]uint32, []*endpoint.ServiceSafePoint, []int64) {
spaceIDs := []uint32{
100,
100,
100,
200,
200,
200,
300,
300,
300,
}
serviceSafePoints := []*endpoint.ServiceSafePoint{
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
}
testTTls := make([]int64, 9)
for i := range testTTls {
testTTls[i] = 10
func (suite *StorageGCTestSuite) TearDownTest() {
if suite.etcd != nil {
suite.etcd.Close()
}
return spaceIDs, serviceSafePoints, testTTls
suite.Require().NoError(os.RemoveAll(suite.cfg.Dir))
}

func (s *testStorageGCSuite) TestSaveLoadServiceSafePoint(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestSaveLoadServiceSafePoint() {
storage := suite.storage
testSpaceID, testSafePoints, testTTLs := testServiceSafePoints()
for i := range testSpaceID {
c.Assert(storage.SaveServiceSafePoint(testSpaceID[i], testSafePoints[i], testTTLs[i]), IsNil)
suite.NoError(storage.SaveServiceSafePoint(testSpaceID[i], testSafePoints[i], testTTLs[i]))
}
for i := range testSpaceID {
loadedSafePoint, err := storage.LoadServiceSafePoint(testSpaceID[i], testSafePoints[i].ServiceID)
c.Assert(err, IsNil)
c.Assert(loadedSafePoint, DeepEquals, testSafePoints[i])
suite.Nil(err)
suite.Equal(testSafePoints[i], loadedSafePoint)
}
}

func (s *testStorageGCSuite) TestLoadMinServiceSafePoint(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestLoadMinServiceSafePoint() {
storage := suite.storage
testTTLs := []int64{2, 6}
serviceSafePoints := []*endpoint.ServiceSafePoint{
{ServiceID: "0", SafePoint: 100},
{ServiceID: "1", SafePoint: 200},
}
testKeySpace := uint32(100)
for i := range serviceSafePoints {
c.Assert(storage.SaveServiceSafePoint(testKeySpace, serviceSafePoints[i], testTTLs[i]), IsNil)
suite.NoError(storage.SaveServiceSafePoint(testKeySpace, serviceSafePoints[i], testTTLs[i]))
}

minSafePoint, err := storage.LoadMinServiceSafePoint(testKeySpace)
c.Assert(err, IsNil)
c.Assert(minSafePoint, DeepEquals, serviceSafePoints[0])
suite.Nil(err)
suite.Equal(serviceSafePoints[0], minSafePoint)

time.Sleep(4 * time.Second)
// the safePoint with ServiceID 0 should be removed due to expiration
// now min should be safePoint with ServiceID 1
minSafePoint2, err := storage.LoadMinServiceSafePoint(testKeySpace)
c.Assert(err, IsNil)
c.Assert(minSafePoint2, DeepEquals, serviceSafePoints[1])
suite.Nil(err)
suite.Equal(serviceSafePoints[1], minSafePoint2)

// verify that service safe point with ServiceID 0 has been removed
ssp, err := storage.LoadServiceSafePoint(testKeySpace, "0")
c.Assert(err, IsNil)
c.Assert(ssp, IsNil)
suite.Nil(err)
suite.Nil(ssp)

time.Sleep(4 * time.Second)
// all remaining service safePoints should be removed due to expiration
ssp, err = storage.LoadMinServiceSafePoint(testKeySpace)
c.Assert(err, IsNil)
c.Assert(ssp, IsNil)
suite.Nil(err)
suite.Nil(ssp)
}

func (s *testStorageGCSuite) TestRemoveServiceSafePoint(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestRemoveServiceSafePoint() {
storage := suite.storage
testSpaceID, testSafePoints, testTTLs := testServiceSafePoints()
// save service safe points
for i := range testSpaceID {
c.Assert(storage.SaveServiceSafePoint(testSpaceID[i], testSafePoints[i], testTTLs[i]), IsNil)
suite.NoError(storage.SaveServiceSafePoint(testSpaceID[i], testSafePoints[i], testTTLs[i]))
}
// remove saved service safe points
for i := range testSpaceID {
c.Assert(storage.RemoveServiceSafePoint(testSpaceID[i], testSafePoints[i].ServiceID), IsNil)
suite.NoError(storage.RemoveServiceSafePoint(testSpaceID[i], testSafePoints[i].ServiceID))
}
// check that service safe points are empty
for i := range testSpaceID {
loadedSafePoint, err := storage.LoadServiceSafePoint(testSpaceID[i], testSafePoints[i].ServiceID)
c.Assert(err, IsNil)
c.Assert(loadedSafePoint, IsNil)
suite.Nil(err)
suite.Nil(loadedSafePoint)
}
}

func (s *testStorageGCSuite) TestSaveLoadGCSafePoint(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestSaveLoadGCSafePoint() {
storage := suite.storage
testSpaceIDs, testSafePoints := testGCSafePoints()
for i := range testSpaceIDs {
testSpaceID := testSpaceIDs[i]
testSafePoint := testSafePoints[i]
err := storage.SaveKeySpaceGCSafePoint(testSpaceID, testSafePoint)
c.Assert(err, IsNil)
suite.NoError(storage.SaveKeySpaceGCSafePoint(testSpaceID, testSafePoint))
loaded, err := storage.LoadKeySpaceGCSafePoint(testSpaceID)
c.Assert(err, IsNil)
c.Assert(loaded, Equals, testSafePoint)
suite.Nil(err)
suite.Equal(testSafePoint, loaded)
}
}

func (s *testStorageGCSuite) TestLoadAllKeySpaceGCSafePoints(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestLoadAllKeySpaceGCSafePoints() {
storage := suite.storage
testSpaceIDs, testSafePoints := testGCSafePoints()
for i := range testSpaceIDs {
err := storage.SaveKeySpaceGCSafePoint(testSpaceIDs[i], testSafePoints[i])
c.Assert(err, IsNil)
suite.NoError(storage.SaveKeySpaceGCSafePoint(testSpaceIDs[i], testSafePoints[i]))
}
loadedSafePoints, err := storage.LoadAllKeySpaceGCSafePoints(true)
c.Assert(err, IsNil)
suite.Nil(err)
sort.Slice(loadedSafePoints, func(a, b int) bool {
return loadedSafePoints[a].SpaceID < loadedSafePoints[b].SpaceID
})
for i := range loadedSafePoints {
c.Assert(loadedSafePoints[i].SpaceID, Equals, testSpaceIDs[i])
c.Assert(loadedSafePoints[i].SafePoint, Equals, testSafePoints[i])
suite.Equal(testSpaceIDs[i], loadedSafePoints[i].SpaceID)
suite.Equal(testSafePoints[i], loadedSafePoints[i].SafePoint)
}

// saving some service safe points.
spaceIDs, safePoints, TTLs := testServiceSafePoints()
for i := range spaceIDs {
c.Assert(storage.SaveServiceSafePoint(spaceIDs[i], safePoints[i], TTLs[i]), IsNil)
suite.NoError(storage.SaveServiceSafePoint(spaceIDs[i], safePoints[i], TTLs[i]))
}

// verify that service safe points do not interfere with gc safe points.
loadedSafePoints, err = storage.LoadAllKeySpaceGCSafePoints(true)
c.Assert(err, IsNil)
suite.Nil(err)
sort.Slice(loadedSafePoints, func(a, b int) bool {
return loadedSafePoints[a].SpaceID < loadedSafePoints[b].SpaceID
})
for i := range loadedSafePoints {
c.Assert(loadedSafePoints[i].SpaceID, Equals, testSpaceIDs[i])
c.Assert(loadedSafePoints[i].SafePoint, Equals, testSafePoints[i])
suite.Equal(testSpaceIDs[i], loadedSafePoints[i].SpaceID)
suite.Equal(testSafePoints[i], loadedSafePoints[i].SafePoint)
}

// verify that when withGCSafePoint set to false, returned safePoints is 0
loadedSafePoints, err = storage.LoadAllKeySpaceGCSafePoints(false)
c.Assert(err, IsNil)
suite.Nil(err)
sort.Slice(loadedSafePoints, func(a, b int) bool {
return loadedSafePoints[a].SpaceID < loadedSafePoints[b].SpaceID
})
for i := range loadedSafePoints {
c.Assert(loadedSafePoints[i].SpaceID, Equals, testSpaceIDs[i])
c.Assert(loadedSafePoints[i].SafePoint, Equals, uint64(0))
suite.Equal(testSpaceIDs[i], loadedSafePoints[i].SpaceID)
suite.Equal(uint64(0), loadedSafePoints[i].SafePoint)
}
}
func (s *testStorageGCSuite) TestRevision(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestRevision() {
storage := suite.storage
keySpace1 := uint32(100)
keySpace2 := uint32(200)
// Touching key space 200 should not change revision of key space 100
c.Assert(storage.TouchKeySpaceRevision(keySpace1), IsNil)
suite.NoError(storage.TouchKeySpaceRevision(keySpace1))
oldRevision, err := storage.LoadKeySpaceRevision(keySpace1)
c.Assert(err, IsNil)
c.Assert(storage.TouchKeySpaceRevision(keySpace2), IsNil)
suite.Nil(err)
suite.NoError(storage.TouchKeySpaceRevision(keySpace2))
newRevision, err := storage.LoadKeySpaceRevision(keySpace1)
c.Assert(err, IsNil)
c.Assert(oldRevision, Equals, newRevision)
suite.Nil(err)
suite.Equal(oldRevision, newRevision)

// Touching the same key space should change revision
c.Assert(storage.TouchKeySpaceRevision(keySpace1), IsNil)
suite.NoError(storage.TouchKeySpaceRevision(keySpace1))
newRevision, err = storage.LoadKeySpaceRevision(keySpace1)
c.Assert(err, IsNil)
c.Assert(oldRevision, Not(Equals), newRevision)
suite.Nil(err)
suite.NotEqual(oldRevision, newRevision)
}

func (s *testStorageGCSuite) TestLoadEmpty(c *C) {
storage := s.storage
func (suite *StorageGCTestSuite) TestLoadEmpty() {
storage := suite.storage
testKeySpace := uint32(100)
// loading non-existing GC safepoint should return 0
gcSafePoint, err := storage.LoadKeySpaceGCSafePoint(testKeySpace)
c.Assert(err, IsNil)
c.Assert(gcSafePoint, Equals, uint64(0))
suite.Nil(err)
suite.Equal(uint64(0), gcSafePoint)

// loading non-existing service safepoint should return nil
serviceSafePoint, err := storage.LoadServiceSafePoint(testKeySpace, "testService")
c.Assert(err, IsNil)
c.Assert(serviceSafePoint, IsNil)
suite.Nil(err)
suite.Nil(serviceSafePoint)

// loading empty key spaces should return empty slices
safePoints, err := storage.LoadAllKeySpaceGCSafePoints(true)
c.Assert(err, IsNil)
c.Assert(safePoints, HasLen, 0)
suite.Nil(err)
suite.Empty(safePoints)

// Loading untouched key spaces should return unavailable revision
revision, err := storage.LoadKeySpaceRevision(testKeySpace)
c.Assert(err, IsNil)
c.Assert(revision, Equals, kv.RevisionUnavailable)
suite.Nil(err)
suite.Equal(kv.RevisionUnavailable, revision)
}

func newTestSingleConfig() *embed.Config {
Expand All @@ -299,13 +254,50 @@ func newTestSingleConfig() *embed.Config {
return cfg
}

func cleanConfig(cfg *embed.Config) error {
// Clean data directory
return os.RemoveAll(cfg.Dir)
func testGCSafePoints() ([]uint32, []uint64) {
spaceIDs := []uint32{
100,
200,
300,
400,
500,
}
safePoints := []uint64{
0,
1,
4396,
23333333333,
math.MaxUint64,
}
return spaceIDs, safePoints
}

func mustNewEmbedEtcd(c *C, cfg *embed.Config) *embed.Etcd {
etcd, err := embed.StartEtcd(cfg)
c.Assert(err, IsNil)
return etcd
func testServiceSafePoints() ([]uint32, []*endpoint.ServiceSafePoint, []int64) {
spaceIDs := []uint32{
100,
100,
100,
200,
200,
200,
300,
300,
300,
}
serviceSafePoints := []*endpoint.ServiceSafePoint{
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
{ServiceID: "service1", SafePoint: 1},
{ServiceID: "service2", SafePoint: 2},
{ServiceID: "service3", SafePoint: 3},
}
testTTls := make([]int64, 9)
for i := range testTTls {
testTTls[i] = 10
}
return spaceIDs, serviceSafePoints, testTTls
}

0 comments on commit ffe8a18

Please sign in to comment.