knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/test/vegeta/pacers/combined_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 TestCombinedPacer(t *testing.T) {
    27  	pacer1 := vegeta.Rate{Freq: 1, Per: time.Second}
    28  	pacer2 := vegeta.Rate{Freq: 5, Per: time.Second}
    29  	pacer3 := vegeta.Rate{Freq: 10, Per: time.Second}
    30  	pacers := []vegeta.Pacer{pacer1, pacer2, pacer3}
    31  	durations := []time.Duration{5 * time.Second, 5 * time.Second, 10 * time.Second}
    32  	pacer, _ := NewCombined(pacers, durations)
    33  
    34  	for _, tt := range []struct {
    35  		name            string
    36  		elapsedTime     time.Duration
    37  		elapsedHits     uint64
    38  		expectedNextHit time.Duration
    39  		expectedStop    bool
    40  	}{{
    41  		name:            "test the first hit",
    42  		elapsedTime:     0 * time.Second,
    43  		elapsedHits:     0,
    44  		expectedNextHit: 1 * time.Second,
    45  	}, {
    46  		name:            "test the switch pacer hit",
    47  		elapsedTime:     5 * time.Second,
    48  		elapsedHits:     5,
    49  		expectedNextHit: 200 * time.Millisecond,
    50  	}, {
    51  		name:            "test the pacer middle hit",
    52  		elapsedTime:     7 * time.Second,
    53  		elapsedHits:     15,
    54  		expectedNextHit: 200 * time.Millisecond,
    55  	}, {
    56  		name:            "test the last hit",
    57  		elapsedTime:     20 * time.Second,
    58  		elapsedHits:     130,
    59  		expectedNextHit: 1 * time.Second,
    60  	}, {
    61  		name:            "test the loop back pacer hit",
    62  		elapsedTime:     22 * time.Second,
    63  		elapsedHits:     132,
    64  		expectedNextHit: 1 * time.Second,
    65  	}, {
    66  		name:            "test the catch up hit",
    67  		elapsedTime:     24 * time.Second,
    68  		elapsedHits:     130,
    69  		expectedNextHit: 0,
    70  	}} {
    71  		t.Run(tt.name, func(t *testing.T) {
    72  			nextHit, _ := pacer.Pace(tt.elapsedTime, tt.elapsedHits)
    73  			if nextHit != tt.expectedNextHit {
    74  				t.Errorf(
    75  					"expected next hit for elapseTime %v and elapsedHits %d is %v, got %v",
    76  					tt.elapsedTime, tt.elapsedHits,
    77  					tt.expectedNextHit, nextHit,
    78  				)
    79  			}
    80  		})
    81  	}
    82  }
    83  
    84  func TestInvalidCombinedPacer(t *testing.T) {
    85  	for _, tt := range []struct {
    86  		name      string
    87  		pacers    []vegeta.Pacer
    88  		durations []time.Duration
    89  	}{{
    90  		name:      "pacers must not be empty",
    91  		pacers:    make([]vegeta.Pacer, 0),
    92  		durations: []time.Duration{10 * time.Second},
    93  	}, {
    94  		name:      "durations must not be empty",
    95  		pacers:    []vegeta.Pacer{vegeta.Rate{Freq: 10, Per: 10 * time.Second}},
    96  		durations: make([]time.Duration, 0),
    97  	}, {
    98  		name: "pacers and durations must have the same length",
    99  		pacers: []vegeta.Pacer{
   100  			vegeta.Rate{Freq: 10, Per: 10 * time.Second},
   101  			vegeta.Rate{Freq: 10, Per: 5 * time.Second},
   102  		},
   103  		durations: []time.Duration{10 * time.Second},
   104  	}, {
   105  		name:      "pacers length must be more than 1",
   106  		pacers:    []vegeta.Pacer{vegeta.Rate{Freq: 10, Per: 10 * time.Second}},
   107  		durations: []time.Duration{10 * time.Second},
   108  	}, {
   109  		name: "duration for each pacer must be longer than 1 second",
   110  		pacers: []vegeta.Pacer{
   111  			vegeta.Rate{Freq: 10, Per: 10 * time.Second},
   112  			vegeta.Rate{Freq: 10, Per: 5 * time.Second},
   113  		},
   114  		durations: []time.Duration{500 * time.Millisecond, 10 * time.Second},
   115  	}} {
   116  		t.Run(tt.name, func(t *testing.T) {
   117  			_, err := NewCombined(tt.pacers, tt.durations)
   118  			if err == nil {
   119  				t.Errorf("the provided configuration should be invalid: %v, %v", tt.pacers, tt.durations)
   120  			}
   121  		})
   122  	}
   123  }