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