MCPcopy Create free account
hub / github.com/goadesign/goa / TestGenerateParallelManyFiles

Function TestGenerateParallelManyFiles

codegen/generator/generate_merge_test.go:78–131  ·  view source on GitHub ↗

TestGenerateParallelManyFiles verifies that parallel file writing correctly handles multiple files (more than runtime.NumCPU()) to exercise the worker pool distribution. This ensures all workers process files and all files are written correctly.

(t *testing.T)

Source from the content-addressed store, hash-verified

76// pool distribution. This ensures all workers process files and all files are
77// written correctly.
78func TestGenerateParallelManyFiles(t *testing.T) {
79 t.Cleanup(func() { Generators = generators })
80
81 // Generate 20 files to ensure we exceed typical CPU counts and exercise
82 // the worker pool's work distribution.
83 const numFiles = 20
84 Generators = func(cmd string) ([]Genfunc, error) {
85 return []Genfunc{
86 func(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
87 files := make([]*codegen.File, numFiles)
88 for i := 0; i < numFiles; i++ {
89 f := &codegen.File{
90 Path: filepath.Join(codegen.Gendir, "types", filepath.Join("parallel", filepath.Join("file"+string(rune('a'+i%26)), "test"+string(rune('0'+i/26))+".go"))),
91 }
92 f.SectionTemplates = []*codegen.SectionTemplate{
93 codegen.Header("Types", "types", nil),
94 {
95 Name: "type-def",
96 Source: "type Test" + string(rune('A'+i)) + " struct{ ID int }\n",
97 },
98 }
99 files[i] = f
100 }
101 return files, nil
102 },
103 }, nil
104 }
105
106 dir := t.TempDir()
107 outputs, err := Generate(dir, "gen", false)
108 if err != nil {
109 t.Fatalf("Generate failed: %v", err)
110 }
111 outputs = assertVersionFile(t, dir, outputs)
112
113 // Verify all generator files were written
114 if len(outputs) != numFiles {
115 t.Fatalf("expected %d output files, got %d", numFiles, len(outputs))
116 }
117
118 // Verify each file exists and has correct content
119 for i := 0; i < numFiles; i++ {
120 outpath := filepath.Join(dir, codegen.Gendir, "types", filepath.Join("parallel", filepath.Join("file"+string(rune('a'+i%26)), "test"+string(rune('0'+i/26))+".go")))
121 bs, err := os.ReadFile(outpath)
122 if err != nil {
123 t.Fatalf("failed reading file %d: %v", i, err)
124 }
125 content := string(bs)
126 expected := "type Test" + string(rune('A'+i)) + " struct"
127 if !strings.Contains(content, expected) {
128 t.Fatalf("file %d missing expected content %q:\n%s", i, expected, content)
129 }
130 }
131}
132
133// TestGenerateParallelWithMerge verifies that parallel file writing correctly
134// handles file merging when multiple generators target the same path. This

Callers

nothing calls this directly

Calls 4

HeaderFunction · 0.92
GenerateFunction · 0.85
assertVersionFileFunction · 0.85
CleanupMethod · 0.80

Tested by

no test coverage detected