package mac import ( "os" "testing" "amadan.net/oficina/equipo/internal/runner/mac/proc" ) // restarted is a new Turns over turns' lock and debts (a runner restart), // and an executor on it. func restarted(turns *Turns, x *Executor, tb Procs) (*Turns, *Executor) { again := NewTurns(turns.lock).Sweep(tb) again.own, again.Settle, again.boot = turns.own, turns.Settle, turns.boot return again, &Executor{Turns: again, Settle: x.Settle} } // verifyAs is the proof a consumer's recovery asks for before it // commits what it found: a read, whoever asks. func verifyAs(x *Executor, id, _ string) bool { return x.Verify(id) } // Round 3, #1: proving a record after a reboot only reads its // completion. A consumer takes it by acknowledging, once its own result // is durable: a crash between the step's proof and its commit loses // nothing, though the Keychain recovery already took its share. func TestRebootCompletionSurvivesACrashBeforeTheCommit(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, laterBoot, nil) tb.add(row(99400, 1, 99400, 99400, 5000, me, ""), "/Users/paulequipo/w/this-boot") if err := turns.debts.Owe(owed(before)); err != nil { t.Fatal(err) } id := RecordID(99400, "attempt-c") if !verifyAs(x, id, ConsumerKeychain) { t.Fatal("the Keychain recovery's proof failed") } x.Acknowledge(id, ConsumerKeychain) if !verifyAs(x, id, ConsumerStep) { t.Fatal("the step recovery's proof failed") } // The runner dies before the step's transaction commits. again, y := restarted(turns, x, tb) if !y.Verify(id) { t.Fatal("the step's proof was lost to a crash before its commit") } y.Acknowledge(id, ConsumerStep) if again.debts.Owes("attempt-c") { t.Fatal("the completion stayed after every consumer acknowledged it") } if sig := tb.signalled(); len(sig) != 0 { t.Fatalf("signalled %v", sig) } } // Round 3, #2: a reboot's evidence is per marker. Proving launch A of a // turn leaves launch B's evidence, so B, proven after a runner restart, // neither checks nor signals the old boot's group ids that this boot // reuses. func TestRebootEvidencePerMarker(t *testing.T) { me := os.Getuid() tb := parseTable(t, turnBegan) turns, x, _, before := clearRig(t, tb, laterBoot, nil) tb.add(row(99400, 1, 99400, 99400, 5000, me, ""), "/Users/paulequipo/w/this-boot") tb.add(row(99401, 1, 99401, 99401, 5000, me, ""), "/Users/paulequipo/w/this-boot-too") o := proc.Obligation{Markers: []string{"attempt-a", "attempt-b"}, Began: turnStart, BootID: "boot-1", Before: before, FlagsKnown: true, Groups: proc.Span{99400: 1, 99401: 1}} if err := turns.debts.Owe(o); err != nil { t.Fatal(err) } if !x.Verify(RecordID(99400, "attempt-a")) { t.Fatal("A not proven by the reboot") } _, y := restarted(turns, x, tb) if !y.Verify(RecordID(99401, "attempt-b")) { t.Fatal("B lost its reboot evidence when A was proven") } if sig := tb.signalled(); len(sig) != 0 { t.Fatalf("signalled %v", sig) } } // parseRecord is proc.ParseRecord (tests). func parseRecord(id string) (int, string, bool) { return proc.ParseRecord(id) }