| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // Task 9 follow-up, round 3, #1: the step's recovery acknowledges a |
| 11 | // proven exit only once what it found is committed. A recovery that dies |
| 12 | // before its commit acknowledges nothing; a later one that commits does, |
| 13 | // after; so does the probe once it records the termination. |
| 14 | func TestRecoveryAcknowledgesOnlyAfterItsCommit(t *testing.T) { |
| 15 | ctx := context.Background() |
| 16 | dir := t.TempDir() |
| 17 | state, err := OpenState(ctx, dir) |
| 18 | if err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | for _, id := range []string{"a1", "b1"} { |
| 22 | d := wire.DispatchCommand{Person: connPerson, TaskID: "tk-" + id, StepID: id, Attempt: 1, Specialist: wire.SpecialistEngineering, Findings: []wire.Finding{}} |
| 23 | if err := state.AcceptDispatch(ctx, "f-"+id, d); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | if err := state.setStepNamespace(ctx, stepRow{DispatchCommand: d}, "run-"+id); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | } |
| 30 | var acked []string |
| 31 | stateOf := func(s *State, step string) string { |
| 32 | var st string |
| 33 | _ = s.db.QueryRow("SELECT state FROM steps WHERE step_id=?", step).Scan(&st) |
| 34 | return st |
| 35 | } |
| 36 | // The runner dies between the proofs and the commit. |
| 37 | crash := &StepEngine{State: state, Acknowledge: func(_ context.Context, id string) { acked = append(acked, id) }, |
| 38 | Verify: func(context.Context, string) bool { state.Close(); return true }} |
| 39 | if err := crash.recoverSteps(ctx); err == nil || len(acked) != 0 { |
| 40 | t.Fatalf("a recovery that never committed: err %v, acknowledged %v", err, acked) |
| 41 | } |
| 42 | state, err = OpenState(ctx, dir) |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | defer state.Close() |
| 47 | proven := map[string]bool{"run-a1": true} |
| 48 | e := &StepEngine{State: state, Verify: func(_ context.Context, id string) bool { return proven[id] }} |
| 49 | e.Acknowledge = func(_ context.Context, id string) { |
| 50 | if st := stateOf(state, id[len("run-"):]); st != runReported { |
| 51 | t.Errorf("%s acknowledged with its step %q, not committed", id, st) |
| 52 | } |
| 53 | acked = append(acked, id) |
| 54 | } |
| 55 | if err := e.recoverSteps(ctx); err != nil || len(acked) != 1 || acked[0] != "run-a1" { |
| 56 | t.Fatalf("recovery: err %v, acknowledged %v", err, acked) |
| 57 | } |
| 58 | proven["run-b1"] = true |
| 59 | e.Probe(ctx) |
| 60 | if len(acked) != 2 || acked[1] != "run-b1" { |
| 61 | t.Fatalf("probe: acknowledged %v", acked) |
| 62 | } |
| 63 | } |
| 64 | |