...

Source file src/k8s.io/apimachinery/pkg/util/managedfields/internal/atmostevery_test.go

Documentation: k8s.io/apimachinery/pkg/util/managedfields/internal

     1  /*
     2  Copyright 2020 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 internal_test
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"k8s.io/apimachinery/pkg/util/managedfields/internal"
    24  )
    25  
    26  func TestAtMostEvery(t *testing.T) {
    27  	duration := time.Second
    28  	delay := 179 * time.Millisecond
    29  	atMostEvery := internal.NewAtMostEvery(delay)
    30  	count := 0
    31  	exit := time.NewTicker(duration)
    32  	tick := time.NewTicker(2 * time.Millisecond)
    33  	defer exit.Stop()
    34  	defer tick.Stop()
    35  
    36  	done := false
    37  	for !done {
    38  		select {
    39  		case <-exit.C:
    40  			done = true
    41  		case <-tick.C:
    42  			atMostEvery.Do(func() {
    43  				count++
    44  			})
    45  		}
    46  	}
    47  
    48  	if expected := int(duration/delay) + 1; count > expected {
    49  		t.Fatalf("Function called %d times, should have been called less than or equal to %d times", count, expected)
    50  	}
    51  }
    52  

View as plain text