k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/controller/tainteviction/timed_workers_test.go (about)

     1  /*
     2  Copyright 2017 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 tainteviction
    18  
    19  import (
    20  	"context"
    21  	"sync"
    22  	"sync/atomic"
    23  	"testing"
    24  	"time"
    25  
    26  	"k8s.io/klog/v2/ktesting"
    27  	testingclock "k8s.io/utils/clock/testing"
    28  )
    29  
    30  func TestExecute(t *testing.T) {
    31  	testVal := int32(0)
    32  	wg := sync.WaitGroup{}
    33  	wg.Add(5)
    34  	queue := CreateWorkerQueue(func(ctx context.Context, fireAt time.Time, args *WorkArgs) error {
    35  		atomic.AddInt32(&testVal, 1)
    36  		wg.Done()
    37  		return nil
    38  	})
    39  	now := time.Now()
    40  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, now)
    41  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, now)
    42  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, now)
    43  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, now)
    44  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, now)
    45  	// Adding the same thing second time should be no-op
    46  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, now)
    47  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, now)
    48  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, now)
    49  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, now)
    50  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, now)
    51  	wg.Wait()
    52  	lastVal := atomic.LoadInt32(&testVal)
    53  	if lastVal != 5 {
    54  		t.Errorf("Expected testVal = 5, got %v", lastVal)
    55  	}
    56  }
    57  
    58  func TestExecuteDelayed(t *testing.T) {
    59  	testVal := int32(0)
    60  	wg := sync.WaitGroup{}
    61  	wg.Add(5)
    62  	queue := CreateWorkerQueue(func(ctx context.Context, fireAt time.Time, args *WorkArgs) error {
    63  		atomic.AddInt32(&testVal, 1)
    64  		wg.Done()
    65  		return nil
    66  	})
    67  	now := time.Now()
    68  	then := now.Add(10 * time.Second)
    69  	fakeClock := testingclock.NewFakeClock(now)
    70  	queue.clock = fakeClock
    71  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
    72  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
    73  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
    74  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
    75  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
    76  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
    77  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
    78  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
    79  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
    80  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
    81  	fakeClock.Step(11 * time.Second)
    82  	wg.Wait()
    83  	lastVal := atomic.LoadInt32(&testVal)
    84  	if lastVal != 5 {
    85  		t.Errorf("Expected testVal = 5, got %v", lastVal)
    86  	}
    87  }
    88  
    89  func TestCancel(t *testing.T) {
    90  	testVal := int32(0)
    91  	wg := sync.WaitGroup{}
    92  	wg.Add(3)
    93  	queue := CreateWorkerQueue(func(ctx context.Context, fireAt time.Time, args *WorkArgs) error {
    94  		atomic.AddInt32(&testVal, 1)
    95  		wg.Done()
    96  		return nil
    97  	})
    98  	now := time.Now()
    99  	then := now.Add(10 * time.Second)
   100  	fakeClock := testingclock.NewFakeClock(now)
   101  	queue.clock = fakeClock
   102  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
   103  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
   104  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
   105  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
   106  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
   107  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
   108  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
   109  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
   110  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
   111  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
   112  	logger, _ := ktesting.NewTestContext(t)
   113  	queue.CancelWork(logger, NewWorkArgs("2", "2").KeyFromWorkArgs())
   114  	queue.CancelWork(logger, NewWorkArgs("4", "4").KeyFromWorkArgs())
   115  	fakeClock.Step(11 * time.Second)
   116  	wg.Wait()
   117  	lastVal := atomic.LoadInt32(&testVal)
   118  	if lastVal != 3 {
   119  		t.Errorf("Expected testVal = 3, got %v", lastVal)
   120  	}
   121  }
   122  
   123  func TestCancelAndReadd(t *testing.T) {
   124  	testVal := int32(0)
   125  	wg := sync.WaitGroup{}
   126  	wg.Add(4)
   127  	queue := CreateWorkerQueue(func(ctx context.Context, fireAt time.Time, args *WorkArgs) error {
   128  		atomic.AddInt32(&testVal, 1)
   129  		wg.Done()
   130  		return nil
   131  	})
   132  	now := time.Now()
   133  	then := now.Add(10 * time.Second)
   134  	fakeClock := testingclock.NewFakeClock(now)
   135  	queue.clock = fakeClock
   136  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
   137  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
   138  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
   139  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
   140  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
   141  	queue.AddWork(context.TODO(), NewWorkArgs("1", "1"), now, then)
   142  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
   143  	queue.AddWork(context.TODO(), NewWorkArgs("3", "3"), now, then)
   144  	queue.AddWork(context.TODO(), NewWorkArgs("4", "4"), now, then)
   145  	queue.AddWork(context.TODO(), NewWorkArgs("5", "5"), now, then)
   146  	logger, _ := ktesting.NewTestContext(t)
   147  	queue.CancelWork(logger, NewWorkArgs("2", "2").KeyFromWorkArgs())
   148  	queue.CancelWork(logger, NewWorkArgs("4", "4").KeyFromWorkArgs())
   149  	queue.AddWork(context.TODO(), NewWorkArgs("2", "2"), now, then)
   150  	fakeClock.Step(11 * time.Second)
   151  	wg.Wait()
   152  	lastVal := atomic.LoadInt32(&testVal)
   153  	if lastVal != 4 {
   154  		t.Errorf("Expected testVal = 4, got %v", lastVal)
   155  	}
   156  }