knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/vegeta/pacers/steady_up_pacer_test.go (about)

     1  /*
     2  Copyright 2019 The Knative 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 pacers
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	vegeta "github.com/tsenart/vegeta/v12/lib"
    24  )
    25  
    26  func TestSteadyUpPacer(t *testing.T) {
    27  	minRate := vegeta.Rate{Freq: 1, Per: time.Second}
    28  	maxRate := vegeta.Rate{Freq: 5, Per: time.Second}
    29  	pacer, _ := NewSteadyUp(minRate, maxRate, 10*time.Second)
    30  
    31  	for _, tt := range []struct {
    32  		name            string
    33  		elapsedTime     time.Duration
    34  		elapsedHits     uint64
    35  		expectedNextHit time.Duration
    36  		expectedStop    bool
    37  	}{{
    38  		name:            "test the first hit",
    39  		elapsedTime:     0 * time.Second,
    40  		elapsedHits:     0,
    41  		expectedNextHit: 853658536 * time.Nanosecond,
    42  	}, {
    43  		name:            "test the up hit",
    44  		elapsedTime:     3 * time.Second,
    45  		elapsedHits:     5,
    46  		expectedNextHit: 520407468 * time.Nanosecond,
    47  	}, {
    48  		name:            "test the catch up hit",
    49  		elapsedTime:     4 * time.Second,
    50  		elapsedHits:     2,
    51  		expectedNextHit: 0,
    52  	}, {
    53  		name:            "test the steady hit",
    54  		elapsedTime:     7052432251 * time.Nanosecond,
    55  		elapsedHits:     17,
    56  		expectedNextHit: 258228895 * time.Nanosecond,
    57  	}} {
    58  		t.Run(tt.name, func(t *testing.T) {
    59  			nextHit, _ := pacer.Pace(tt.elapsedTime, tt.elapsedHits)
    60  			if nextHit != tt.expectedNextHit {
    61  				t.Errorf(
    62  					"expected next hit for elapsedTime %v and elapsedHits %d is %v, got %v",
    63  					tt.elapsedTime, tt.elapsedHits,
    64  					tt.expectedNextHit, nextHit,
    65  				)
    66  			}
    67  		})
    68  	}
    69  }
    70  
    71  func TestInvalidSteadyUpPacer(t *testing.T) {
    72  	for _, tt := range []struct {
    73  		name       string
    74  		min        vegeta.Rate
    75  		max        vegeta.Rate
    76  		upDuration time.Duration
    77  	}{{
    78  		name:       "up duration must be larger than 0",
    79  		min:        vegeta.Rate{Freq: 10, Per: time.Second},
    80  		max:        vegeta.Rate{Freq: 5, Per: time.Second},
    81  		upDuration: 0,
    82  	}, {
    83  		name:       "min rate must be larger than 0",
    84  		min:        vegeta.Rate{Freq: 0, Per: time.Second},
    85  		max:        vegeta.Rate{Freq: 5, Per: time.Second},
    86  		upDuration: 10 * time.Second,
    87  	}, {
    88  		name:       "max rate must be larger than 0",
    89  		min:        vegeta.Rate{Freq: 10, Per: time.Second},
    90  		max:        vegeta.Rate{Freq: 0, Per: time.Second},
    91  		upDuration: 10 * time.Second,
    92  	}, {
    93  		name:       "min rate must be smaller than max rate",
    94  		min:        vegeta.Rate{Freq: 10, Per: time.Second},
    95  		max:        vegeta.Rate{Freq: 6, Per: time.Second},
    96  		upDuration: 10 * time.Second,
    97  	}} {
    98  		t.Run(tt.name, func(t *testing.T) {
    99  			_, err := NewSteadyUp(tt.min, tt.max, tt.upDuration)
   100  			if err == nil {
   101  				t.Errorf("the provided configuration should be invalid: %v, %v, %v", tt.min, tt.max, tt.upDuration)
   102  			}
   103  		})
   104  	}
   105  }