|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/jack-work/figaro/api/rpc" |
| 9 | + "github.com/jack-work/figaro/internal/livelog/aria" |
| 10 | +) |
| 11 | + |
| 12 | +// ISSUE #25, AT THE SEAM WHERE IT LIVED. |
| 13 | +// |
| 14 | +// The pager attends a parent. `:fork -- test` prompts a branch and then hops |
| 15 | +// to it. The hop takes its generation, dials the branch (which is subscribed |
| 16 | +// on accept and starts streaming at once), and only then waits for the lineage |
| 17 | +// read before claimSubject swaps the client. The branch's opening question, |
| 18 | +// an OpenInquiry frame for the turn after the parent's last, arrives in that |
| 19 | +// gap. It passed the generation check and was folded into the PARENT's |
| 20 | +// client, which claimSubject then shelved; coming back to the parent restored |
| 21 | +// it from the shelf and painted the branch's question at the end of the |
| 22 | +// parent's reply. |
| 23 | +// |
| 24 | +// Captured on a real tape (Gluck, 2026-10-10): the turn-10 frame at t=66.578, |
| 25 | +// beside form deltas naming the branch, before the client's first read for it |
| 26 | +// at t=66.617. This drives the same order without a wire. |
| 27 | +func TestHop_AFrameFromTheAriaBeingDialledNeverReachesTheOneBeingLeft(t *testing.T) { |
| 28 | + in := fenceSession(t) |
| 29 | + const parent, branch = "parentAA", "branchBB" |
| 30 | + |
| 31 | + // The parent is on screen, claimed, with three turns. |
| 32 | + gen := atomic.AddUint64(&in.subjectGen, 1) |
| 33 | + if _, _, ok := in.claimSubject(gen, parent, "", nil, 0); !ok { |
| 34 | + t.Fatal("could not claim the parent") |
| 35 | + } |
| 36 | + in.lt.apply(aria.Page{Parts: partsFor(1, 3, "PARENT")}) |
| 37 | + |
| 38 | + // The hop to the branch begins: a new generation, a connection dialled for |
| 39 | + // the branch with its handler, and no claim yet. |
| 40 | + gen = atomic.AddUint64(&in.subjectGen, 1) |
| 41 | + handler := in.notifyHandler(gen, branch) |
| 42 | + |
| 43 | + // The branch opens its first turn: the question alone, as |
| 44 | + // Server.OpenInquiry broadcasts it. |
| 45 | + frame, err := json.Marshal(aria.Page{Parts: []aria.TurnPart{{Turn: aria.Turn{ID: 4, Inquiry: "test"}}}}) |
| 46 | + if err != nil { |
| 47 | + t.Fatal(err) |
| 48 | + } |
| 49 | + handler(rpc.MethodAriaFrame, frame) |
| 50 | + |
| 51 | + if inq, ok := in.lt.client.InquiryOf(4); ok { |
| 52 | + t.Fatalf("the branch's question %q landed in the parent's client before the hop claimed the branch", inq.Text) |
| 53 | + } |
| 54 | + if turn, _, ok := in.lt.client.OpenKey(); ok && turn == 4 { |
| 55 | + t.Fatal("the parent's client holds the branch's turn 4 open") |
| 56 | + } |
| 57 | + |
| 58 | + // The claim: the parent goes on the shelf, and must go there clean. |
| 59 | + if _, _, ok := in.claimSubject(gen, branch, parent, nil, 3); !ok { |
| 60 | + t.Fatal("could not claim the branch") |
| 61 | + } |
| 62 | + shelved := in.parked[parent] |
| 63 | + if shelved == nil { |
| 64 | + t.Fatal("the parent was not shelved") |
| 65 | + } |
| 66 | + if _, ok := shelved.client.InquiryOf(4); ok { |
| 67 | + t.Fatal("the parent went onto the shelf carrying the branch's question: the hop back will paint it") |
| 68 | + } |
| 69 | + |
| 70 | + // After the claim the same frame is the branch's own, and is folded. |
| 71 | + handler(rpc.MethodAriaFrame, frame) |
| 72 | + if _, ok := in.lt.client.InquiryOf(4); !ok { |
| 73 | + t.Fatal("after the claim the branch's own question was dropped: the fence is too wide") |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// A connection whose switch was SUPERSEDED stays mute even if its aria is |
| 78 | +// later claimed by another switch: the generation still fences it. The id |
| 79 | +// check is added to the generation, it does not replace it. |
| 80 | +func TestHop_TheIdentityFenceDoesNotReplaceTheGeneration(t *testing.T) { |
| 81 | + in := fenceSession(t) |
| 82 | + stale := atomic.AddUint64(&in.subjectGen, 1) |
| 83 | + old := in.notifyHandler(stale, "ariaCCCC") |
| 84 | + fresh := atomic.AddUint64(&in.subjectGen, 1) |
| 85 | + if _, _, ok := in.claimSubject(fresh, "ariaCCCC", "", nil, 0); !ok { |
| 86 | + t.Fatal("could not claim") |
| 87 | + } |
| 88 | + frame, _ := json.Marshal(aria.Page{Parts: partsFor(1, 1, "STALE")}) |
| 89 | + old(rpc.MethodAriaFrame, frame) |
| 90 | + if in.lt.client.Count() != 0 { |
| 91 | + t.Fatal("a connection from a superseded switch folded a frame") |
| 92 | + } |
| 93 | +} |
0 commit comments