2026-08-16 01:27:30 +08:00
|
|
|
package backendupdate
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"yms-daemon/internal/daemonapi"
|
|
|
|
|
"yms-daemon/internal/transaction"
|
|
|
|
|
)
|
|
|
|
|
|
2026-08-17 10:10:14 +08:00
|
|
|
// TestRestartRejectsUnfinishedBackendUpdate 验证当存在未完成的后端更新事务时,Restart 会被拒绝,
|
|
|
|
|
// 并返回 transaction.ErrActiveExists 及该活跃事务的记录。
|
2026-08-16 01:27:30 +08:00
|
|
|
func TestRestartRejectsUnfinishedBackendUpdate(t *testing.T) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
store, err := transaction.OpenStore(ctx, filepath.Join(t.TempDir(), "transactions.db"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("open transaction store: %v", err)
|
|
|
|
|
}
|
|
|
|
|
defer store.Close()
|
|
|
|
|
request, err := json.Marshal(persistedRequest{InputType: daemonapi.InputTypeNativeJAR})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("encode unfinished update request: %v", err)
|
|
|
|
|
}
|
|
|
|
|
active, _, err := store.CreateTransaction(ctx, transaction.CreateRequest{
|
|
|
|
|
ID: "unfinished-backend-update",
|
|
|
|
|
IdempotencyKey: "backend:update:unfinished",
|
|
|
|
|
Source: sourceLocalCLI,
|
|
|
|
|
Service: serviceBackend,
|
|
|
|
|
Request: request,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("create unfinished update transaction: %v", err)
|
|
|
|
|
}
|
|
|
|
|
updater := &Updater{store: store}
|
|
|
|
|
record, err := updater.Restart(ctx, nil)
|
|
|
|
|
if !errors.Is(err, transaction.ErrActiveExists) || record.ID != active.ID {
|
|
|
|
|
t.Fatalf("unexpected restart result with unfinished update: record=%+v err=%v", record, err)
|
|
|
|
|
}
|
|
|
|
|
}
|