| 1 |
/* |
| 2 |
Copyright 2020 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 fairqueuing |
| 18 |
|
| 19 |
import ( |
| 20 |
"math" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
testclock "k8s.io/utils/clock/testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestIntegrator(t *testing.T) { |
| 28 |
now := time.Now() |
| 29 |
clk := testclock.NewFakeClock(now) |
| 30 |
igr := NewNamedIntegrator(clk, "testee") |
| 31 |
igr.Add(3) |
| 32 |
clk.Step(time.Second) |
| 33 |
results := igr.GetResults() |
| 34 |
rToo := igr.Reset() |
| 35 |
if e := (IntegratorResults{Duration: time.Second.Seconds(), Average: 3, Deviation: 0, Min: 0, Max: 3}); !e.Equal(&results) { |
| 36 |
t.Errorf("expected %#+v, got %#+v", e, results) |
| 37 |
} |
| 38 |
if !results.Equal(&rToo) { |
| 39 |
t.Errorf("expected %#+v, got %#+v", results, rToo) |
| 40 |
} |
| 41 |
igr.Set(2) |
| 42 |
results = igr.GetResults() |
| 43 |
if e := (IntegratorResults{Duration: 0, Average: math.NaN(), Deviation: math.NaN(), Min: 2, Max: 3}); !e.Equal(&results) { |
| 44 |
t.Errorf("expected %#+v, got %#+v", e, results) |
| 45 |
} |
| 46 |
clk.Step(time.Millisecond) |
| 47 |
igr.Add(-1) |
| 48 |
clk.Step(time.Millisecond) |
| 49 |
results = igr.GetResults() |
| 50 |
if e := (IntegratorResults{Duration: 2 * time.Millisecond.Seconds(), Average: 1.5, Deviation: 0.5, Min: 1, Max: 3}); !e.Equal(&results) { |
| 51 |
t.Errorf("expected %#+v, got %#+v", e, results) |
| 52 |
} |
| 53 |
} |