-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6699 from martinyonatann/main
add parallel execution example
- Loading branch information
Showing
2 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const ( | ||
numWorkers = 5 | ||
numTasks = 20 | ||
) | ||
|
||
func worker(id int, tasks <-chan int, results chan<- int, wg *sync.WaitGroup) { | ||
defer wg.Done() | ||
for task := range tasks { | ||
// Simulate some processing time | ||
time.Sleep(time.Millisecond * time.Duration(task)) | ||
// Send the result back to the main goroutine | ||
results <- task * 2 | ||
} | ||
} | ||
|
||
func main() { | ||
// Create channels for tasks and results | ||
tasks := make(chan int, numTasks) | ||
results := make(chan int, numTasks) | ||
|
||
// Create a WaitGroup to wait for all goroutines to finish | ||
var wg sync.WaitGroup | ||
|
||
// Start worker goroutines | ||
for i := 1; i <= numWorkers; i++ { | ||
wg.Add(1) | ||
go worker(i, tasks, results, &wg) | ||
} | ||
|
||
// Send tasks to the workers | ||
for i := 1; i <= numTasks; i++ { | ||
tasks <- i | ||
} | ||
close(tasks) | ||
|
||
// Close the results channel when all workers are done | ||
go func() { | ||
wg.Wait() | ||
close(results) | ||
}() | ||
|
||
// Collect results from the workers | ||
var collectedResults []int | ||
for result := range results { | ||
collectedResults = append(collectedResults, result) | ||
} | ||
|
||
// Print the collected results | ||
fmt.Println("Collected Results:", collectedResults) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"os" | ||
"strings" | ||
"sync" | ||
"testing" | ||
) | ||
|
||
func TestWorker(t *testing.T) { | ||
// Create channels for tasks and results | ||
tasks := make(chan int) | ||
results := make(chan int) | ||
|
||
// Create a WaitGroup to wait for the worker goroutine to finish | ||
var wg sync.WaitGroup | ||
|
||
// Start the worker goroutine | ||
wg.Add(1) | ||
go worker(1, tasks, results, &wg) | ||
|
||
// Send a task to the worker | ||
tasks <- 10 | ||
close(tasks) // Close the tasks channel | ||
|
||
// Wait for the worker to finish | ||
wg.Wait() | ||
|
||
// Close the results channel and collect the result | ||
close(results) | ||
result := <-results | ||
|
||
// Assert the result | ||
expectedResult := 10 * 2 | ||
if result != expectedResult { | ||
t.Errorf("Expected %d, but got %d", expectedResult, result) | ||
} | ||
} | ||
|
||
func TestMainFunction(t *testing.T) { | ||
// Capture stdout for testing | ||
old := os.Stdout | ||
r, w, _ := os.Pipe() | ||
os.Stdout = w | ||
|
||
// Run the main function | ||
main() | ||
|
||
// Close the write pipe and revert stdout | ||
w.Close() | ||
os.Stdout = old | ||
|
||
var buf bytes.Buffer | ||
io.Copy(&buf, r) | ||
output := buf.String() | ||
|
||
// Perform assertions on the output | ||
expectedOutput := "Collected Results:" | ||
if !strings.Contains(output, expectedOutput) { | ||
t.Errorf("Expected output to contain: %s, but got: %s", expectedOutput, output) | ||
} | ||
} |