github.com/KinWaiYuen/client-go/v2@v2.5.4/oracle/oracles/export_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/export_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
    36  
    37  import (
    38  	"sync/atomic"
    39  	"time"
    40  
    41  	"github.com/KinWaiYuen/client-go/v2/oracle"
    42  )
    43  
    44  // SetOracleHookCurrentTime exports localOracle's time hook to test.
    45  func SetOracleHookCurrentTime(oc oracle.Oracle, t time.Time) {
    46  	switch o := oc.(type) {
    47  	case *localOracle:
    48  		if o.hook == nil {
    49  			o.hook = &struct {
    50  				currentTime time.Time
    51  			}{}
    52  		}
    53  		o.hook.currentTime = t
    54  	}
    55  }
    56  
    57  // NewEmptyPDOracle exports pdOracle struct to test
    58  func NewEmptyPDOracle() oracle.Oracle {
    59  	return &pdOracle{}
    60  }
    61  
    62  // SetEmptyPDOracleLastTs exports PD oracle's global last ts to test.
    63  func SetEmptyPDOracleLastTs(oc oracle.Oracle, ts uint64) {
    64  	switch o := oc.(type) {
    65  	case *pdOracle:
    66  		lastTSInterface, _ := o.lastTSMap.LoadOrStore(oracle.GlobalTxnScope, new(uint64))
    67  		lastTSPointer := lastTSInterface.(*uint64)
    68  		atomic.StoreUint64(lastTSPointer, ts)
    69  		lasTSArrivalInterface, _ := o.lastArrivalTSMap.LoadOrStore(oracle.GlobalTxnScope, new(uint64))
    70  		lasTSArrivalPointer := lasTSArrivalInterface.(*uint64)
    71  		atomic.StoreUint64(lasTSArrivalPointer, uint64(time.Now().Unix()*1000))
    72  	}
    73  	setEmptyPDOracleLastArrivalTs(oc, ts)
    74  }
    75  
    76  // setEmptyPDOracleLastArrivalTs exports PD oracle's global last ts to test.
    77  func setEmptyPDOracleLastArrivalTs(oc oracle.Oracle, ts uint64) {
    78  	switch o := oc.(type) {
    79  	case *pdOracle:
    80  		o.setLastArrivalTS(ts, oracle.GlobalTxnScope)
    81  	}
    82  }