| 1 |
/* |
| 2 |
Copyright 2022 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 cacher |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"errors" |
| 22 |
"sync" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
|
| 26 |
testingclock "k8s.io/utils/clock/testing" |
| 27 |
) |
| 28 |
|
| 29 |
func Test_newReady(t *testing.T) { |
| 30 |
errCh := make(chan error, 10) |
| 31 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 32 |
ready.setError(nil) |
| 33 |
// create 10 goroutines waiting for ready |
| 34 |
for i := 0; i < 10; i++ { |
| 35 |
go func() { |
| 36 |
errCh <- ready.wait(context.Background()) |
| 37 |
}() |
| 38 |
} |
| 39 |
select { |
| 40 |
case <-time.After(1 * time.Second): |
| 41 |
case <-errCh: |
| 42 |
t.Errorf("ready should be blocking") |
| 43 |
} |
| 44 |
ready.setReady() |
| 45 |
for i := 0; i < 10; i++ { |
| 46 |
if err := <-errCh; err != nil { |
| 47 |
t.Errorf("unexpected error on channel %d", i) |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func Test_newReadySetIdempotent(t *testing.T) { |
| 53 |
errCh := make(chan error, 10) |
| 54 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 55 |
ready.setError(nil) |
| 56 |
ready.setError(nil) |
| 57 |
ready.setError(nil) |
| 58 |
if generation, _, err := ready.checkAndReadGeneration(); generation != 0 || err == nil { |
| 59 |
t.Errorf("unexpected state: generation=%v ready=%v", generation, err) |
| 60 |
} |
| 61 |
ready.setReady() |
| 62 |
if generation, _, err := ready.checkAndReadGeneration(); generation != 1 || err != nil { |
| 63 |
t.Errorf("unexpected state: generation=%v ready=%v", generation, err) |
| 64 |
} |
| 65 |
ready.setReady() |
| 66 |
ready.setReady() |
| 67 |
if generation, _, err := ready.checkAndReadGeneration(); generation != 1 || err != nil { |
| 68 |
t.Errorf("unexpected state: generation=%v ready=%v", generation, err) |
| 69 |
} |
| 70 |
ready.setError(nil) |
| 71 |
// create 10 goroutines waiting for ready and stop |
| 72 |
for i := 0; i < 10; i++ { |
| 73 |
go func() { |
| 74 |
errCh <- ready.wait(context.Background()) |
| 75 |
}() |
| 76 |
} |
| 77 |
select { |
| 78 |
case <-time.After(1 * time.Second): |
| 79 |
case <-errCh: |
| 80 |
t.Errorf("ready should be blocking") |
| 81 |
} |
| 82 |
ready.setReady() |
| 83 |
if generation, _, err := ready.checkAndReadGeneration(); generation != 2 || err != nil { |
| 84 |
t.Errorf("unexpected state: generation=%v ready=%v", generation, err) |
| 85 |
} |
| 86 |
for i := 0; i < 10; i++ { |
| 87 |
if err := <-errCh; err != nil { |
| 88 |
t.Errorf("unexpected error on channel %d", i) |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Test_newReadyRacy executes all the possible transitions randomly. |
| 94 |
// It must run with the race detector enabled. |
| 95 |
func Test_newReadyRacy(t *testing.T) { |
| 96 |
concurrency := 1000 |
| 97 |
errCh := make(chan error, concurrency) |
| 98 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 99 |
ready.setError(nil) |
| 100 |
|
| 101 |
wg := sync.WaitGroup{} |
| 102 |
wg.Add(2 * concurrency) |
| 103 |
for i := 0; i < concurrency; i++ { |
| 104 |
go func() { |
| 105 |
errCh <- ready.wait(context.Background()) |
| 106 |
}() |
| 107 |
go func() { |
| 108 |
defer wg.Done() |
| 109 |
ready.setError(nil) |
| 110 |
}() |
| 111 |
go func() { |
| 112 |
defer wg.Done() |
| 113 |
ready.setReady() |
| 114 |
}() |
| 115 |
} |
| 116 |
// Last one has to be set to true. |
| 117 |
wg.Wait() |
| 118 |
ready.setReady() |
| 119 |
|
| 120 |
for i := 0; i < concurrency; i++ { |
| 121 |
if err := <-errCh; err != nil { |
| 122 |
t.Errorf("unexpected error %v on channel %d", err, i) |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
func Test_newReadyStop(t *testing.T) { |
| 128 |
errCh := make(chan error, 10) |
| 129 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 130 |
ready.setError(nil) |
| 131 |
// create 10 goroutines waiting for ready and stop |
| 132 |
for i := 0; i < 10; i++ { |
| 133 |
go func() { |
| 134 |
errCh <- ready.wait(context.Background()) |
| 135 |
}() |
| 136 |
} |
| 137 |
select { |
| 138 |
case <-time.After(1 * time.Second): |
| 139 |
case <-errCh: |
| 140 |
t.Errorf("ready should be blocking") |
| 141 |
} |
| 142 |
ready.stop() |
| 143 |
for i := 0; i < 10; i++ { |
| 144 |
if err := <-errCh; err == nil { |
| 145 |
t.Errorf("unexpected success on channel %d", i) |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func Test_newReadyCheck(t *testing.T) { |
| 151 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 152 |
// it starts as false |
| 153 |
if _, err := ready.check(); err == nil { |
| 154 |
t.Errorf("unexpected ready state %v", err) |
| 155 |
} |
| 156 |
ready.setReady() |
| 157 |
if _, err := ready.check(); err != nil { |
| 158 |
t.Errorf("unexpected ready state %v", err) |
| 159 |
} |
| 160 |
// stop sets ready to false |
| 161 |
ready.stop() |
| 162 |
if _, err := ready.check(); err == nil { |
| 163 |
t.Errorf("unexpected ready state %v", err) |
| 164 |
} |
| 165 |
// can not set to true if is stopped |
| 166 |
ready.setReady() |
| 167 |
if _, err := ready.check(); err == nil { |
| 168 |
t.Errorf("unexpected ready state %v", err) |
| 169 |
} |
| 170 |
err := ready.wait(context.Background()) |
| 171 |
if err == nil { |
| 172 |
t.Errorf("expected error waiting on a stopped state") |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func Test_newReadyCancelPending(t *testing.T) { |
| 177 |
errCh := make(chan error, 10) |
| 178 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 179 |
ready.setError(nil) |
| 180 |
ctx, cancel := context.WithCancel(context.Background()) |
| 181 |
// create 10 goroutines stuck on pending |
| 182 |
for i := 0; i < 10; i++ { |
| 183 |
go func() { |
| 184 |
errCh <- ready.wait(ctx) |
| 185 |
}() |
| 186 |
} |
| 187 |
select { |
| 188 |
case <-time.After(1 * time.Second): |
| 189 |
case <-errCh: |
| 190 |
t.Errorf("ready should be blocking") |
| 191 |
} |
| 192 |
cancel() |
| 193 |
for i := 0; i < 10; i++ { |
| 194 |
if err := <-errCh; err == nil { |
| 195 |
t.Errorf("unexpected success on channel %d", i) |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
func Test_newReadyStateChangeTimestamp(t *testing.T) { |
| 201 |
fakeClock := testingclock.NewFakeClock(time.Now()) |
| 202 |
fakeClock.SetTime(time.Now()) |
| 203 |
|
| 204 |
ready := newReady(fakeClock) |
| 205 |
fakeClock.Step(time.Minute) |
| 206 |
checkReadyTransitionTime(t, ready, time.Minute) |
| 207 |
|
| 208 |
ready.setReady() |
| 209 |
fakeClock.Step(time.Minute) |
| 210 |
checkReadyTransitionTime(t, ready, time.Minute) |
| 211 |
fakeClock.Step(time.Minute) |
| 212 |
checkReadyTransitionTime(t, ready, 2*time.Minute) |
| 213 |
|
| 214 |
ready.setError(nil) |
| 215 |
fakeClock.Step(time.Minute) |
| 216 |
checkReadyTransitionTime(t, ready, time.Minute) |
| 217 |
fakeClock.Step(time.Minute) |
| 218 |
checkReadyTransitionTime(t, ready, 2*time.Minute) |
| 219 |
|
| 220 |
ready.setReady() |
| 221 |
fakeClock.Step(time.Minute) |
| 222 |
checkReadyTransitionTime(t, ready, time.Minute) |
| 223 |
|
| 224 |
ready.stop() |
| 225 |
fakeClock.Step(time.Minute) |
| 226 |
checkReadyTransitionTime(t, ready, time.Minute) |
| 227 |
fakeClock.Step(time.Minute) |
| 228 |
checkReadyTransitionTime(t, ready, 2*time.Minute) |
| 229 |
} |
| 230 |
|
| 231 |
func checkReadyTransitionTime(t *testing.T, r *ready, expectedLastStateChangeDuration time.Duration) { |
| 232 |
if lastStateChangeDuration, _ := r.check(); lastStateChangeDuration != expectedLastStateChangeDuration { |
| 233 |
t.Errorf("unexpected last state change duration: %v, expected: %v", lastStateChangeDuration, expectedLastStateChangeDuration) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
func TestReadyError(t *testing.T) { |
| 238 |
ready := newReady(testingclock.NewFakeClock(time.Now())) |
| 239 |
_, _, err := ready.checkAndReadGeneration() |
| 240 |
if err == nil || err.Error() != "storage is (re)initializing" { |
| 241 |
t.Errorf("Unexpected error when unready, got %q", err) |
| 242 |
} |
| 243 |
ready.setError(errors.New("etcd is down")) |
| 244 |
_, _, err = ready.checkAndReadGeneration() |
| 245 |
if err == nil || err.Error() != "storage is (re)initializing: etcd is down" { |
| 246 |
t.Errorf("Unexpected error when unready, got %q", err) |
| 247 |
} |
| 248 |
ready.setError(nil) |
| 249 |
_, _, err = ready.checkAndReadGeneration() |
| 250 |
if err == nil || err.Error() != "storage is (re)initializing" { |
| 251 |
t.Errorf("Unexpected error when unready, got %q", err) |
| 252 |
} |
| 253 |
} |