github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/ghcache/ghcache_test.go (about)

     1  /*
     2  Copyright 2022 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 ghcache
    18  
    19  import (
    20  	"reflect"
    21  	"sync"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestCalculateRequestWaitDuration(t *testing.T) {
    27  	maxDelayTime := time.Second * 10
    28  	throttlingTime := time.Second
    29  	throttlingTimeForGET := time.Millisecond * 100
    30  	currentTime := time.Date(2022, time.January, 2, 0, 0, 0, 0, time.UTC)
    31  	type args struct {
    32  		t       tokenInfo
    33  		toQueue time.Time
    34  		getReq  bool
    35  	}
    36  	tests := []struct {
    37  		name     string
    38  		args     args
    39  		toQueue  time.Time
    40  		duration time.Duration
    41  	}{
    42  		{
    43  			name: "No request for some time, no need to wait",
    44  			args: args{
    45  				t: tokenInfo{
    46  					timestamp: currentTime.Add(-time.Minute),
    47  				},
    48  				toQueue: currentTime,
    49  			},
    50  			toQueue:  currentTime,
    51  			duration: 0,
    52  		},
    53  		{
    54  			name: "Non-GET request was made half second ago and incoming non-GET",
    55  			args: args{
    56  				t: tokenInfo{
    57  					timestamp: currentTime.Add(-time.Millisecond * 500),
    58  				},
    59  				toQueue: currentTime,
    60  			},
    61  			toQueue:  currentTime.Add(time.Millisecond * 500),
    62  			duration: time.Millisecond * 500,
    63  		},
    64  		{
    65  			name: "GET request was made half second ago and incoming GET",
    66  			args: args{
    67  				t: tokenInfo{
    68  					timestamp: currentTime.Add(-time.Millisecond * 500),
    69  					getReq:    true,
    70  				},
    71  				toQueue: currentTime,
    72  				getReq:  true,
    73  			},
    74  			toQueue:  currentTime,
    75  			duration: 0,
    76  		},
    77  		{
    78  			name: "Non-GET request needs to be scheduled, but there is a queue formed, adding on top",
    79  			args: args{
    80  				t: tokenInfo{
    81  					timestamp: currentTime.Add(time.Second),
    82  				},
    83  				toQueue: currentTime,
    84  			},
    85  			toQueue:  currentTime.Add(2 * time.Second),
    86  			duration: 2 * time.Second,
    87  		},
    88  		{
    89  			name: "GET request needs to be scheduled, but there is a queue formed, adding on top",
    90  			args: args{
    91  				t: tokenInfo{
    92  					timestamp: currentTime.Add(time.Second),
    93  				},
    94  				toQueue: currentTime,
    95  				getReq:  true,
    96  			},
    97  			toQueue:  currentTime.Add(time.Second + throttlingTimeForGET),
    98  			duration: time.Second + throttlingTimeForGET,
    99  		},
   100  		{
   101  			name: "Non-GET request needs to be scheduled, but there is a large queue formed, adding with max schedule time",
   102  			args: args{
   103  				t: tokenInfo{
   104  					timestamp: currentTime.Add(maxDelayTime - time.Millisecond),
   105  				},
   106  				toQueue: currentTime,
   107  			},
   108  			toQueue:  currentTime.Add(maxDelayTime),
   109  			duration: maxDelayTime,
   110  		},
   111  		{
   112  			name: "GET request needs to be scheduled, but there is a large queue formed, adding with max schedule time",
   113  			args: args{
   114  				t: tokenInfo{
   115  					timestamp: currentTime.Add(maxDelayTime - time.Millisecond),
   116  					getReq:    true,
   117  				},
   118  				toQueue: currentTime,
   119  			},
   120  			toQueue:  currentTime.Add(maxDelayTime),
   121  			duration: maxDelayTime,
   122  		},
   123  		{
   124  			name: "GET request needs to be scheduled, previous non-GET, duration shorter than throttlingTimeForGET",
   125  			args: args{
   126  				t: tokenInfo{
   127  					timestamp: currentTime.Add(-throttlingTimeForGET / 2),
   128  				},
   129  				toQueue: currentTime,
   130  				getReq:  true,
   131  			},
   132  			toQueue:  currentTime.Add(throttlingTimeForGET / 2),
   133  			duration: throttlingTimeForGET / 2,
   134  		},
   135  		{
   136  			name: "GET request was made and incoming request is also GET",
   137  			args: args{
   138  				t: tokenInfo{
   139  					timestamp: currentTime.Add(-time.Millisecond * 50),
   140  					getReq:    true,
   141  				},
   142  				toQueue: currentTime,
   143  				getReq:  true,
   144  			},
   145  			toQueue:  currentTime.Add(time.Millisecond * 50),
   146  			duration: time.Millisecond * 50,
   147  		},
   148  		{
   149  			name: "GET request was made and incoming request is also GET, but time has passed",
   150  			args: args{
   151  				t: tokenInfo{
   152  					timestamp: currentTime.Add(-throttlingTimeForGET * 2),
   153  					getReq:    true,
   154  				},
   155  				toQueue: currentTime,
   156  				getReq:  true,
   157  			},
   158  			toQueue:  currentTime,
   159  			duration: 0,
   160  		},
   161  	}
   162  
   163  	for _, tt := range tests {
   164  		t.Run(tt.name, func(t *testing.T) {
   165  			tr := &tokensRegistry{
   166  				lock:                 sync.Mutex{},
   167  				tokens:               map[string]tokenInfo{},
   168  				throttlingTime:       throttlingTime,
   169  				throttlingTimeForGET: throttlingTimeForGET,
   170  				maxDelayTime:         maxDelayTime,
   171  			}
   172  			got, got1 := tr.calculateRequestWaitDuration(tt.args.t, tt.args.toQueue, tt.args.getReq)
   173  			if !reflect.DeepEqual(got, tt.toQueue) {
   174  				t.Errorf("calculateRequestWaitDuration() got = %v, want %v", got, tt.toQueue)
   175  			}
   176  			if got1 != tt.duration {
   177  				t.Errorf("calculateRequestWaitDuration() got1 = %v, want %v", got1, tt.duration)
   178  			}
   179  		})
   180  	}
   181  }