| 1 |
/* |
| 2 |
Copyright 2016 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 cacher |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
"time" |
| 22 |
|
| 23 |
testingclock "k8s.io/utils/clock/testing" |
| 24 |
) |
| 25 |
|
| 26 |
func TestTimeBudget(t *testing.T) { |
| 27 |
fakeClock := testingclock.NewFakeClock(time.Now()) |
| 28 |
|
| 29 |
budget := &timeBudgetImpl{ |
| 30 |
clock: fakeClock, |
| 31 |
budget: time.Duration(0), |
| 32 |
maxBudget: 200 * time.Millisecond, |
| 33 |
refresh: 50 * time.Millisecond, |
| 34 |
last: fakeClock.Now(), |
| 35 |
} |
| 36 |
if res := budget.takeAvailable(); res != time.Duration(0) { |
| 37 |
t.Errorf("Expected: %v, got: %v", time.Duration(0), res) |
| 38 |
} |
| 39 |
|
| 40 |
// wait for longer than the maxBudget |
| 41 |
nextTime := time.Now().Add(10 * time.Second) |
| 42 |
fakeClock.SetTime(nextTime) |
| 43 |
if res := budget.takeAvailable(); res != budget.maxBudget { |
| 44 |
t.Errorf("Expected: %v, got: %v", budget.maxBudget, res) |
| 45 |
} |
| 46 |
// add two refresh intervals to accumulate 2*refresh durations |
| 47 |
nextTime = nextTime.Add(2 * time.Second) |
| 48 |
fakeClock.SetTime(nextTime) |
| 49 |
if res := budget.takeAvailable(); res != 2*budget.refresh { |
| 50 |
t.Errorf("Expected: %v, got: %v", 2*budget.refresh, res) |
| 51 |
} |
| 52 |
// return one refresh duration to have only one refresh duration available |
| 53 |
// we didn't advanced on time yet |
| 54 |
budget.returnUnused(budget.refresh) |
| 55 |
if res := budget.takeAvailable(); res != budget.refresh { |
| 56 |
t.Errorf("Expected: %v, got: %v", budget.refresh, res) |
| 57 |
} |
| 58 |
|
| 59 |
// return a negative value to the budget |
| 60 |
// we didn't advanced on time yet |
| 61 |
budget.returnUnused(-time.Duration(50)) |
| 62 |
if res := budget.takeAvailable(); res != time.Duration(0) { |
| 63 |
t.Errorf("Expected: %v, got: %v", time.Duration(0), res) |
| 64 |
} |
| 65 |
// handle back in time problem with an empty budget |
| 66 |
nextTime = nextTime.Add(-2 * time.Minute) |
| 67 |
fakeClock.SetTime(nextTime) |
| 68 |
if res := budget.takeAvailable(); res != time.Duration(0) { |
| 69 |
t.Errorf("Expected: %v, got: %v", time.Duration(0), res) |
| 70 |
} |
| 71 |
// wait for longer than the maxBudget |
| 72 |
// verify that adding a negative value didn't affected |
| 73 |
nextTime = nextTime.Add(10 * time.Minute) |
| 74 |
fakeClock.SetTime(nextTime) |
| 75 |
if res := budget.takeAvailable(); res != budget.maxBudget { |
| 76 |
t.Errorf("Expected: %v, got: %v", budget.maxBudget, res) |
| 77 |
} |
| 78 |
|
| 79 |
// handle back in time problem with time on the budget |
| 80 |
budget.returnUnused(10 * time.Second) |
| 81 |
nextTime = nextTime.Add(-2 * time.Minute) |
| 82 |
fakeClock.SetTime(nextTime) |
| 83 |
if res := budget.takeAvailable(); res != budget.maxBudget { |
| 84 |
t.Errorf("Expected: %v, got: %v", budget.maxBudget, res) |
| 85 |
} |
| 86 |
} |