...

Source file src/k8s.io/utils/clock/testing/simple_interval_clock_test.go

Documentation: k8s.io/utils/clock/testing

     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 testing
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  func TestSimpleIntervalClockNow(t *testing.T) {
    25  	cases := []struct {
    26  		duration time.Duration
    27  	}{
    28  		{duration: 10 * time.Millisecond},
    29  		{duration: 0 * time.Millisecond},
    30  		{duration: -10 * time.Millisecond},
    31  	}
    32  
    33  	startTime := time.Now()
    34  	for _, c := range cases {
    35  		expectedTime := startTime.Add(c.duration)
    36  		sic := &SimpleIntervalClock{
    37  			Time:     startTime,
    38  			Duration: c.duration,
    39  		}
    40  		actualTime := sic.Now()
    41  		if !expectedTime.Equal(actualTime) {
    42  			t.Errorf("expected %#v, got %#v", expectedTime, actualTime)
    43  		}
    44  	}
    45  }
    46  
    47  func TestSimpleIntervalClockSince(t *testing.T) {
    48  	cases := []struct {
    49  		delta time.Duration
    50  	}{
    51  		{delta: 10 * time.Millisecond},
    52  		{delta: 0 * time.Millisecond},
    53  		{delta: -10 * time.Millisecond},
    54  	}
    55  
    56  	startTime := time.Now()
    57  	duration := time.Millisecond
    58  	sic := &SimpleIntervalClock{
    59  		Time:     startTime,
    60  		Duration: duration,
    61  	}
    62  
    63  	for _, c := range cases {
    64  		// Try and add compute a "since" time by
    65  		// Add()ing a -c.delta.
    66  		timeSinceDelta := startTime.Add(-c.delta)
    67  		expectedDelta := sic.Since(timeSinceDelta)
    68  		if expectedDelta != c.delta {
    69  			t.Errorf("expected %#v, got %#v", expectedDelta, c.delta)
    70  		}
    71  	}
    72  }
    73  

View as plain text