github.com/KinWaiYuen/client-go/v2@v2.5.4/oracle/oracles/local_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/local_test.go 19 // 20 21 // Copyright 2016 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 "testing" 40 "time" 41 42 "github.com/KinWaiYuen/client-go/v2/oracle" 43 "github.com/KinWaiYuen/client-go/v2/oracle/oracles" 44 "github.com/stretchr/testify/assert" 45 "github.com/stretchr/testify/require" 46 ) 47 48 func TestLocalOracle(t *testing.T) { 49 l := oracles.NewLocalOracle() 50 defer l.Close() 51 m := map[uint64]struct{}{} 52 for i := 0; i < 100000; i++ { 53 ts, err := l.GetTimestamp(context.Background(), &oracle.Option{}) 54 require.Nil(t, err) 55 m[ts] = struct{}{} 56 } 57 58 assert.Len(t, m, 100000, "should generate same ts") 59 } 60 61 func TestIsExpired(t *testing.T) { 62 o := oracles.NewLocalOracle() 63 defer o.Close() 64 65 start := time.Now() 66 oracles.SetOracleHookCurrentTime(o, start) 67 ts, _ := o.GetTimestamp(context.Background(), &oracle.Option{}) 68 oracles.SetOracleHookCurrentTime(o, start.Add(10*time.Millisecond)) 69 70 expire := o.IsExpired(ts, 5, &oracle.Option{}) 71 assert.True(t, expire) 72 73 expire = o.IsExpired(ts, 200, &oracle.Option{}) 74 assert.False(t, expire) 75 } 76 77 func TestLocalOracle_UntilExpired(t *testing.T) { 78 o := oracles.NewLocalOracle() 79 defer o.Close() 80 start := time.Now() 81 oracles.SetOracleHookCurrentTime(o, start) 82 ts, _ := o.GetTimestamp(context.Background(), &oracle.Option{}) 83 84 oracles.SetOracleHookCurrentTime(o, start.Add(10*time.Millisecond)) 85 assert.Equal(t, int64(-4), o.UntilExpired(ts, 6, &oracle.Option{})) 86 assert.Equal(t, int64(4), o.UntilExpired(ts, 14, &oracle.Option{})) 87 }