...

Source file src/k8s.io/kubernetes/pkg/kubelet/util/queue/work_queue_test.go

Documentation: k8s.io/kubernetes/pkg/kubelet/util/queue

     1  /*
     2  Copyright 2015 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 queue
    18  
    19  import (
    20  	"testing"
    21  	"time"
    22  
    23  	"github.com/stretchr/testify/assert"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"k8s.io/apimachinery/pkg/util/sets"
    26  	"k8s.io/utils/clock"
    27  	testingclock "k8s.io/utils/clock/testing"
    28  )
    29  
    30  func newTestBasicWorkQueue() (*basicWorkQueue, *testingclock.FakeClock) {
    31  	fakeClock := testingclock.NewFakeClock(time.Now())
    32  	wq := &basicWorkQueue{
    33  		clock: fakeClock,
    34  		queue: make(map[types.UID]time.Time),
    35  	}
    36  	return wq, fakeClock
    37  }
    38  
    39  func compareResults(t *testing.T, expected, actual []types.UID) {
    40  	expectedSet := sets.NewString()
    41  	for _, u := range expected {
    42  		expectedSet.Insert(string(u))
    43  	}
    44  	actualSet := sets.NewString()
    45  	for _, u := range actual {
    46  		actualSet.Insert(string(u))
    47  	}
    48  	if !expectedSet.Equal(actualSet) {
    49  		t.Errorf("Expected %#v, got %#v", expectedSet.List(), actualSet.List())
    50  	}
    51  }
    52  
    53  func TestGetWork(t *testing.T) {
    54  	q, clock := newTestBasicWorkQueue()
    55  	q.Enqueue(types.UID("foo1"), -1*time.Minute)
    56  	q.Enqueue(types.UID("foo2"), -1*time.Minute)
    57  	q.Enqueue(types.UID("foo3"), 1*time.Minute)
    58  	q.Enqueue(types.UID("foo4"), 1*time.Minute)
    59  	expected := []types.UID{types.UID("foo1"), types.UID("foo2")}
    60  	compareResults(t, expected, q.GetWork())
    61  	compareResults(t, []types.UID{}, q.GetWork())
    62  	// Dial the time to 1 hour ahead.
    63  	clock.Step(time.Hour)
    64  	expected = []types.UID{types.UID("foo3"), types.UID("foo4")}
    65  	compareResults(t, expected, q.GetWork())
    66  	compareResults(t, []types.UID{}, q.GetWork())
    67  }
    68  
    69  func TestNewBasicWorkQueue(t *testing.T) {
    70  	tests := []struct {
    71  		clock             clock.Clock
    72  		expectedWorkQueue WorkQueue
    73  	}{
    74  		{
    75  			clock:             clock.RealClock{},
    76  			expectedWorkQueue: &basicWorkQueue{queue: make(map[types.UID]time.Time), clock: clock.RealClock{}},
    77  		},
    78  	}
    79  	for _, test := range tests {
    80  		workQueue := NewBasicWorkQueue(test.clock)
    81  		assert.Equal(t, test.expectedWorkQueue, workQueue)
    82  	}
    83  }
    84  

View as plain text