github.com/lfch/etcd-io/tests/v3@v3.0.0-20221004140520-eac99acd3e9d/functional/tester/cluster_shuffle.go (about) 1 // Copyright 2018 The etcd 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 package tester 16 17 import ( 18 "math/rand" 19 "time" 20 21 "go.uber.org/zap" 22 ) 23 24 func (clus *Cluster) shuffleCases() { 25 rand.Seed(time.Now().UnixNano()) 26 offset := rand.Intn(1000) 27 n := len(clus.cases) 28 cp := coprime(n) 29 30 css := make([]Case, n) 31 for i := 0; i < n; i++ { 32 css[i] = clus.cases[(cp*i+offset)%n] 33 } 34 clus.cases = css 35 clus.lg.Info("shuffled test failure cases", zap.Int("total", n)) 36 } 37 38 /* 39 x and y of GCD 1 are coprime to each other 40 41 x1 = ( coprime of n * idx1 + offset ) % n 42 x2 = ( coprime of n * idx2 + offset ) % n 43 (x2 - x1) = coprime of n * (idx2 - idx1) % n 44 45 = (idx2 - idx1) = 1 46 47 Consecutive x's are guaranteed to be distinct 48 */ 49 func coprime(n int) int { 50 coprime := 1 51 for i := n / 2; i < n; i++ { 52 if gcd(i, n) == 1 { 53 coprime = i 54 break 55 } 56 } 57 return coprime 58 } 59 60 func gcd(x, y int) int { 61 if y == 0 { 62 return x 63 } 64 return gcd(y, x%y) 65 }