TestGenerateParallelErrorHandling verifies that when file rendering fails in the parallel worker pool, the first error is captured and returned while other workers continue processing.
(t *testing.T)
| 208 | // in the parallel worker pool, the first error is captured and returned while |
| 209 | // other workers continue processing. |
| 210 | func TestGenerateParallelErrorHandling(t *testing.T) { |
| 211 | t.Cleanup(func() { Generators = generators }) |
| 212 | |
| 213 | // Create multiple files where some will fail to render due to invalid paths. |
| 214 | // Worker pool should capture first error but continue processing other files. |
| 215 | Generators = func(cmd string) ([]Genfunc, error) { |
| 216 | return []Genfunc{ |
| 217 | func(genpkg string, roots []eval.Root) ([]*codegen.File, error) { |
| 218 | files := make([]*codegen.File, 5) |
| 219 | for i := 0; i < 5; i++ { |
| 220 | f := &codegen.File{ |
| 221 | Path: filepath.Join(codegen.Gendir, "types", "file"+string(rune('0'+i))+".go"), |
| 222 | } |
| 223 | f.SectionTemplates = []*codegen.SectionTemplate{ |
| 224 | codegen.Header("Types", "types", nil), |
| 225 | {Name: "type", Source: "type T" + string(rune('0'+i)) + " struct{}\n"}, |
| 226 | } |
| 227 | // Make file 2 fail by adding an invalid path character after writing starts |
| 228 | if i == 2 { |
| 229 | // Use a FinalizeFunc that returns an error |
| 230 | f.FinalizeFunc = func(fp string) error { |
| 231 | return os.ErrInvalid |
| 232 | } |
| 233 | } |
| 234 | files[i] = f |
| 235 | } |
| 236 | return files, nil |
| 237 | }, |
| 238 | }, nil |
| 239 | } |
| 240 | |
| 241 | dir := t.TempDir() |
| 242 | _, err := Generate(dir, "gen", false) |
| 243 | if err == nil { |
| 244 | t.Fatal("expected error from parallel generation, got nil") |
| 245 | } |
| 246 | // Verify we got an error (the first one encountered) |
| 247 | if !strings.Contains(err.Error(), "invalid") && !strings.Contains(err.Error(), "finalize") { |
| 248 | t.Fatalf("unexpected error message: %v", err) |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | // TestGenerateParallelSingleFile verifies that parallel file writing works |
| 253 | // correctly with just a single file (minimal parallelism edge case). |