|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "os/signal" |
| 9 | + "sync" |
| 10 | + "syscall" |
| 11 | + "time" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + plugins := []string{"table", "tracetable"} |
| 16 | + ctx, cancel := context.WithCancel(context.Background()) |
| 17 | + defer cancel() |
| 18 | + |
| 19 | + // Handle OS signals for termination (Ctrl+C, CI cancellation) |
| 20 | + // Signal(0xf) // 15 and Signal(0x2) // 2 |
| 21 | + sigChan := make(chan os.Signal, 1) |
| 22 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 23 | + |
| 24 | + // Let's wait and catch the termination signals |
| 25 | + // If any of these signals are received, we will cancel the context which will stop all running plugin processes |
| 26 | + go func() { |
| 27 | + <-sigChan |
| 28 | + fmt.Println("\nTermination signal received. Cleaning up...") |
| 29 | + cancel() |
| 30 | + }() |
| 31 | + |
| 32 | + // Let's make sure that the main function waits for all plugin processes to finish before exiting |
| 33 | + var wg sync.WaitGroup |
| 34 | + |
| 35 | + for _, plugin := range plugins { |
| 36 | + wg.Add(1) |
| 37 | + go func(p string) { |
| 38 | + defer wg.Done() |
| 39 | + |
| 40 | + // Start percli plugin in the background using the context |
| 41 | + cmd := exec.CommandContext(ctx, "percli", "plugin", "start", "./"+p) |
| 42 | + cmd.Stdout = os.Stdout |
| 43 | + cmd.Stderr = os.Stderr |
| 44 | + |
| 45 | + fmt.Printf("Starting plugin: %s\n", p) |
| 46 | + if err := cmd.Start(); err != nil { |
| 47 | + fmt.Errorf("failed to start %s: %v\n", p, err) |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // Instead of a blind sleep 10, could we do a health logic here? I am not sure! |
| 52 | + // Plugins start on an arbitrary port, how could we check if they are ruining or not?! |
| 53 | + time.Sleep(10 * time.Second) |
| 54 | + |
| 55 | + // Run your test command |
| 56 | + testCmd := exec.Command("npm", "run", "test:ci", "--prefix", "e2e", "--", "tests/"+p) |
| 57 | + testCmd.Stdout = os.Stdout |
| 58 | + testCmd.Stderr = os.Stderr |
| 59 | + if err := testCmd.Run(); err != nil { |
| 60 | + fmt.Errorf("tests failed for plugin %s: %v\n", p, err) |
| 61 | + } |
| 62 | + }(plugin) |
| 63 | + } |
| 64 | + |
| 65 | + wg.Wait() |
| 66 | + fmt.Println("All plugin tests completed.") |
| 67 | +} |
0 commit comments