Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
183 lines
6.0 KiB
Go
183 lines
6.0 KiB
Go
package background
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"academy/internal/types/interfaces"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type countingKeepAliver struct{ calls int }
|
|
|
|
func (k *countingKeepAliver) KeepAlive(context.Context, interfaces.SessionRef) error {
|
|
k.calls++
|
|
return nil
|
|
}
|
|
|
|
type recordingEmitter struct{ finished []interfaces.WorkResult }
|
|
|
|
func (e *recordingEmitter) EmitWorkFinished(
|
|
_ context.Context, _ interfaces.SessionRef, _ string, r interfaces.WorkResult,
|
|
) error {
|
|
e.finished = append(e.finished, r)
|
|
return nil
|
|
}
|
|
|
|
type lifecycleFixture struct {
|
|
l *Lifecycle
|
|
registry *MemoryActivityRegistry
|
|
inbox *MemoryNoticeInbox
|
|
keep *countingKeepAliver
|
|
events *recordingEmitter
|
|
}
|
|
|
|
func newLifecycleFixture() lifecycleFixture {
|
|
f := lifecycleFixture{
|
|
registry: NewMemoryActivityRegistry(nil),
|
|
inbox: NewMemoryNoticeInbox(nil),
|
|
keep: &countingKeepAliver{},
|
|
events: &recordingEmitter{},
|
|
}
|
|
f.l = &Lifecycle{Registry: f.registry, Inbox: f.inbox, KeepAlive: f.keep, Events: f.events}
|
|
return f
|
|
}
|
|
|
|
var lcSess = interfaces.SessionRef{TenantID: 1, SessionID: "s"}
|
|
|
|
func lcActivity(id string) interfaces.BackgroundActivity {
|
|
return interfaces.BackgroundActivity{ID: id, Kind: "shell_job", Title: "make build", StartedAt: time.Now()}
|
|
}
|
|
|
|
func liveIDs(t *testing.T, r interfaces.ActivityRegistry) []string {
|
|
t.Helper()
|
|
items, err := r.List(context.Background(), lcSess)
|
|
require.NoError(t, err)
|
|
ids := make([]string, 0, len(items))
|
|
for _, it := range items {
|
|
ids = append(ids, it.ID)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func TestBeginRegistersBeforeStartAndKeepsAlive(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
var registeredAtStart []string
|
|
err := f.l.Begin(context.Background(), lcSess, lcActivity("sj_1"), time.Minute, 0,
|
|
func(context.Context) error {
|
|
registeredAtStart = liveIDs(t, f.registry)
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
require.Equal(t, []string{"sj_1"}, registeredAtStart)
|
|
require.Equal(t, []string{"sj_1"}, liveIDs(t, f.registry))
|
|
require.Equal(t, 1, f.keep.calls)
|
|
}
|
|
|
|
func TestBeginRemovesRegistrationWhenStartFails(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
boom := errors.New("start failed")
|
|
err := f.l.Begin(context.Background(), lcSess, lcActivity("sj_1"), time.Minute, 0,
|
|
func(context.Context) error { return boom })
|
|
require.ErrorIs(t, err, boom)
|
|
require.Empty(t, liveIDs(t, f.registry))
|
|
require.Zero(t, f.keep.calls)
|
|
}
|
|
|
|
func TestBeginReturnsLimitWithoutStarting(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
require.NoError(t, f.l.Begin(context.Background(), lcSess, lcActivity("sj_1"), time.Minute, 1,
|
|
func(context.Context) error { return nil }))
|
|
started := false
|
|
err := f.l.Begin(context.Background(), lcSess, lcActivity("sj_2"), time.Minute, 1,
|
|
func(context.Context) error { started = true; return nil })
|
|
require.ErrorIs(t, err, interfaces.ErrActivityLimit)
|
|
require.False(t, started)
|
|
}
|
|
|
|
func TestAliveRenewsAndKeepsAliveOnlyWhenFound(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
require.NoError(t, f.registry.Add(context.Background(), lcSess, lcActivity("sj_1"), time.Minute, 0))
|
|
|
|
found, err := f.l.Alive(context.Background(), lcSess, "sj_1", time.Minute)
|
|
require.NoError(t, err)
|
|
require.True(t, found)
|
|
require.Equal(t, 1, f.keep.calls)
|
|
|
|
found, err = f.l.Alive(context.Background(), lcSess, "missing", time.Minute)
|
|
require.NoError(t, err)
|
|
require.False(t, found)
|
|
require.Equal(t, 1, f.keep.calls)
|
|
}
|
|
|
|
func TestFinishPushesEmitsOnceAndRemoves(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
ctx := context.Background()
|
|
require.NoError(t, f.registry.Add(ctx, lcSess, lcActivity("sj_1"), time.Minute, 0))
|
|
n := interfaces.BackgroundNotice{ID: "sj_1", Kind: "shell_job", Text: "done", FinishedAt: time.Now()}
|
|
r := interfaces.WorkResult{ID: "sj_1", State: interfaces.WorkStateSucceeded, Terminal: true}
|
|
|
|
require.NoError(t, f.l.Finish(ctx, lcSess, n, r, "msg-1"))
|
|
require.NoError(t, f.l.Finish(ctx, lcSess, n, r, "msg-1")) // second reporter
|
|
|
|
require.Len(t, f.events.finished, 1)
|
|
require.Empty(t, liveIDs(t, f.registry))
|
|
pending, err := f.inbox.Pending(ctx, lcSess)
|
|
require.NoError(t, err)
|
|
require.Len(t, pending, 1)
|
|
}
|
|
|
|
func TestFinishKeepsRegistrationWhenPushFails(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
ctx := context.Background()
|
|
require.NoError(t, f.registry.Add(ctx, lcSess, lcActivity("sj_1"), time.Minute, 0))
|
|
f.l.Inbox = failingInbox{}
|
|
|
|
err := f.l.Finish(ctx, lcSess, interfaces.BackgroundNotice{ID: "sj_1"}, interfaces.WorkResult{ID: "sj_1"}, "")
|
|
require.Error(t, err)
|
|
require.Equal(t, []string{"sj_1"}, liveIDs(t, f.registry),
|
|
"without a notice the item must stay visible, or queries would find it nowhere")
|
|
require.Empty(t, f.events.finished)
|
|
}
|
|
|
|
func TestDiscardRemovesWithoutNoticeOrEvent(t *testing.T) {
|
|
f := newLifecycleFixture()
|
|
ctx := context.Background()
|
|
require.NoError(t, f.registry.Add(ctx, lcSess, lcActivity("sj_1"), time.Minute, 0))
|
|
|
|
require.NoError(t, f.l.Discard(ctx, lcSess, "sj_1"))
|
|
|
|
require.Empty(t, liveIDs(t, f.registry))
|
|
pending, err := f.inbox.Pending(ctx, lcSess)
|
|
require.NoError(t, err)
|
|
require.Empty(t, pending)
|
|
require.Empty(t, f.events.finished)
|
|
}
|
|
|
|
func TestLifecycleToleratesNilOptionalParts(t *testing.T) {
|
|
l := &Lifecycle{Registry: NewMemoryActivityRegistry(nil), Inbox: NewMemoryNoticeInbox(nil)}
|
|
ctx := context.Background()
|
|
require.NoError(t, l.Begin(ctx, lcSess, lcActivity("sj_1"), time.Minute, 0,
|
|
func(context.Context) error { return nil }))
|
|
_, err := l.Alive(ctx, lcSess, "sj_1", time.Minute)
|
|
require.NoError(t, err)
|
|
require.NoError(t, l.Finish(ctx, lcSess, interfaces.BackgroundNotice{ID: "sj_1", FinishedAt: time.Now()},
|
|
interfaces.WorkResult{ID: "sj_1"}, ""))
|
|
}
|
|
|
|
type failingInbox struct{}
|
|
|
|
func (failingInbox) Push(context.Context, interfaces.SessionRef, interfaces.BackgroundNotice) (bool, error) {
|
|
return false, errors.New("redis down")
|
|
}
|
|
|
|
func (failingInbox) Pending(context.Context, interfaces.SessionRef) ([]interfaces.BackgroundNotice, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (failingInbox) Ack(context.Context, interfaces.SessionRef, []string) error { return nil }
|
|
|
|
func (failingInbox) Clear(context.Context, interfaces.SessionRef) error { return nil }
|