Skip to content

Commit

Permalink
Merge pull request #6699 from martinyonatann/main
Browse files Browse the repository at this point in the history
add parallel execution example
  • Loading branch information
ossamamehmood authored Oct 31, 2023
2 parents 0f6558a + 4f23a5c commit 6d95e68
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Add Code Here/go/routine/parallel_execution.go
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)
}
64 changes: 64 additions & 0 deletions Add Code Here/go/routine/parallel_execution_test.go
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)
}
}

0 comments on commit 6d95e68

Please sign in to comment.