|
| 1 | +/* |
| 2 | +Copyright 2024 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 slowset |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +func TestSlowSet(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + retentionTime time.Duration |
| 28 | + resyncPeriod time.Duration |
| 29 | + testFunc func(*SlowSet) bool |
| 30 | + }{ |
| 31 | + { |
| 32 | + name: "Should not change time of a key if added multiple times", |
| 33 | + resyncPeriod: 100 * time.Millisecond, |
| 34 | + testFunc: func(s *SlowSet) bool { |
| 35 | + key := "key" |
| 36 | + info := ObjectData{ |
| 37 | + Timestamp: time.Now(), |
| 38 | + } |
| 39 | + s.Add(key, info) |
| 40 | + time1 := s.workSet[key] |
| 41 | + s.Add(key, info) |
| 42 | + time2 := s.workSet[key] |
| 43 | + return time1 == time2 |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "Should remove key after retention time", |
| 48 | + retentionTime: 200 * time.Millisecond, |
| 49 | + resyncPeriod: 100 * time.Millisecond, |
| 50 | + testFunc: func(s *SlowSet) bool { |
| 51 | + key := "key" |
| 52 | + info := ObjectData{ |
| 53 | + Timestamp: time.Now(), |
| 54 | + } |
| 55 | + s.Add(key, info) |
| 56 | + time.Sleep(300 * time.Millisecond) |
| 57 | + return !s.Contains(key) |
| 58 | + }, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "Should not remove key before retention time", |
| 62 | + retentionTime: 200 * time.Millisecond, |
| 63 | + resyncPeriod: 100 * time.Millisecond, |
| 64 | + testFunc: func(s *SlowSet) bool { |
| 65 | + key := "key" |
| 66 | + info := ObjectData{ |
| 67 | + Timestamp: time.Now(), |
| 68 | + } |
| 69 | + s.Add(key, info) |
| 70 | + time.Sleep(100 * time.Millisecond) |
| 71 | + return s.Contains(key) |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "Should return time remaining for added keys", |
| 76 | + retentionTime: 300 * time.Millisecond, |
| 77 | + resyncPeriod: 100 * time.Millisecond, |
| 78 | + testFunc: func(s *SlowSet) bool { |
| 79 | + key := "key" |
| 80 | + info := ObjectData{ |
| 81 | + Timestamp: time.Now(), |
| 82 | + } |
| 83 | + s.Add(key, info) |
| 84 | + time.Sleep(100 * time.Millisecond) |
| 85 | + timeRemaining := s.TimeRemaining(key) |
| 86 | + return timeRemaining > 0 && timeRemaining < 300*time.Millisecond |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "should return false for Contains if key is present but expired", |
| 91 | + resyncPeriod: 200 * time.Millisecond, |
| 92 | + retentionTime: 300 * time.Millisecond, |
| 93 | + testFunc: func(s *SlowSet) bool { |
| 94 | + key := "key" |
| 95 | + info := ObjectData{ |
| 96 | + Timestamp: time.Now(), |
| 97 | + } |
| 98 | + s.Add(key, info) |
| 99 | + time.Sleep(301 * time.Millisecond) |
| 100 | + return !s.Contains(key) |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for i := range tests { |
| 106 | + test := tests[i] |
| 107 | + t.Run(test.name, func(t *testing.T) { |
| 108 | + s := NewSlowSet(test.retentionTime) |
| 109 | + s.resyncPeriod = test.resyncPeriod |
| 110 | + stopCh := make(chan struct{}, 1) |
| 111 | + go s.Run(stopCh) |
| 112 | + defer close(stopCh) |
| 113 | + if !test.testFunc(s) { |
| 114 | + t.Errorf("Test failed") |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments