返回 DeepSeek-Reasonix
discard_ownership_test.go
根目录 / desktop / internal / draftstate / discard_ownership_test.go
1 package draftstate
2
3 import (
4 "errors"
5 "testing"
6 )
7
8 func TestDiscardAndSubmissionOwnershipOrder(t *testing.T) {
9 for _, submitFirst := range []bool{false, true} {
10 t.Run(map[bool]string{false: "discard-first", true: "submit-first"}[submitFirst], func(t *testing.T) {
11 s := testStore(t)
12 ctx := t.Context()
13 draft, _, err := s.Open(ctx, "workspace", "global", "", "draft", `{}`)
14 if err != nil {
15 t.Fatal(err)
16 }
17 submit := func() error {
18 _, _, err := s.BeginOperation(ctx, Operation{ID: "submit", DraftID: draft.ID, WorkspaceID: draft.WorkspaceID, DraftRevision: draft.Revision, SessionID: "session", SubmissionID: "message", Fingerprint: "input", RequestJSON: `{}`})
19 return err
20 }
21 discard := func() error { return s.Discard(ctx, draft.ID, draft.Revision) }
22 first, second := discard, submit
23 if submitFirst {
24 first, second = submit, discard
25 }
26 // A barrier fixes transaction ownership before the competing request
27 // proceeds; scheduling or repeated test runs cannot choose the winner.
28 claimed := make(chan error, 1)
29 go func() { claimed <- first() }()
30 if err := <-claimed; err != nil {
31 t.Fatal(err)
32 }
33 if err := second(); err == nil {
34 t.Fatal("both discard and first submission acquired ownership")
35 }
36 if submitFirst {
37 for _, phase := range []string{"dispatching", "dispatch_unknown", "cancel_requested"} {
38 if _, err := s.SetOperationPhase(ctx, "submit", phase, ""); err != nil {
39 t.Fatal(err)
40 }
41 if err := discard(); !errors.Is(err, ErrOperationConflict) {
42 t.Fatalf("%s must retain ownership: %v", phase, err)
43 }
44 }
45 }
46 })
47 }
48 }
49
49 lines GO