|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package binpacking |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + testingclock "k8s.io/utils/clock/testing" |
| 25 | +) |
| 26 | + |
| 27 | +func TestTimeLimiter(t *testing.T) { |
| 28 | + testCases := []struct { |
| 29 | + name string |
| 30 | + maxBinpackingDuration time.Duration |
| 31 | + timeAdvanced time.Duration |
| 32 | + expectStop bool |
| 33 | + }{ |
| 34 | + { |
| 35 | + name: "Time limit not exceeded", |
| 36 | + maxBinpackingDuration: 10 * time.Second, |
| 37 | + timeAdvanced: 5 * time.Second, |
| 38 | + expectStop: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "Time limit exceeded", |
| 42 | + maxBinpackingDuration: 10 * time.Second, |
| 43 | + timeAdvanced: 15 * time.Second, |
| 44 | + expectStop: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Time limit met exactly", |
| 48 | + maxBinpackingDuration: 10 * time.Second, |
| 49 | + timeAdvanced: 10 * time.Second, |
| 50 | + expectStop: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Zero duration", |
| 54 | + maxBinpackingDuration: 0, |
| 55 | + timeAdvanced: 1 * time.Millisecond, |
| 56 | + expectStop: true, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tc := range testCases { |
| 61 | + t.Run(tc.name, func(t *testing.T) { |
| 62 | + fakeClock := testingclock.NewFakeClock(time.Now()) |
| 63 | + limiter := newTimeLimiterWithClock(tc.maxBinpackingDuration, fakeClock.Now) |
| 64 | + |
| 65 | + limiter.InitBinpacking(nil, nil) |
| 66 | + fakeClock.Step(tc.timeAdvanced) |
| 67 | + |
| 68 | + assert.Equal(t, tc.expectStop, limiter.StopBinpacking(nil, nil)) |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
0 commit comments