| 1 |
/* |
| 2 |
Copyright 2021 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 request |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"testing" |
| 22 |
"time" |
| 23 |
|
| 24 |
clocktesting "k8s.io/utils/clock/testing" |
| 25 |
) |
| 26 |
|
| 27 |
func TestLatencyTrackersFrom(t *testing.T) { |
| 28 |
type testCase struct { |
| 29 |
Durations []time.Duration |
| 30 |
SumDurations time.Duration |
| 31 |
MaxDuration time.Duration |
| 32 |
} |
| 33 |
tc := testCase{ |
| 34 |
Durations: []time.Duration{100, 200, 300, 200, 400, 300, 100}, |
| 35 |
SumDurations: 1600, |
| 36 |
MaxDuration: 400, |
| 37 |
} |
| 38 |
t.Run("TestLatencyTrackersFrom", func(t *testing.T) { |
| 39 |
parent := context.TODO() |
| 40 |
_, ok := LatencyTrackersFrom(parent) |
| 41 |
if ok { |
| 42 |
t.Error("expected LatencyTrackersFrom to not be initialized") |
| 43 |
} |
| 44 |
|
| 45 |
clk := clocktesting.FakeClock{} |
| 46 |
ctx := WithLatencyTrackersAndCustomClock(parent, &clk) |
| 47 |
wd, ok := LatencyTrackersFrom(ctx) |
| 48 |
if !ok { |
| 49 |
t.Error("expected LatencyTrackersFrom to be initialized") |
| 50 |
} |
| 51 |
if wd.MutatingWebhookTracker.GetLatency() != 0 || wd.ValidatingWebhookTracker.GetLatency() != 0 || wd.APFQueueWaitTracker.GetLatency() != 0 { |
| 52 |
t.Error("expected values to be initialized to 0") |
| 53 |
} |
| 54 |
|
| 55 |
for _, d := range tc.Durations { |
| 56 |
wd.MutatingWebhookTracker.Track(func() { clk.Step(d) }) |
| 57 |
wd.ValidatingWebhookTracker.Track(func() { clk.Step(d) }) |
| 58 |
wd.APFQueueWaitTracker.Track(func() { clk.Step(d) }) |
| 59 |
} |
| 60 |
|
| 61 |
wd, ok = LatencyTrackersFrom(ctx) |
| 62 |
if !ok { |
| 63 |
t.Errorf("expected webhook duration to be initialized") |
| 64 |
} |
| 65 |
|
| 66 |
if wd.MutatingWebhookTracker.GetLatency() != tc.SumDurations { |
| 67 |
t.Errorf("expected admit duration: %q, but got: %q", tc.SumDurations, wd.MutatingWebhookTracker.GetLatency()) |
| 68 |
} |
| 69 |
|
| 70 |
if wd.ValidatingWebhookTracker.GetLatency() != tc.MaxDuration { |
| 71 |
t.Errorf("expected validate duration: %q, but got: %q", tc.MaxDuration, wd.ValidatingWebhookTracker.GetLatency()) |
| 72 |
} |
| 73 |
|
| 74 |
if wd.APFQueueWaitTracker.GetLatency() != tc.MaxDuration { |
| 75 |
t.Errorf("expected priority & fairness duration: %q, but got: %q", tc.MaxDuration, wd.APFQueueWaitTracker.GetLatency()) |
| 76 |
} |
| 77 |
}) |
| 78 |
} |