Files
archie/internal/daemon/running_test.go
T

49 lines
1.4 KiB
Go

package daemon
import (
"context"
"testing"
)
func TestStoppingOnly(t *testing.T) {
tests := []struct {
name string
end func(parent context.CancelFunc, r *runningTasks, run context.Context) context.Context
want bool
}{
{"daemon shutdown", func(parent context.CancelFunc, _ *runningTasks, run context.Context) context.Context {
parent()
return run
}, true},
{"operator stop", func(_ context.CancelFunc, r *runningTasks, run context.Context) context.Context {
r.stop(1)
return run
}, false},
{"container exit", func(_ context.CancelFunc, _ *runningTasks, run context.Context) context.Context {
exited := make(chan struct{})
close(exited)
ctx, _ := withContainerExit(run, closedContainer{exited})
<-ctx.Done()
return ctx
}, false},
{"still running", func(context.CancelFunc, *runningTasks, context.Context) context.Context { return context.Background() }, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
parent, cancel := context.WithCancel(context.Background())
defer cancel()
r := &runningTasks{}
run, done := r.begin(parent, 1, "")
defer done()
if got := stoppingOnly(tt.end(cancel, r, run)); got != tt.want {
t.Fatalf("stoppingOnly = %v, want %v", got, tt.want)
}
})
}
}
type closedContainer struct{ exited chan struct{} }
func (c closedContainer) Exited() <-chan struct{} { return c.exited }
func (closedContainer) ExitDetail() string { return "" }