github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/latch/scheduler_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/latch/scheduler_test.go
    19  //
    20  
    21  // Copyright 2018 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 latch
    36  
    37  import (
    38  	"bytes"
    39  	"math/rand"
    40  	"sync"
    41  	"testing"
    42  	"time"
    43  )
    44  
    45  func TestWithConcurrency(t *testing.T) {
    46  	sched := NewScheduler(7)
    47  	defer sched.Close()
    48  	rand.Seed(time.Now().Unix())
    49  
    50  	ch := make(chan [][]byte, 100)
    51  	const workerCount = 10
    52  	var wg sync.WaitGroup
    53  	wg.Add(workerCount)
    54  	for i := 0; i < workerCount; i++ {
    55  		go func(ch <-chan [][]byte, wg *sync.WaitGroup) {
    56  			for txn := range ch {
    57  				lock := sched.Lock(getTso(), txn)
    58  				if lock.IsStale() {
    59  					// Should restart the transaction or return error
    60  				} else {
    61  					lock.SetCommitTS(getTso())
    62  					// Do 2pc
    63  				}
    64  				sched.UnLock(lock)
    65  			}
    66  			wg.Done()
    67  		}(ch, &wg)
    68  	}
    69  
    70  	for i := 0; i < 999; i++ {
    71  		ch <- generate()
    72  	}
    73  	close(ch)
    74  
    75  	wg.Wait()
    76  }
    77  
    78  // generate generates something like:
    79  // {[]byte("a"), []byte("b"), []byte("c")}
    80  // {[]byte("a"), []byte("d"), []byte("e"), []byte("f")}
    81  // {[]byte("e"), []byte("f"), []byte("g"), []byte("h")}
    82  // The data should not repeat in the sequence.
    83  func generate() [][]byte {
    84  	table := []byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'}
    85  	ret := make([][]byte, 0, 5)
    86  	chance := []int{100, 60, 40, 20}
    87  	for i := 0; i < len(chance); i++ {
    88  		needMore := rand.Intn(100) < chance[i]
    89  		if needMore {
    90  			randBytes := []byte{table[rand.Intn(len(table))]}
    91  			if !contains(randBytes, ret) {
    92  				ret = append(ret, randBytes)
    93  			}
    94  		}
    95  	}
    96  	return ret
    97  }
    98  
    99  func contains(x []byte, set [][]byte) bool {
   100  	for _, y := range set {
   101  		if bytes.Equal(x, y) {
   102  			return true
   103  		}
   104  	}
   105  	return false
   106  }