| 1 |
/* |
| 2 |
Copyright The containerd 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 task |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"fmt" |
| 22 |
"sync" |
| 23 |
"testing" |
| 24 |
"time" |
| 25 |
) |
| 26 |
|
| 27 |
// TestBackgroundTasks tests background task manager. |
| 28 |
func TestBackgroundTasks(t *testing.T) { |
| 29 |
doGo := func(f func()) { |
| 30 |
invoked := make(chan struct{}) |
| 31 |
go func() { |
| 32 |
invoked <- struct{}{} |
| 33 |
f() |
| 34 |
}() |
| 35 |
<-invoked |
| 36 |
time.Sleep(10 * time.Millisecond) |
| 37 |
} |
| 38 |
|
| 39 |
wait := func(t *testing.T, name string, done func() bool) { |
| 40 |
ch := make(chan struct{}) |
| 41 |
go func() { |
| 42 |
for !done() { |
| 43 |
time.Sleep(10 * time.Millisecond) |
| 44 |
} |
| 45 |
ch <- struct{}{} |
| 46 |
}() |
| 47 |
select { |
| 48 |
case <-ch: |
| 49 |
case <-time.After(5 * time.Second): |
| 50 |
t.Fatalf("timeout for %q", name) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
tests := []struct { |
| 55 |
name string |
| 56 |
concurrency int64 |
| 57 |
checkInterval time.Duration |
| 58 |
context func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) |
| 59 |
assert func(task1, task2, task3, task4 *sampleTask) bool |
| 60 |
}{ |
| 61 |
{ |
| 62 |
name: "privilege_running", |
| 63 |
concurrency: 1, |
| 64 |
checkInterval: 100 * time.Millisecond, |
| 65 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 66 |
pm.DoPrioritizedTask() |
| 67 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 68 |
time.Sleep(300 * time.Millisecond) // wait for long time... |
| 69 |
}, |
| 70 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 71 |
return (task1.assert(false, false, false)) |
| 72 |
}, |
| 73 |
}, |
| 74 |
{ |
| 75 |
name: "concurrency", |
| 76 |
concurrency: 2, |
| 77 |
checkInterval: time.Duration(0), // We don't care prioritized tasks now |
| 78 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 79 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 80 |
wait(t, "task1 started", task1.checkStarted()) |
| 81 |
doGo(func() { pm.InvokeBackgroundTask(task2.do, 24*time.Hour) }) |
| 82 |
wait(t, "task2 started", task2.checkStarted()) |
| 83 |
doGo(func() { pm.InvokeBackgroundTask(task3.do, 24*time.Hour) }) |
| 84 |
doGo(func() { pm.InvokeBackgroundTask(task4.do, 24*time.Hour) }) |
| 85 |
time.Sleep(300 * time.Millisecond) // wait for long time... |
| 86 |
}, |
| 87 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 88 |
return (task1.assert(true, false, false) && |
| 89 |
task2.assert(true, false, false) && |
| 90 |
task3.assert(false, false, false) && |
| 91 |
task4.assert(false, false, false)) |
| 92 |
}, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "cancel", |
| 96 |
concurrency: 2, |
| 97 |
checkInterval: 100 * time.Millisecond, |
| 98 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 99 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 100 |
wait(t, "task1 started", task1.checkStarted()) |
| 101 |
doGo(func() { pm.InvokeBackgroundTask(task2.do, 24*time.Hour) }) |
| 102 |
wait(t, "task2 started", task2.checkStarted()) |
| 103 |
pm.DoPrioritizedTask() |
| 104 |
wait(t, "task1 canceled", task1.checkCanceled()) |
| 105 |
wait(t, "task2 canceled", task2.checkCanceled()) |
| 106 |
}, |
| 107 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 108 |
return (task1.assert(true, false, true) && |
| 109 |
task2.assert(true, false, true)) |
| 110 |
}, |
| 111 |
}, |
| 112 |
{ |
| 113 |
name: "resume", |
| 114 |
concurrency: 2, |
| 115 |
checkInterval: 100 * time.Millisecond, |
| 116 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 117 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 118 |
wait(t, "task1 started", task1.checkStarted()) |
| 119 |
doGo(func() { pm.InvokeBackgroundTask(task2.do, 24*time.Hour) }) |
| 120 |
wait(t, "task2 started", task2.checkStarted()) |
| 121 |
pm.DoPrioritizedTask() |
| 122 |
wait(t, "task1 canceled", task1.checkCanceled()) |
| 123 |
wait(t, "task2 canceled", task2.checkCanceled()) |
| 124 |
task1.reset() |
| 125 |
task2.reset() |
| 126 |
pm.DonePrioritizedTask() |
| 127 |
wait(t, "task1 resumed", task1.checkStarted()) |
| 128 |
wait(t, "task2 resumed", task2.checkStarted()) |
| 129 |
}, |
| 130 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 131 |
return (task1.assert(true, false, false) && |
| 132 |
task2.assert(true, false, false)) |
| 133 |
}, |
| 134 |
}, |
| 135 |
{ |
| 136 |
name: "finish_partial", |
| 137 |
concurrency: 1, |
| 138 |
checkInterval: time.Duration(0), // We don't care prioritized tasks now |
| 139 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 140 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 141 |
wait(t, "task1 started", task1.checkStarted()) |
| 142 |
doGo(func() { pm.InvokeBackgroundTask(task2.do, 24*time.Hour) }) |
| 143 |
task1.finish() |
| 144 |
wait(t, "task1 done", task1.checkDone()) |
| 145 |
wait(t, "task2 started", task2.checkStarted()) |
| 146 |
}, |
| 147 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 148 |
return (task1.assert(true, true, false) && |
| 149 |
task2.assert(true, false, false)) |
| 150 |
}, |
| 151 |
}, |
| 152 |
{ |
| 153 |
name: "finish_all", |
| 154 |
concurrency: 1, |
| 155 |
checkInterval: time.Duration(0), // We don't care prioritized tasks now |
| 156 |
context: func(t *testing.T, pm *BackgroundTaskManager, task1, task2, task3, task4 *sampleTask) { |
| 157 |
doGo(func() { pm.InvokeBackgroundTask(task1.do, 24*time.Hour) }) |
| 158 |
wait(t, "task1 started", task1.checkStarted()) |
| 159 |
doGo(func() { pm.InvokeBackgroundTask(task2.do, 24*time.Hour) }) |
| 160 |
task1.finish() |
| 161 |
wait(t, "task1 done", task1.checkDone()) |
| 162 |
wait(t, "task2 started", task2.checkStarted()) |
| 163 |
task2.finish() |
| 164 |
wait(t, "task2 done", task2.checkDone()) |
| 165 |
}, |
| 166 |
assert: func(task1, task2, task3, task4 *sampleTask) bool { |
| 167 |
return (task1.assert(true, true, false) && |
| 168 |
task2.assert(true, true, false)) |
| 169 |
}, |
| 170 |
}, |
| 171 |
} |
| 172 |
|
| 173 |
for _, tt := range tests { |
| 174 |
t.Run(tt.name, func(t *testing.T) { |
| 175 |
var ( |
| 176 |
pm = NewBackgroundTaskManager(tt.concurrency, tt.checkInterval) |
| 177 |
task1 = newSampleTask() |
| 178 |
task2 = newSampleTask() |
| 179 |
task3 = newSampleTask() |
| 180 |
task4 = newSampleTask() |
| 181 |
) |
| 182 |
tt.context(t, pm, task1, task2, task3, task4) |
| 183 |
if !tt.assert(task1, task2, task3, task4) { |
| 184 |
t.Errorf("assertion failed:status=(task1:%q,task2:%q,task3:%q,task4:%q)", |
| 185 |
task1.dumpStatus(), task2.dumpStatus(), |
| 186 |
task3.dumpStatus(), task4.dumpStatus()) |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
type sampleTask struct { |
| 193 |
started bool |
| 194 |
done bool |
| 195 |
canceled bool |
| 196 |
mu sync.Mutex |
| 197 |
|
| 198 |
finishCh chan struct{} |
| 199 |
} |
| 200 |
|
| 201 |
func newSampleTask() *sampleTask { |
| 202 |
return &sampleTask{ |
| 203 |
finishCh: make(chan struct{}), |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
func (st *sampleTask) checkStarted() func() bool { |
| 208 |
return func() bool { |
| 209 |
st.mu.Lock() |
| 210 |
defer st.mu.Unlock() |
| 211 |
return st.started |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
func (st *sampleTask) checkDone() func() bool { |
| 216 |
return func() bool { |
| 217 |
st.mu.Lock() |
| 218 |
defer st.mu.Unlock() |
| 219 |
return st.done |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func (st *sampleTask) checkCanceled() func() bool { |
| 224 |
return func() bool { |
| 225 |
st.mu.Lock() |
| 226 |
defer st.mu.Unlock() |
| 227 |
return st.canceled |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
func (st *sampleTask) do(ctx context.Context) { |
| 232 |
st.mu.Lock() |
| 233 |
st.started = true |
| 234 |
st.mu.Unlock() |
| 235 |
select { |
| 236 |
case <-st.finishCh: |
| 237 |
st.mu.Lock() |
| 238 |
st.done = true |
| 239 |
st.mu.Unlock() |
| 240 |
case <-ctx.Done(): |
| 241 |
st.mu.Lock() |
| 242 |
st.canceled = true |
| 243 |
st.mu.Unlock() |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
func (st *sampleTask) finish() { |
| 248 |
st.finishCh <- struct{}{} |
| 249 |
} |
| 250 |
|
| 251 |
func (st *sampleTask) reset() { |
| 252 |
st.mu.Lock() |
| 253 |
st.started, st.done, st.canceled = false, false, false |
| 254 |
st.mu.Unlock() |
| 255 |
st.finishCh = make(chan struct{}) |
| 256 |
} |
| 257 |
|
| 258 |
func (st *sampleTask) dumpStatus() string { |
| 259 |
st.mu.Lock() |
| 260 |
defer st.mu.Unlock() |
| 261 |
return fmt.Sprintf("started=%v,done=%v,canceled=%v", st.started, st.done, st.canceled) |
| 262 |
} |
| 263 |
|
| 264 |
func (st *sampleTask) assert(started, done, canceled bool) bool { |
| 265 |
st.mu.Lock() |
| 266 |
defer st.mu.Unlock() |
| 267 |
return (st.started == started) && (st.done == done) && (st.canceled == canceled) |
| 268 |
} |