Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HW6 is completed #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed hw05_parallel_execution/.sync
Empty file.
28 changes: 26 additions & 2 deletions hw06_pipeline_execution/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,30 @@ type (
type Stage func(in In) (out Out)

func ExecutePipeline(in In, done In, stages ...Stage) Out {
// Place your code here.
return nil
if in == nil {
return nil
}

for _, st := range stages {
tmpChan := make(chan interface{})
go func(ch In) {
defer close(tmpChan)

for {
select {
case item, ok := <-ch:
if !ok {
return
}
tmpChan <- item
case <-done:
return
}
}
}(in)

in = st(tmpChan)
}

return in
}
29 changes: 29 additions & 0 deletions hw06_pipeline_execution/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"testing"
"time"

"github.com/stretchr/testify/require"

Check failure on line 8 in hw06_pipeline_execution/pipeline_test.go

View workflow job for this annotation

GitHub Actions / lint

import 'github.com/stretchr/testify/require' is not allowed from list 'Main' (depguard)
)

const (
Expand Down Expand Up @@ -36,6 +36,35 @@
g("Stringifier", func(v interface{}) interface{} { return strconv.Itoa(v.(int)) }),
}

t.Run("empty stages list", func(t *testing.T) {
in := make(Bi)
data := []string{"a", "b", "c", "d", "e"}

go func() {
for _, v := range data {
in <- v
}
close(in)
}()

result := make([]string, 0, len(data))
start := time.Now()
for s := range ExecutePipeline(in, nil, []Stage{}...) {
result = append(result, s.(string))
}
elapsed := time.Since(start)

require.Equal(t, data, result)
require.Less(t,
int64(elapsed),
int64(sleepPerStage)+int64(fault))
})

t.Run("nil check", func(t *testing.T) {
res := ExecutePipeline(nil, nil, stages...)
require.Nil(t, res)
})

t.Run("simple case", func(t *testing.T) {
in := make(Bi)
data := []int{1, 2, 3, 4, 5}
Expand Down
Loading