Files
go-learning/exercises/01-core/07-concurrency/concurrency_test.go
T

31 lines
691 B
Go

package concurrency
import (
"context"
"testing"
"time"
)
func TestParallelSum(t *testing.T) {
chunks := [][]int{{1, 2, 3}, {4, 5}, {10, 20, 30}}
if got := ParallelSum(chunks); got != 75 {
t.Fatalf("got %d, want 75", got)
}
}
func TestWaitOrCancel(t *testing.T) {
ch := make(chan string, 1)
ch <- "ready"
got, err := WaitOrCancel(context.Background(), ch)
if err != nil || got != "ready" {
t.Fatalf("got (%q, %v), want (ready, nil)", got, err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
never := make(chan string)
if _, err := WaitOrCancel(ctx, never); err == nil {
t.Fatal("expected context deadline error")
}
}