feat: self-update of master and agents (#15)
This commit is contained in:
@@ -29,7 +29,7 @@ func newFakeExec() *fakeExec {
|
||||
return &fakeExec{gate: make(chan struct{}), started: make(chan string, 10)}
|
||||
}
|
||||
|
||||
func (f *fakeExec) Execute(ctx context.Context, job protocol.JobStart, out io.Writer) (string, error) {
|
||||
func (f *fakeExec) Execute(ctx context.Context, job Job, out io.Writer) (string, error) {
|
||||
n := f.cur.Add(1)
|
||||
defer f.cur.Add(-1)
|
||||
for {
|
||||
@@ -148,7 +148,7 @@ func TestRunner_TakeProgress(t *testing.T) {
|
||||
func TestRunner_TakeProgressReportsStartedJobOnce(t *testing.T) {
|
||||
gate := make(chan struct{})
|
||||
started := make(chan struct{})
|
||||
r := startRunner(t, execFunc(func(ctx context.Context, _ protocol.JobStart, _ io.Writer) (string, error) {
|
||||
r := startRunner(t, execFunc(func(ctx context.Context, _ Job, _ io.Writer) (string, error) {
|
||||
close(started)
|
||||
<-gate
|
||||
return "x", nil
|
||||
@@ -160,14 +160,14 @@ func TestRunner_TakeProgressReportsStartedJobOnce(t *testing.T) {
|
||||
close(gate)
|
||||
}
|
||||
|
||||
type execFunc func(ctx context.Context, job protocol.JobStart, out io.Writer) (string, error)
|
||||
type execFunc func(ctx context.Context, job Job, out io.Writer) (string, error)
|
||||
|
||||
func (f execFunc) Execute(ctx context.Context, job protocol.JobStart, out io.Writer) (string, error) {
|
||||
func (f execFunc) Execute(ctx context.Context, job Job, out io.Writer) (string, error) {
|
||||
return f(ctx, job, out)
|
||||
}
|
||||
|
||||
func TestRunner_Timeout(t *testing.T) {
|
||||
r := startRunner(t, execFunc(func(ctx context.Context, _ protocol.JobStart, _ io.Writer) (string, error) {
|
||||
r := startRunner(t, execFunc(func(ctx context.Context, _ Job, _ io.Writer) (string, error) {
|
||||
<-ctx.Done()
|
||||
return "", ctx.Err()
|
||||
}), 20*time.Millisecond)
|
||||
@@ -178,7 +178,7 @@ func TestRunner_Timeout(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunner_OutputCapped(t *testing.T) {
|
||||
r := startRunner(t, execFunc(func(_ context.Context, _ protocol.JobStart, out io.Writer) (string, error) {
|
||||
r := startRunner(t, execFunc(func(_ context.Context, _ Job, out io.Writer) (string, error) {
|
||||
io.WriteString(out, strings.Repeat("a", 10))
|
||||
io.WriteString(out, strings.Repeat("b", protocol.MaxJobOutput))
|
||||
return "x", nil
|
||||
@@ -190,7 +190,7 @@ func TestRunner_OutputCapped(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRunner_ChangedSignals(t *testing.T) {
|
||||
r := startRunner(t, execFunc(func(context.Context, protocol.JobStart, io.Writer) (string, error) { return "x", nil }), time.Minute)
|
||||
r := startRunner(t, execFunc(func(context.Context, Job, io.Writer) (string, error) { return "x", nil }), time.Minute)
|
||||
r.Submit("1", protocol.JobStart{Ref: "a:1"})
|
||||
select {
|
||||
case <-r.Changed():
|
||||
@@ -198,3 +198,105 @@ func TestRunner_ChangedSignals(t *testing.T) {
|
||||
t.Fatal("no change signal")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunner_ResumeJumpsTheQueue(t *testing.T) {
|
||||
fe := newFakeExec()
|
||||
r := NewRunner(fe, time.Minute)
|
||||
r.Submit("1", protocol.JobStart{Ref: "a:1"})
|
||||
started := time.Date(2026, 10, 13, 9, 0, 0, 0, time.UTC)
|
||||
r.Resume("9", started, func(_ context.Context, out io.Writer) (string, error) {
|
||||
io.WriteString(out, "from the helper\n")
|
||||
return "resumed@sha256:bb", nil
|
||||
})
|
||||
assert.Equal(t, []string{"1", "9"}, r.Known(), "known before the worker starts")
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); r.Run(ctx) }()
|
||||
t.Cleanup(func() { cancel(); wg.Wait() })
|
||||
|
||||
got := waitPending(t, r, 1)
|
||||
require.Len(t, got, 1, "the resumed job runs first; job 1 waits on the gate")
|
||||
assert.Equal(t, "9", got[0].ID)
|
||||
assert.Equal(t, protocol.ResultSucceeded, got[0].Result.Status)
|
||||
assert.Equal(t, "resumed@sha256:bb", got[0].Result.Ref)
|
||||
assert.Equal(t, "from the helper\n", got[0].Result.Output)
|
||||
assert.True(t, started.Equal(got[0].Result.StartedAt))
|
||||
|
||||
r.Resume("9", started, nil) // already held: ignored
|
||||
assert.Equal(t, "a:1", <-fe.started)
|
||||
fe.gate <- struct{}{}
|
||||
waitPending(t, r, 2)
|
||||
}
|
||||
|
||||
func TestRunner_ResumedJobFailure(t *testing.T) {
|
||||
r := startRunner(t, newFakeExec(), time.Minute)
|
||||
r.Resume("9", time.Now(), func(context.Context, io.Writer) (string, error) { return "", errors.New("helper failed") })
|
||||
got := waitPending(t, r, 1)
|
||||
assert.Equal(t, protocol.ResultFailed, got[0].Result.Status)
|
||||
assert.Equal(t, "helper failed", got[0].Result.Error)
|
||||
}
|
||||
|
||||
func TestRunner_SelfJobsRunLast(t *testing.T) {
|
||||
fe := newFakeExec()
|
||||
r := NewRunner(fe, time.Minute)
|
||||
r.Submit("1", protocol.JobStart{Ref: "self:1", Self: true})
|
||||
r.Submit("2", protocol.JobStart{Ref: "a:2"})
|
||||
r.Submit("3", protocol.JobStart{Ref: "self:3", Self: true})
|
||||
r.Submit("4", protocol.JobStart{Ref: "a:4"})
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() { defer wg.Done(); r.Run(ctx) }()
|
||||
t.Cleanup(func() { cancel(); wg.Wait() })
|
||||
|
||||
var order []string
|
||||
for i := 0; i < 4; i++ {
|
||||
order = append(order, <-fe.started)
|
||||
fe.gate <- struct{}{}
|
||||
}
|
||||
assert.Equal(t, []string{"a:2", "a:4", "self:1", "self:3"}, order)
|
||||
}
|
||||
|
||||
func TestRunner_SelfJobRunsWhenAloneAndNonSelfSubmittedLater(t *testing.T) {
|
||||
fe := newFakeExec()
|
||||
r := startRunner(t, fe, time.Minute)
|
||||
r.Submit("1", protocol.JobStart{Ref: "self:1", Self: true})
|
||||
assert.Equal(t, "self:1", <-fe.started)
|
||||
fe.gate <- struct{}{}
|
||||
waitPending(t, r, 1)
|
||||
}
|
||||
|
||||
func TestRunner_StoppingTheAgentRecordsNoResult(t *testing.T) {
|
||||
r := NewRunner(execFunc(func(ctx context.Context, _ Job, _ io.Writer) (string, error) {
|
||||
<-ctx.Done()
|
||||
return "", ctx.Err()
|
||||
}), time.Minute)
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() { r.Run(ctx); close(done) }()
|
||||
r.Submit("1", protocol.JobStart{Ref: "a:1"})
|
||||
require.Eventually(t, func() bool { return len(r.TakeProgress()) > 0 }, time.Second, 5*time.Millisecond)
|
||||
|
||||
cancel()
|
||||
<-done
|
||||
assert.Empty(t, r.Pending(), "no \"context canceled\" result")
|
||||
assert.Empty(t, r.Known(), "the job is dropped, so the master marks it lost unless a successor resumes it")
|
||||
}
|
||||
|
||||
func TestRunner_ExecutorGetsIDAndStart(t *testing.T) {
|
||||
var got Job
|
||||
r := startRunner(t, execFunc(func(_ context.Context, job Job, _ io.Writer) (string, error) {
|
||||
got = job
|
||||
return "x", nil
|
||||
}), time.Minute)
|
||||
now := time.Date(2026, 10, 13, 9, 0, 0, 0, time.UTC)
|
||||
r.now = func() time.Time { return now }
|
||||
r.Submit("5", protocol.JobStart{Ref: "a:1"})
|
||||
waitPending(t, r, 1)
|
||||
assert.Equal(t, "5", got.ID)
|
||||
assert.Equal(t, "a:1", got.Ref)
|
||||
assert.True(t, now.Equal(got.Started))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user