| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"sync/atomic" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
) |
| 9 |
|
| 10 |
func TestJanitor(t *testing.T) { |
| 11 |
ctx, cancel := context.WithCancel(context.Background()) |
| 12 |
defer cancel() |
| 13 |
|
| 14 |
janitor := newJanitor(ctx, time.Millisecond) |
| 15 |
|
| 16 |
checkDone := make(chan struct{}) |
| 17 |
janitor.done = checkDone |
| 18 |
|
| 19 |
calledClean := int64(0) |
| 20 |
janitor.run(func() { atomic.AddInt64(&calledClean, 1) }) |
| 21 |
|
| 22 |
|
| 23 |
time.Sleep(10 * time.Millisecond) |
| 24 |
cancel() |
| 25 |
|
| 26 |
select { |
| 27 |
case <-checkDone: |
| 28 |
case <-time.After(time.Second): |
| 29 |
t.Fatalf("failed to call done channel") |
| 30 |
} |
| 31 |
|
| 32 |
got := atomic.LoadInt64(&calledClean) |
| 33 |
if got <= 1 { |
| 34 |
t.Fatalf("failed to call clean callback in janitor: %d", got) |
| 35 |
} |
| 36 |
} |