github.com/abayer/test-infra@v0.0.5/mungegithub/mungers/mungerutil/time_cache_test.go (about)

     1  /*
     2  Copyright 2016 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 mungerutil
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  )
    23  
    24  type testLTG struct {
    25  	number int
    26  	label  string
    27  	time   time.Time
    28  }
    29  
    30  func (t *testLTG) Number() int { return t.number }
    31  func (t *testLTG) FirstLabelTime(label string) *time.Time {
    32  	if label == t.label {
    33  		return &t.time
    34  	}
    35  	return nil
    36  }
    37  
    38  func TestFirstLabelCache(t *testing.T) {
    39  	timeA := time.Now()
    40  	timeB := timeA.Add(time.Minute)
    41  	table := []struct {
    42  		obj    testLTG
    43  		expect time.Time
    44  	}{
    45  		// Returns the time and caches it.
    46  		{testLTG{1, "lgtm", timeA}, timeA},
    47  
    48  		// Returns zero time, does not cache.
    49  		{testLTG{2, "blah", timeA}, time.Time{}},
    50  
    51  		// Returns old cached value. (Note, in reality the values we
    52  		// cache should not change.)
    53  		{testLTG{1, "lgtm", timeB}, timeA},
    54  
    55  		// The empty time was not cached.
    56  		{testLTG{2, "lgtm", timeB}, timeB},
    57  	}
    58  
    59  	cache := NewLabelTimeCache("lgtm")
    60  	for i, tt := range table {
    61  		got, ok := cache.FirstLabelTime(&tt.obj)
    62  		if e := (tt.expect != time.Time{}); e != ok {
    63  			t.Errorf("%v: Expected %v, got %v", i, e, ok)
    64  			continue
    65  		}
    66  		if got != tt.expect {
    67  			t.Errorf("%v: Expected %v, got %v", i, tt.expect, got)
    68  		}
    69  	}
    70  }