k8s.io/apiserver@v0.31.1/pkg/util/flowcontrol/request/seat_seconds_test.go (about)

     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  	"math"
    21  	"testing"
    22  	"time"
    23  )
    24  
    25  // TestSeatSecondsString exercises the SeatSeconds constructor and de-constructors (String, ToFloat).
    26  func TestSeatSecondsString(t *testing.T) {
    27  	testCases := []struct {
    28  		ss          SeatSeconds
    29  		expectFloat float64
    30  		expectStr   string
    31  	}{
    32  		{ss: SeatSeconds(1), expectFloat: 1.0 / ssScale, expectStr: "0.00000001ss"},
    33  		{ss: SeatSeconds(ssScale - 1), expectFloat: (ssScale - 1) / ssScale, expectStr: "0.99999999ss"},
    34  		{ss: 0, expectFloat: 0, expectStr: "0.00000000ss"},
    35  		{ss: SeatsTimesDuration(1, time.Second), expectFloat: 1, expectStr: "1.00000000ss"},
    36  		{ss: SeatsTimesDuration(123, 100*time.Millisecond), expectFloat: 12.3, expectStr: "12.30000000ss"},
    37  		{ss: SeatsTimesDuration(1203, 10*time.Millisecond), expectFloat: 12.03, expectStr: "12.03000000ss"},
    38  	}
    39  	for _, testCase := range testCases {
    40  		actualStr := testCase.ss.String()
    41  		if actualStr != testCase.expectStr {
    42  			t.Errorf("SeatSeconds(%d).String() is %q but expected %q", uint64(testCase.ss), actualStr, testCase.expectStr)
    43  		}
    44  		actualFloat := testCase.ss.ToFloat()
    45  		if math.Round(actualFloat*ssScale) != math.Round(testCase.expectFloat*ssScale) {
    46  			t.Errorf("SeatSeconds(%d).ToFloat() is %v but expected %v", uint64(testCase.ss), actualFloat, testCase.expectFloat)
    47  		}
    48  	}
    49  }
    50  
    51  func TestSeatSecondsPerSeat(t *testing.T) {
    52  	testCases := []struct {
    53  		ss     SeatSeconds
    54  		seats  float64
    55  		expect time.Duration
    56  	}{
    57  		{ss: SeatsTimesDuration(10, time.Second), seats: 1, expect: 10 * time.Second},
    58  		{ss: SeatsTimesDuration(1, time.Second), seats: 10, expect: 100 * time.Millisecond},
    59  		{ss: SeatsTimesDuration(13, 5*time.Millisecond), seats: 5, expect: 13 * time.Millisecond},
    60  		{ss: SeatsTimesDuration(12, 0), seats: 10, expect: 0},
    61  	}
    62  	for _, testCase := range testCases {
    63  		actualDuration := testCase.ss.DurationPerSeat(testCase.seats)
    64  		if actualDuration != testCase.expect {
    65  			t.Errorf("DurationPerSeats returned %v rather than expected %q", actualDuration, testCase.expect)
    66  		}
    67  	}
    68  }