test(storage): add extent and extent store unit test

Signed-off-by: NaturalSelect <2145973003@qq.com>
This commit is contained in:
NaturalSelect 2023-09-06 18:14:53 +08:00 committed by leonrayang
parent e4970c2016
commit 2cfb03dcc1
16 changed files with 621 additions and 69 deletions

View File

@ -249,9 +249,6 @@ func (s *CarryWeightNodeSelector) getCarryNodes(nset *nodeSet, maxTotal uint64,
}
func (s *CarryWeightNodeSelector) setNodeCarry(nodes SortedWeightedNodes, availCarryCount, replicaNum int) {
if availCarryCount >= replicaNum {
return
}
for availCarryCount < replicaNum {
availCarryCount = 0
for _, nt := range nodes {

View File

@ -475,7 +475,7 @@ func (e *Extent) autoComputeExtentCrc(crcFunc UpdateCrcFunc) (crc uint32, err er
return crc, err
}
// DeleteTiny deletes a stiny extent.
// DeleteTiny deletes a tiny extent.
func (e *Extent) punchDelete(offset, size int64) (hasDelete bool, err error) {
log.LogDebugf("punchDelete extent %v offset %v, size %v", e, offset, size)
if int(offset)%util.PageSize != 0 {

View File

@ -0,0 +1,213 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package storage_test
import (
"fmt"
"hash/crc32"
"os"
"testing"
"github.com/cubefs/cubefs/proto"
"github.com/cubefs/cubefs/storage"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
func getPathForExtentStoreTest() (path string, err error) {
path, err = os.MkdirTemp("", "")
if err != nil {
return
}
path = fmt.Sprintf("%v/extents", path)
return
}
func extentStoreRwTest(t *testing.T, s *storage.ExtentStore, id uint64) {
dataStr := "hello world"
data := []byte(dataStr)
crc := crc32.ChecksumIEEE(data)
// append write
_, err := s.Write(id, 0, int64(len(data)), data, crc, storage.AppendWriteType, true)
require.NoError(t, err)
actualCrc, err := s.Read(id, 0, int64(len(data)), data, false)
require.NoError(t, err)
require.EqualValues(t, crc, actualCrc)
// random write
_, err = s.Write(id, 0, int64(len(data)), data, crc, storage.RandomWriteType, true)
require.NoError(t, err)
actualCrc, err = s.Read(id, 0, int64(len(data)), data, false)
require.NoError(t, err)
require.EqualValues(t, crc, actualCrc)
// TODO: append random write
require.NotEqualValues(t, s.GetStoreUsedSize(), 0)
}
func extentStoreMarkDeleteTiny(t *testing.T, s *storage.ExtentStore, id uint64, offset int64, size int64) {
err := s.MarkDelete(id, offset, int64(size))
require.NoError(t, err)
eds, err := s.GetHasDeleteTinyRecords()
require.NoError(t, err)
found := false
for _, ed := range eds {
if ed.ExtentID == id && ed.Offset == uint64(offset) {
found = true
}
}
require.Equal(t, found, true)
}
func extentStoreMarkDeleteNormalTest(t *testing.T, s *storage.ExtentStore, id uint64) {
ei, err := s.Watermark(id)
require.NoError(t, err)
err = s.MarkDelete(id, 0, int64(ei.Size))
require.NoError(t, err)
require.Equal(t, s.IsDeletedNormalExtent(id), true)
eds, err := s.GetHasDeleteExtent()
require.NoError(t, err)
found := false
for _, ed := range eds {
if ed.ExtentID == id && ed.Offset == 0 {
found = true
}
}
require.Equal(t, found, true)
}
func extentStoreMarkDeleteTinyTest(t *testing.T, s *storage.ExtentStore, id uint64) {
size, err := s.GetTinyExtentOffset(id)
require.NoError(t, err)
dataStr := "hello world"
data := []byte(dataStr)
require.NotEqualValues(t, size, 0)
// write second file to extent
crc := crc32.ChecksumIEEE(data)
_, err = s.Write(id, size, int64(len(data)), data, crc, storage.AppendWriteType, true)
require.NoError(t, err)
// mark delete first file
extentStoreMarkDeleteTiny(t, s, id, 0, size)
newSize, err := s.GetTinyExtentOffset(id)
require.NoError(t, err)
// mark delete second file
extentStoreMarkDeleteTiny(t, s, id, size, newSize-size)
}
func extentStoreMarkDeleteTest(t *testing.T, s *storage.ExtentStore, id uint64) {
if !s.HasExtent(id) {
t.Errorf("target extent doesn't exits")
return
}
if storage.IsTinyExtent(id) {
extentStoreMarkDeleteTinyTest(t, s, id)
return
}
extentStoreMarkDeleteNormalTest(t, s, id)
}
func extentStoreSizeTest(t *testing.T, s *storage.ExtentStore) {
maxId, size := s.GetMaxExtentIDAndPartitionSize()
total := s.StoreSizeExtentID(maxId)
require.EqualValues(t, total, size)
}
func extentStoreLogicalTest(t *testing.T, s *storage.ExtentStore) {
normalId, err := s.NextExtentID()
require.NoError(t, err)
err = s.Create(normalId)
require.NoError(t, err)
s.SendToAvailableTinyExtentC(testTinyExtentID)
tinyId, err := s.GetAvailableTinyExtent()
require.NoError(t, err)
ids := []uint64{
normalId,
tinyId,
}
for _, id := range ids {
extentStoreRwTest(t, s, id)
extentStoreSizeTest(t, s)
extentStoreMarkDeleteTest(t, s, id)
extentStoreSizeTest(t, s)
}
}
func reopenExtentStoreTest(t *testing.T, dpType int) {
path, err := getPathForExtentStoreTest()
require.NoError(t, err)
s, err := storage.NewExtentStore(path, 0, 1*util.GB, dpType, true)
require.NoError(t, err)
defer s.Close()
id, err := s.NextExtentID()
require.NoError(t, err)
err = s.Create(id)
require.NoError(t, err)
dataStr := "hello world"
data := []byte(dataStr)
crc := crc32.ChecksumIEEE(data)
// write some data
_, err = s.Write(id, 0, int64(len(data)), data, crc, storage.AppendWriteType, true)
require.NoError(t, err)
firstSnap, err := s.SnapShot()
require.NoError(t, err)
s.Close()
newStor, err := storage.NewExtentStore(path, 0, 1*util.GB, dpType, false)
require.NoError(t, err)
defer newStor.Close()
// read data
actualCrc, err := newStor.Read(id, 0, int64(len(data)), data, false)
require.NoError(t, err)
require.EqualValues(t, crc, actualCrc)
secondSnap, err := newStor.SnapShot()
require.NoError(t, err)
require.EqualValues(t, len(firstSnap), len(secondSnap))
// check snapshot
firstSnapNames := make(map[string]interface{})
for _, file := range firstSnap {
firstSnapNames[file.Name] = 1
}
secondSnapNames := make(map[string]interface{})
for _, file := range secondSnap {
secondSnapNames[file.Name] = 1
}
for k := range firstSnapNames {
require.NotNil(t, secondSnapNames[k])
}
for k := range secondSnapNames {
require.NotNil(t, firstSnapNames[k])
}
}
func extentStoreTest(t *testing.T, dpType int) {
path, err := getPathForExtentStoreTest()
if err != nil {
t.Errorf("failed to get extent path, err %v", err)
return
}
s, err := storage.NewExtentStore(path, 0, 1*util.GB, dpType, true)
require.NoError(t, err)
defer s.Close()
extentStoreLogicalTest(t, s)
reopenExtentStoreTest(t, dpType)
}
func TestExtentStores(t *testing.T) {
dpTypes := []int{
proto.PartitionTypeNormal,
proto.PartitionTypePreLoad,
proto.PartitionTypeCache,
}
for _, ty := range dpTypes {
extentStoreTest(t, ty)
}
}

196
storage/extent_test.go Normal file
View File

@ -0,0 +1,196 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package storage_test
import (
"fmt"
"os"
"testing"
"github.com/cubefs/cubefs/storage"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
const testTinyExtentID = 1
const testNormalExtentID = 65
func getTestExtentName(id uint64) (name string, err error) {
tmp, err := os.MkdirTemp("", "")
if err != nil {
return
}
name = fmt.Sprintf("%v/%v", tmp, id)
return
}
func getTestTinyExtentName() (name string, err error) {
return getTestExtentName(testTinyExtentID)
}
func getTestNormalExtentName() (name string, err error) {
return getTestExtentName(testNormalExtentID)
}
func mockCrcPersist(t *testing.T, e *storage.Extent, blockNo int, blockCrc uint32) (err error) {
t.Logf("persist crc extent blockNo: %v blockCrc:%v", blockNo, blockCrc)
return
}
func getMockCrcPersist(t *testing.T) storage.UpdateCrcFunc {
return func(e *storage.Extent, blockNo int, crc uint32) (err error) {
return mockCrcPersist(t, e, blockNo, crc)
}
}
func normalExtentRwTest(t *testing.T, e *storage.Extent) {
dataStr := "hello world"
data := []byte(dataStr)
// append write
_, err := e.Write(data, 0, int64(len(data)), 0, storage.AppendWriteType, true, getMockCrcPersist(t), nil)
require.NoError(t, err)
require.EqualValues(t, e.Size(), len(data))
_, err = e.Read(data, 0, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
// failed append write
_, err = e.Write(data, 0, int64(len(data)), 0, storage.AppendWriteType, true, getMockCrcPersist(t), nil)
require.Error(t, err)
// random append write
oldSize := e.Size()
_, err = e.Write(data, 0, int64(len(data)), 0, storage.RandomWriteType, true, getMockCrcPersist(t), nil)
require.NoError(t, err)
require.Equal(t, e.Size(), oldSize)
_, err = e.Read(data, 0, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
// TODO: append random write test
}
func tinyExtentRwTest(t *testing.T, e *storage.Extent) {
dataStr := "hello world"
data := []byte(dataStr)
// append write
_, err := e.Write(data, 0, int64(len(data)), 0, storage.AppendWriteType, true, getMockCrcPersist(t), nil)
require.NoError(t, err)
require.EqualValues(t, e.Size()%util.PageSize, 0)
_, err = e.Read(data, 0, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
// failed append write
_, err = e.Write(data, 0, int64(len(data)), 0, storage.AppendWriteType, true, getMockCrcPersist(t), nil)
require.Error(t, err)
// random write
oldSize := e.Size()
_, err = e.Write(data, int64(len(data)), int64(len(data)), 0, storage.RandomWriteType, true, getMockCrcPersist(t), nil)
require.NoError(t, err)
require.Equal(t, e.Size(), oldSize)
_, err = e.Read(data, int64(len(data)), int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
}
func normalExtentCreateTest(t *testing.T, name string) {
e := storage.NewExtentInCore(name, testNormalExtentID)
err := e.InitToFS()
require.NoError(t, err)
defer e.Close()
normalExtentRwTest(t, e)
}
func tinyExtentCreateTest(t *testing.T, name string) {
e := storage.NewExtentInCore(name, testTinyExtentID)
err := e.InitToFS()
require.NoError(t, err)
defer e.Close()
tinyExtentRwTest(t, e)
}
func normalExtentRecoveryTest(t *testing.T, name string) {
e := storage.NewExtentInCore(name, testNormalExtentID)
require.Equal(t, e.Exist(), true)
err := e.RestoreFromFS()
require.NoError(t, err)
defer e.Close()
dataStr := "hello world"
data := []byte(dataStr)
_, err = e.ReadTiny(data, 0, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
}
func tinyExtentRecoveryTest(t *testing.T, name string) {
e := storage.NewExtentInCore(name, testTinyExtentID)
require.Equal(t, e.Exist(), true)
err := e.RestoreFromFS()
require.NoError(t, err)
defer e.Close()
dataStr := "hello world"
data := []byte(dataStr)
_, err = e.ReadTiny(data, 0, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
_, err = e.Read(data, int64(len(data)), int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
}
func tinyExtentRepairTest(t *testing.T, name string) {
e := storage.NewExtentInCore(name, testTinyExtentID)
require.Equal(t, e.Exist(), true)
err := e.RestoreFromFS()
require.NoError(t, err)
defer e.Close()
dataStr := "hello world"
data := []byte(dataStr)
size := e.Size()
err = e.TinyExtentRecover(nil, size, int64(len(data)), 0, true)
require.NoError(t, err)
t.Logf("extent data size is %v", e.Size())
_, err = e.Read(data, size, int64(len(data)), true)
require.NoError(t, err)
for _, v := range data {
require.EqualValues(t, v, 0)
}
size = e.Size()
data = []byte(dataStr)
err = e.TinyExtentRecover(data, size, int64(len(data)), 0, false)
require.NoError(t, err)
_, err = e.Read(data, size, int64(len(data)), false)
require.NoError(t, err)
require.Equal(t, string(data), dataStr)
}
func TestTinyExtent(t *testing.T) {
name, err := getTestTinyExtentName()
if err != nil {
t.Errorf("failed to get extent path")
return
}
tinyExtentCreateTest(t, name)
tinyExtentRecoveryTest(t, name)
tinyExtentRepairTest(t, name)
}
func TestNormalExtent(t *testing.T) {
name, err := getTestNormalExtentName()
if err != nil {
t.Errorf("failed to get extent path")
return
}
normalExtentCreateTest(t, name)
normalExtentRecoveryTest(t, name)
}

View File

@ -18,13 +18,12 @@ import (
"testing"
"github.com/cubefs/cubefs/util/atomicutil"
"github.com/stretchr/testify/require"
)
func TestAtomicFloat64(t *testing.T) {
f := atomicutil.Float64{}
testVal := float64(1.0)
f.Store(testVal)
if f.Load() != testVal {
t.Errorf("Atomic float64 should equal with %v but got %v", testVal, f.Load())
}
require.Equal(t, testVal, f.Load())
}

View File

@ -19,6 +19,7 @@ import (
"github.com/cubefs/cubefs/blockcache/bcache"
"github.com/cubefs/cubefs/util/buf"
"github.com/stretchr/testify/require"
)
func TestFileBCachePool(t *testing.T) {
@ -27,12 +28,13 @@ func TestFileBCachePool(t *testing.T) {
}
firstVal := buf.BCachePool.Get()
secondVal := buf.BCachePool.Get()
if len(firstVal) != len(secondVal) {
t.Errorf("Get() should return buffer with same length")
return
}
if &firstVal[0] == &secondVal[0] {
t.Errorf("two Get() should not return one buffer")
return
}
// check length
require.Equal(t, len(firstVal), len(secondVal))
// check address
require.NotSame(t, &firstVal[0], &secondVal[0])
oldAddr := &secondVal[0]
buf.BCachePool.Put(secondVal)
secondVal = buf.BCachePool.Get()
// check put and get
require.Same(t, oldAddr, &secondVal[0])
}

View File

@ -19,33 +19,36 @@ import (
"github.com/cubefs/cubefs/util"
"github.com/cubefs/cubefs/util/buf"
"github.com/stretchr/testify/require"
)
func checkPool(t *testing.T, pool *buf.BufferPool, size int) (success bool) {
const checkPoolLoopCount = buf.HeaderBufferPoolSize
func checkPool(t *testing.T, pool *buf.BufferPool, size int) {
first, err := pool.Get(size)
if err != nil {
t.Errorf("failed to get buffer from pool %v", err)
return
}
require.NoError(t, err)
second, err := pool.Get(size)
if err != nil {
t.Errorf("failed to get buffer from pool %v", err)
return
}
if len(first) != len(second) {
t.Errorf("two buffer should have same length")
return
}
if len(first) != size {
t.Errorf("buffer length should be %v", size)
return
}
require.NoError(t, err)
// check size
require.Equal(t, len(first), len(second))
require.Equal(t, len(first), size)
if &first[0] == &second[0] {
t.Errorf("two Get() should not returns one buffer")
return
}
success = true
return
oldAddr := &second[0]
pool.Put(second)
success := false
for i := 0; i < checkPoolLoopCount; i++ {
second, err = pool.Get(size)
require.NoError(t, err)
require.NotSame(t, &second[0], &first[0])
if oldAddr == &second[0] {
success = true
break
}
}
require.Equal(t, true, success)
}
func TestBufferPool(t *testing.T) {
@ -56,8 +59,6 @@ func TestBufferPool(t *testing.T) {
util.DefaultTinySizeLimit,
}
for _, size := range sizes {
if !checkPool(t, pool, size) {
return
}
checkPool(t, pool, size)
}
}

View File

@ -0,0 +1,41 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package buf_test
import (
"testing"
"github.com/cubefs/cubefs/util/buf"
"github.com/stretchr/testify/require"
)
func checkCachePool(t *testing.T, pool *buf.FileCachePool) {
first := pool.Get()
second := pool.Get()
require.Equal(t, len(first), len(second))
require.NotSame(t, &first[0], &second[0])
oldAddr := &second[0]
pool.Put(second)
second = pool.Get()
require.NotSame(t, &second[0], &first[0])
require.Same(t, oldAddr, &second[0])
}
func TestCachePool(t *testing.T) {
if buf.CachePool == nil {
buf.InitCachePool(8388608)
}
checkCachePool(t, buf.CachePool)
}

View File

@ -19,6 +19,7 @@ import (
"testing"
"github.com/cubefs/cubefs/util/iputil"
"github.com/stretchr/testify/require"
)
// from iputil.GetDistance
@ -29,8 +30,5 @@ func commonPrefixLen(first net.IP, second net.IP) int {
func TestCommonPrefixLength(t *testing.T) {
firstV6 := net.ParseIP("fe80::1")
secondV6 := net.ParseIP("fe80::2")
if commonPrefixLen(firstV6, secondV6) != 64 {
t.Errorf("fe80::1 & fe80::2 common prefix length should be 64")
return
}
require.Equal(t, 64, commonPrefixLen(firstV6, secondV6))
}

View File

@ -19,6 +19,7 @@ import (
"testing"
"github.com/cubefs/cubefs/util/iputil"
"github.com/stretchr/testify/require"
)
func TestGetRealIp(t *testing.T) {
@ -30,22 +31,13 @@ func TestGetRealIp(t *testing.T) {
// cubefs.io
ip := "13.250.168.211"
request.RemoteAddr = ip
if iputil.RealIP(request) != ip {
t.Errorf("should returns %v but got %v", request.RemoteAddr, iputil.RealIP(request))
return
}
require.Equal(t, iputil.RealIP(request), ip)
request.RemoteAddr = "192.168.0.1"
request.Header.Add("X-Forwarded-For", ip)
if iputil.RealIP(request) != ip {
t.Errorf("should returns %v but got %v", ip, iputil.RealIP(request))
return
}
require.Equal(t, iputil.RealIP(request), ip)
for k := range request.Header {
delete(request.Header, k)
}
request.Header.Add("X-Real-Ip", ip)
if iputil.RealIP(request) != ip {
t.Errorf("should returns %v but got %v", ip, iputil.RealIP(request))
return
}
require.Equal(t, iputil.RealIP(request), ip)
}

36
util/mem_test.go Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package util_test
import (
"os"
"testing"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
func TestGetMemInfo(t *testing.T) {
total, used, err := util.GetMemInfo()
require.NoError(t, err)
require.NotEqual(t, total, 0)
require.LessOrEqual(t, used, total)
}
func TestGetProcessMemory(t *testing.T) {
used, err := util.GetProcessMemory(os.Getpid())
require.NoError(t, err)
require.NotEqual(t, 0, used)
}

31
util/multipart_test.go Normal file
View File

@ -0,0 +1,31 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package util_test
import (
"testing"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
const testMpIDForMultiPart = 1
func TestMultiPartID(t *testing.T) {
id := util.CreateMultipartID(testMpIDForMultiPart).String()
pid, found := util.MultipartIDFromString(id).PartitionID()
require.Equal(t, found, true)
require.EqualValues(t, pid, testMpIDForMultiPart)
}

View File

@ -10,7 +10,7 @@
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.k
// permissions and limitations under the License.
package util

55
util/set_test.go Normal file
View File

@ -0,0 +1,55 @@
// Copyright 2023 The CubeFS Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package util_test
import (
"sync"
"testing"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
var stringsForSetTest = []string{
"Hello",
"World",
"1234",
}
func TestSet(t *testing.T) {
s := util.NewSet()
if s.Len() != 0 {
t.Errorf("set should be empty")
return
}
wg := &sync.WaitGroup{}
wg.Add(len(stringsForSetTest))
for _, v := range stringsForSetTest {
copyV := v
go func() {
s.Add(copyV)
wg.Done()
}()
}
wg.Wait()
for _, v := range stringsForSetTest {
require.Equal(t, s.Has(v), true)
}
s.Clear()
require.Equal(t, s.Len(), 0)
s.Add(stringsForSetTest[0])
s.Remove(stringsForSetTest[0])
require.NotEqual(t, s.Has(stringsForSetTest[0]), true)
}

View File

@ -18,20 +18,16 @@ import (
"testing"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
func TestRandomString(t *testing.T) {
first := util.RandomString(256, util.UpperLetter|util.LowerLetter|util.Numeric)
second := util.RandomString(256, util.UpperLetter|util.LowerLetter|util.Numeric)
if first == second {
t.Errorf("first should not equal with second")
return
}
require.NotEqual(t, first, second)
}
func TestSubStr(t *testing.T) {
str := "abcd"
if util.SubString(str, 1, 2) != "b" {
t.Errorf("SubString should returns \"b\" not got \"%v\"", util.SubString(str, 1, 2))
}
require.Equal(t, util.SubString(str, 1, 2), "b")
}

View File

@ -18,15 +18,10 @@ import (
"testing"
"github.com/cubefs/cubefs/util"
"github.com/stretchr/testify/require"
)
func TestIsIpv4(t *testing.T) {
if !util.IsIPV4Addr("127.0.0.1:80") {
t.Errorf("127.0.0.1 is a ipv4 addr")
return
}
if util.IsIPV4("[1fff:0:a88:85a3::ac1f]:80") {
t.Errorf("1fff:0:a88:85a3::ac1f is not a ipv4 addr")
return
}
require.Equal(t, util.IsIPV4Addr("127.0.0.1:80"), true)
require.NotEqual(t, util.IsIPV4("[1fff:0:a88:85a3::ac1f]:80"), true)
}