github.com/uber/kraken@v0.1.4/lib/torrent/scheduler/buckets_test.go (about) 1 // Copyright (c) 2016-2019 Uber Technologies, Inc. 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 package scheduler 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/uber/kraken/utils/memsize" 21 "github.com/stretchr/testify/require" 22 "github.com/uber-go/tally" 23 ) 24 25 func TestGetBucket(t *testing.T) { 26 tests := []struct { 27 desc string 28 size uint64 29 expected string 30 }{ 31 {"below min", memsize.MB, "xsmall"}, 32 {"above max", 30 * memsize.GB, "xxlarge"}, 33 {"between buckets", 3 * memsize.GB, "large"}, 34 {"exact bucket", 1 * memsize.GB, "medium"}, 35 } 36 for _, test := range tests { 37 t.Run(test.desc, func(t *testing.T) { 38 require.Equal(t, test.expected, getBucket(test.size).sizeTag) 39 }) 40 } 41 } 42 43 func TestBucketAddRange(t *testing.T) { 44 tests := []struct { 45 desc string 46 start, stop, width time.Duration 47 expected []time.Duration 48 }{ 49 {"normal", 1, 4, 1, []time.Duration{1, 2, 3}}, 50 {"single", 1, 4, 5, []time.Duration{1}}, 51 {"none", 4, 1, 1, nil}, 52 } 53 for _, test := range tests { 54 t.Run(test.desc, func(t *testing.T) { 55 b := newBucket("test", 0) 56 b.addRange(test.start, test.stop, test.width) 57 require.Equal(t, tally.DurationBuckets(test.expected), b.durations) 58 }) 59 } 60 }