Improve tso codecov (#7407)

Signed-off-by: yudong.cai <yudong.cai@zilliz.com>
This commit is contained in:
Cai Yudong 2021-09-01 18:29:59 +08:00 committed by GitHub
parent 3721cbfea1
commit bd3056fcbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 16 deletions

View File

@ -12,6 +12,7 @@
package tso
import (
"math"
"os"
"strings"
"testing"
@ -87,8 +88,58 @@ func TestGlobalTSOAllocator_All(t *testing.T) {
assert.Nil(t, err)
})
t.Run("Alloc", func(t *testing.T) {
_, err := gTestTsoAllocator.Alloc(100)
assert.Nil(t, err)
})
t.Run("AllocOne", func(t *testing.T) {
_, err := gTestTsoAllocator.AllocOne()
assert.Nil(t, err)
})
t.Run("Reset", func(t *testing.T) {
gTestTsoAllocator.Reset()
})
}
func TestGlobalTSOAllocator_Fail(t *testing.T) {
endpoints := os.Getenv("ETCD_ENDPOINTS")
if endpoints == "" {
endpoints = "localhost:2379"
}
etcdEndpoints := strings.Split(endpoints, ",")
etcdKV, err := tsoutil.NewTSOKVBase(etcdEndpoints, "/test/root/kv", "tsoTest")
assert.NoError(t, err)
gTestTsoAllocator = NewGlobalTSOAllocator("timestamp", etcdKV)
t.Run("Initialize", func(t *testing.T) {
err := gTestTsoAllocator.Initialize()
assert.Nil(t, err)
})
t.Run("GenerateTSO_invalid", func(t *testing.T) {
_, err := gTestTsoAllocator.GenerateTSO(0)
assert.NotNil(t, err)
})
gTestTsoAllocator.SetLimitMaxLogic(true)
t.Run("SetTSO_invalid", func(t *testing.T) {
err := gTestTsoAllocator.SetTSO(0)
assert.NotNil(t, err)
err = gTestTsoAllocator.SetTSO(math.MaxUint64)
assert.NotNil(t, err)
})
t.Run("Alloc_invalid", func(t *testing.T) {
_, err := gTestTsoAllocator.Alloc(0)
assert.NotNil(t, err)
_, err = gTestTsoAllocator.Alloc(math.MaxUint32)
assert.NotNil(t, err)
})
t.Run("Reset", func(t *testing.T) {
gTestTsoAllocator.Reset()
})
}

View File

@ -68,19 +68,19 @@ type timestampOracle struct {
lastSavedTime atomic.Value
}
func (t *timestampOracle) loadTimestamp() (time.Time, error) {
strData, err := t.txnKV.Load(t.key)
var binData []byte = []byte(strData)
if err != nil {
return typeutil.ZeroTime, err
}
if len(binData) == 0 {
return typeutil.ZeroTime, nil
}
return typeutil.ParseTimestamp(binData)
}
//func (t *timestampOracle) loadTimestamp() (time.Time, error) {
// strData, err := t.txnKV.Load(t.key)
//
// var binData []byte = []byte(strData)
//
// if err != nil {
// return typeutil.ZeroTime, err
// }
// if len(binData) == 0 {
// return typeutil.ZeroTime, nil
// }
// return typeutil.ParseTimestamp(binData)
//}
// save timestamp, if lastTs is 0, we think the timestamp doesn't exist, so create it,
// otherwise, update it.
@ -95,12 +95,10 @@ func (t *timestampOracle) saveTimestamp(ts time.Time) error {
}
func (t *timestampOracle) InitTimestamp() error {
//last, err := t.loadTimestamp()
//if err != nil {
// return err
//}
next := time.Now()
// If the current system time minus the saved etcd timestamp is less than `updateTimestampGuard`,