| 1 | package export_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "runtime" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "the-valley/web/export" |
| 12 | "the-valley/web/export/exporttest" |
| 13 | ) |
| 14 | |
| 15 | // stopInPhase is a context that counts the checks of Err made within a |
| 16 | // function whose name ends with phase, and is cancelled at the check |
| 17 | // numbered at. With at 0 it is never cancelled, and counts alone. |
| 18 | type stopInPhase struct { |
| 19 | context.Context |
| 20 | cancel context.CancelFunc |
| 21 | phase string |
| 22 | at int |
| 23 | checks int |
| 24 | } |
| 25 | |
| 26 | func (c *stopInPhase) Err() error { |
| 27 | var pcs [32]uintptr |
| 28 | frames := runtime.CallersFrames(pcs[:runtime.Callers(2, pcs[:])]) |
| 29 | for { |
| 30 | frame, more := frames.Next() |
| 31 | if strings.HasSuffix(frame.Function, c.phase) { |
| 32 | c.checks++ |
| 33 | if c.checks == c.at { |
| 34 | c.cancel() |
| 35 | } |
| 36 | break |
| 37 | } |
| 38 | if !more { |
| 39 | break |
| 40 | } |
| 41 | } |
| 42 | return c.Context.Err() |
| 43 | } |
| 44 | |
| 45 | // stopsAtEveryCheck runs f once to count the checks it makes within |
| 46 | // phase, and then once cancelled at each of them. Each cancelled run |
| 47 | // returns context.Canceled, and makes at most one check after the one |
| 48 | // that saw the cancellation, the one that reports it. It returns how |
| 49 | // many checks a whole run makes. |
| 50 | func stopsAtEveryCheck(t *testing.T, phase string, f func(context.Context) error) int { |
| 51 | t.Helper() |
| 52 | count := &stopInPhase{Context: context.Background(), phase: phase} |
| 53 | if err := f(count); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | for at := 1; at <= count.checks; at++ { |
| 57 | base, cancel := context.WithCancel(context.Background()) |
| 58 | ctx := &stopInPhase{Context: base, cancel: cancel, phase: phase, at: at} |
| 59 | err := f(ctx) |
| 60 | cancel() |
| 61 | if !errors.Is(err, context.Canceled) || ctx.checks > at+1 { |
| 62 | t.Errorf("cancelled at check %d of %d in %s, it returned %v after %d checks", at, count.checks, phase, err, ctx.checks) |
| 63 | } |
| 64 | } |
| 65 | return count.checks |
| 66 | } |
| 67 | |
| 68 | // CheckNames stops when its context is cancelled, inside each loop: over |
| 69 | // the groups and their aliases, and over the projects and their web |
| 70 | // names, former names and aliases. It checks before each name it reads, |
| 71 | // so a long list of names keeps no cancelled reader at work. |
| 72 | func TestCheckNamesStopsWhenCancelled(t *testing.T) { |
| 73 | var groups []export.Group |
| 74 | var placed []export.Placed |
| 75 | names := 0 |
| 76 | for g := range 3 { |
| 77 | id := fmt.Sprintf("g%d", g) |
| 78 | groups = append(groups, export.Group{ID: id, Name: id + "-name", Aliases: []string{id + "-a", id + "-b"}}) |
| 79 | names += 4 |
| 80 | for p := range 3 { |
| 81 | project := fmt.Sprintf("%s-p%d", id, p) |
| 82 | placed = append(placed, export.Placed{Project: project, Placement: export.Placement{ |
| 83 | Group: id, Name: fmt.Sprint("web", p), FormerNames: []string{fmt.Sprint("old", p), fmt.Sprint("older", p)}, |
| 84 | Aliases: []string{project + "-a", project + "-b"}, |
| 85 | }}) |
| 86 | names += 6 |
| 87 | } |
| 88 | } |
| 89 | placed = append(placed, export.Placed{Project: "z-unplaced"}) |
| 90 | names++ |
| 91 | checks := stopsAtEveryCheck(t, ".CheckNames", func(ctx context.Context) error { |
| 92 | return export.CheckNames(ctx, groups, placed) |
| 93 | }) |
| 94 | if checks < names { |
| 95 | t.Errorf("CheckNames read %d names and checked its context %d times", names, checks) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // Reading an export checks its groups and the places of its projects |
| 100 | // last, and stops when its context is cancelled anywhere in that check. |
| 101 | func TestValidatingGroupsStopsWhenCancelled(t *testing.T) { |
| 102 | f := exporttest.New(t) |
| 103 | e, err := export.Decode(f.ExportJSON) |
| 104 | if err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | site := e.Projects[0] |
| 108 | site.Name, site.Group, site.WebName, site.FormerNames, site.Aliases = "site", "web", "app", []string{"pages"}, []string{"site"} |
| 109 | e.Projects = append(e.Projects, site) |
| 110 | e.Groups = append(e.Groups, export.Group{ID: "web", Name: "docs", Aliases: []string{"d"}}) |
| 111 | checks := stopsAtEveryCheck(t, ".(*Export).checkGroups", e.ValidateContext) |
| 112 | // Each group's identifier, name and alias, and each project's |
| 113 | // identifier, web name, former name and alias. |
| 114 | if names := 2*3 + 2*4; checks < names { |
| 115 | t.Errorf("checking the export's groups read %d names, and checked the context %d times", names, checks) |
| 116 | } |
| 117 | } |