54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package reaper_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"lincodex/labd/internal/reaper"
|
|
)
|
|
|
|
type target struct {
|
|
sweeps atomic.Int32
|
|
reconciles atomic.Int32
|
|
}
|
|
|
|
func (t *target) Sweep(context.Context) { t.sweeps.Add(1) }
|
|
|
|
func (t *target) Reconcile(context.Context) error {
|
|
t.reconciles.Add(1)
|
|
return errors.New("lxd unreachable")
|
|
}
|
|
|
|
func TestRunSweepsAndReconcilesUntilCancelled(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
work := &target{}
|
|
var failures atomic.Int32
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
reaper.Run(ctx, work, reaper.Config{SweepInterval: time.Millisecond, ReconcileInterval: 2 * time.Millisecond},
|
|
func(err error) {
|
|
if err != nil {
|
|
failures.Add(1)
|
|
}
|
|
}, slog.New(slog.DiscardHandler))
|
|
}()
|
|
deadline := time.Now().Add(5 * time.Second)
|
|
for work.sweeps.Load() < 3 || failures.Load() < 2 {
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("sweeps %d, failed reconciliations %d", work.sweeps.Load(), failures.Load())
|
|
}
|
|
time.Sleep(time.Millisecond)
|
|
}
|
|
cancel()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(5 * time.Second):
|
|
t.Fatal("Run() did not return after cancellation")
|
|
}
|
|
}
|