github.com/KinWaiYuen/client-go/v2@v2.5.4/oracle/oracles/pd_test.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/oracle/oracles/pd_test.go 19 // 20 21 // Copyright 2019 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package oracles_test 36 37 import ( 38 "context" 39 "math" 40 "testing" 41 "time" 42 43 "github.com/KinWaiYuen/client-go/v2/oracle" 44 "github.com/KinWaiYuen/client-go/v2/oracle/oracles" 45 "github.com/stretchr/testify/assert" 46 ) 47 48 func TestPDOracle_UntilExpired(t *testing.T) { 49 lockAfter, lockExp := 10, 15 50 o := oracles.NewEmptyPDOracle() 51 start := time.Now() 52 oracles.SetEmptyPDOracleLastTs(o, oracle.GoTimeToTS(start)) 53 lockTs := oracle.GoTimeToTS(start.Add(time.Duration(lockAfter)*time.Millisecond)) + 1 54 waitTs := o.UntilExpired(lockTs, uint64(lockExp), &oracle.Option{TxnScope: oracle.GlobalTxnScope}) 55 assert.Equal(t, int64(lockAfter+lockExp), waitTs) 56 } 57 58 func TestPdOracle_GetStaleTimestamp(t *testing.T) { 59 o := oracles.NewEmptyPDOracle() 60 61 start := time.Now() 62 oracles.SetEmptyPDOracleLastTs(o, oracle.GoTimeToTS(start)) 63 ts, err := o.GetStaleTimestamp(context.Background(), oracle.GlobalTxnScope, 10) 64 assert.Nil(t, err) 65 assert.WithinDuration(t, start.Add(-10*time.Second), oracle.GetTimeFromTS(ts), 2*time.Second) 66 67 _, err = o.GetStaleTimestamp(context.Background(), oracle.GlobalTxnScope, 1e12) 68 assert.NotNil(t, err) 69 assert.Regexp(t, ".*invalid prevSecond.*", err.Error()) 70 71 _, err = o.GetStaleTimestamp(context.Background(), oracle.GlobalTxnScope, math.MaxUint64) 72 assert.NotNil(t, err) 73 assert.Regexp(t, ".*invalid prevSecond.*", err.Error()) 74 }