package thin import ( "context" "testing" "amadan.net/oficina/equipo/internal/wire" ) // Task 9 follow-up, round 3, #1: the step's recovery acknowledges a // proven exit only once what it found is committed. A recovery that dies // before its commit acknowledges nothing; a later one that commits does, // after; so does the probe once it records the termination. func TestRecoveryAcknowledgesOnlyAfterItsCommit(t *testing.T) { ctx := context.Background() dir := t.TempDir() state, err := OpenState(ctx, dir) if err != nil { t.Fatal(err) } for _, id := range []string{"a1", "b1"} { d := wire.DispatchCommand{Person: connPerson, TaskID: "tk-" + id, StepID: id, Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}} if err := state.AcceptDispatch(ctx, "f-"+id, d); err != nil { t.Fatal(err) } if err := state.setStepNamespace(ctx, stepRow{DispatchCommand: d}, "run-"+id); err != nil { t.Fatal(err) } } var acked []string stateOf := func(s *State, step string) string { var st string _ = s.db.QueryRow("SELECT state FROM steps WHERE step_id=?", step).Scan(&st) return st } // The runner dies between the proofs and the commit. crash := &StepEngine{State: state, Acknowledge: func(_ context.Context, id string) { acked = append(acked, id) }, Verify: func(context.Context, string) bool { state.Close(); return true }} if err := crash.recoverSteps(ctx); err == nil || len(acked) != 0 { t.Fatalf("a recovery that never committed: err %v, acknowledged %v", err, acked) } state, err = OpenState(ctx, dir) if err != nil { t.Fatal(err) } defer state.Close() proven := map[string]bool{"run-a1": true} e := &StepEngine{State: state, Verify: func(_ context.Context, id string) bool { return proven[id] }} e.Acknowledge = func(_ context.Context, id string) { if st := stateOf(state, id[len("run-"):]); st != runReported { t.Errorf("%s acknowledged with its step %q, not committed", id, st) } acked = append(acked, id) } if err := e.recoverSteps(ctx); err != nil || len(acked) != 1 || acked[0] != "run-a1" { t.Fatalf("recovery: err %v, acknowledged %v", err, acked) } proven["run-b1"] = true e.Probe(ctx) if len(acked) != 2 || acked[1] != "run-b1" { t.Fatalf("probe: acknowledged %v", acked) } }