go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/cv/internal/common/project_distribuition_test.go (about) 1 // Copyright 2020 The LUCI 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 common 16 17 import ( 18 "fmt" 19 "sort" 20 "testing" 21 "time" 22 23 . "github.com/smartystreets/goconvey/convey" 24 ) 25 26 func TestProjectOffset(t *testing.T) { 27 t.Parallel() 28 29 Convey("DistributeOffset forms uniformish distribution", t, func() { 30 31 testIntervalOf100x := func(d time.Duration) { 32 Convey((100 * d).String(), func() { 33 offsets := make([]time.Duration, 101) 34 for i := 0; i < 101; i++ { 35 project := fmt.Sprintf("project-%d", i*i) 36 offsets[i] = DistributeOffset(100*d, "kind", project) 37 } 38 sort.Slice(offsets, func(i, j int) bool { return offsets[i] < offsets[j] }) 39 So(offsets[0], ShouldBeGreaterThanOrEqualTo, time.Duration(0)) 40 for i, o := range offsets { 41 min := time.Duration(i-10) * d 42 max := time.Duration(i+10) * d 43 So(o, ShouldBeBetweenOrEqual, min, max) 44 } 45 So(offsets[100], ShouldBeLessThan, 100*d) 46 }) 47 } 48 49 testIntervalOf100x(time.Nanosecond) 50 testIntervalOf100x(time.Millisecond) 51 testIntervalOf100x(10 * time.Millisecond) 52 testIntervalOf100x(100 * time.Millisecond) 53 testIntervalOf100x(time.Second) 54 testIntervalOf100x(time.Minute) 55 testIntervalOf100x(time.Hour) 56 testIntervalOf100x(7 * 24 * time.Hour) 57 }) 58 }