k8s.io/apiserver@v0.31.1/pkg/util/flowcontrol/fairqueuing/promise/promise_test.go (about)

     1  /*
     2  Copyright 2019 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 promise
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"k8s.io/apimachinery/pkg/util/wait"
    25  )
    26  
    27  func TestWriteOnceSet(t *testing.T) {
    28  	oldTime := time.Now()
    29  	cval := &oldTime
    30  	ctx, cancel := context.WithCancel(context.Background())
    31  	wr := NewWriteOnce(nil, ctx, cval)
    32  	gots := make(chan interface{})
    33  	goGetExpectNotYet(t, wr, gots, "Set")
    34  	now := time.Now()
    35  	aval := &now
    36  	if !wr.Set(aval) {
    37  		t.Error("Set() returned false")
    38  	}
    39  	expectGotValue(t, gots, aval)
    40  	goGetAndExpect(t, wr, gots, aval)
    41  	later := time.Now()
    42  	bval := &later
    43  	if wr.Set(bval) {
    44  		t.Error("second Set() returned true")
    45  	}
    46  	goGetAndExpect(t, wr, gots, aval)
    47  	cancel()
    48  	time.Sleep(time.Second) // give it a chance to misbehave
    49  	goGetAndExpect(t, wr, gots, aval)
    50  }
    51  
    52  func TestWriteOnceCancel(t *testing.T) {
    53  	oldTime := time.Now()
    54  	cval := &oldTime
    55  	ctx, cancel := context.WithCancel(context.Background())
    56  	wr := NewWriteOnce(nil, ctx, cval)
    57  	gots := make(chan interface{})
    58  	goGetExpectNotYet(t, wr, gots, "cancel")
    59  	cancel()
    60  	expectGotValue(t, gots, cval)
    61  	goGetAndExpect(t, wr, gots, cval)
    62  	later := time.Now()
    63  	bval := &later
    64  	if wr.Set(bval) {
    65  		t.Error("Set() after cancel returned true")
    66  	}
    67  	goGetAndExpect(t, wr, gots, cval)
    68  }
    69  
    70  func TestWriteOnceInitial(t *testing.T) {
    71  	oldTime := time.Now()
    72  	cval := &oldTime
    73  	ctx, cancel := context.WithCancel(context.Background())
    74  	now := time.Now()
    75  	aval := &now
    76  	wr := NewWriteOnce(aval, ctx, cval)
    77  	gots := make(chan interface{})
    78  	goGetAndExpect(t, wr, gots, aval)
    79  	later := time.Now()
    80  	bval := &later
    81  	if wr.Set(bval) {
    82  		t.Error("Set of initialized promise returned true")
    83  	}
    84  	goGetAndExpect(t, wr, gots, aval)
    85  	cancel()
    86  	time.Sleep(time.Second) // give it a chance to misbehave
    87  	goGetAndExpect(t, wr, gots, aval)
    88  }
    89  
    90  func goGetExpectNotYet(t *testing.T, wr WriteOnce, gots chan interface{}, trigger string) {
    91  	go func() {
    92  		gots <- wr.Get()
    93  	}()
    94  	select {
    95  	case <-gots:
    96  		t.Errorf("Get returned before %s", trigger)
    97  	case <-time.After(time.Second):
    98  		t.Log("Good: Get did not return yet")
    99  	}
   100  }
   101  
   102  func goGetAndExpect(t *testing.T, wr WriteOnce, gots chan interface{}, expected interface{}) {
   103  	go func() {
   104  		gots <- wr.Get()
   105  	}()
   106  	expectGotValue(t, gots, expected)
   107  }
   108  
   109  func expectGotValue(t *testing.T, gots <-chan interface{}, expected interface{}) {
   110  	select {
   111  	case gotVal := <-gots:
   112  		t.Logf("Got %v", gotVal)
   113  		if gotVal != expected {
   114  			t.Errorf("Get returned %v, expected: %v", gotVal, expected)
   115  		}
   116  	case <-time.After(wait.ForeverTestTimeout):
   117  		t.Error("Get did not return")
   118  	}
   119  }