2016-02-04 21:33:11 +00:00
|
|
|
package worker_test
|
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2016-02-04 21:33:11 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/worker"
|
2016-02-04 21:33:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const concurrency = 10
|
|
|
|
|
|
|
|
var errTooLarge = errors.New("too large")
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
func square(ctx context.Context, job worker.Job) (interface{}, error) {
|
2016-02-05 21:22:24 +00:00
|
|
|
n := job.Data.(int)
|
2016-02-04 21:33:11 +00:00
|
|
|
if n > 2000 {
|
|
|
|
return nil, errTooLarge
|
|
|
|
}
|
|
|
|
return n * n, nil
|
|
|
|
}
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
func newBufferedPool(ctx context.Context, bufsize int, n int, f worker.Func) (chan worker.Job, chan worker.Job, *worker.Pool) {
|
2016-02-04 21:33:11 +00:00
|
|
|
inCh := make(chan worker.Job, bufsize)
|
2016-02-05 21:22:24 +00:00
|
|
|
outCh := make(chan worker.Job, bufsize)
|
2016-02-04 21:33:11 +00:00
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
return inCh, outCh, worker.New(ctx, n, f, inCh, outCh)
|
2016-02-04 21:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool(t *testing.T) {
|
2017-06-04 09:16:55 +00:00
|
|
|
inCh, outCh, p := newBufferedPool(context.TODO(), 200, concurrency, square)
|
2016-02-04 21:33:11 +00:00
|
|
|
|
|
|
|
for i := 0; i < 150; i++ {
|
2016-02-05 21:22:24 +00:00
|
|
|
inCh <- worker.Job{Data: i}
|
2016-02-04 21:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(inCh)
|
|
|
|
p.Wait()
|
|
|
|
|
|
|
|
for res := range outCh {
|
|
|
|
if res.Error != nil {
|
2016-02-05 21:22:24 +00:00
|
|
|
t.Errorf("unexpected error for job %v received: %v", res.Data, res.Error)
|
|
|
|
continue
|
2016-02-04 21:33:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-05 21:22:24 +00:00
|
|
|
n := res.Data.(int)
|
2016-02-04 21:33:11 +00:00
|
|
|
m := res.Result.(int)
|
|
|
|
|
|
|
|
if m != n*n {
|
|
|
|
t.Errorf("wrong value for job %d returned: want %d, got %d", n, n*n, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPoolErrors(t *testing.T) {
|
2017-06-04 09:16:55 +00:00
|
|
|
inCh, outCh, p := newBufferedPool(context.TODO(), 200, concurrency, square)
|
2016-02-04 21:33:11 +00:00
|
|
|
|
|
|
|
for i := 0; i < 150; i++ {
|
2016-02-05 21:22:24 +00:00
|
|
|
inCh <- worker.Job{Data: i + 1900}
|
2016-02-04 21:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(inCh)
|
|
|
|
p.Wait()
|
|
|
|
|
|
|
|
for res := range outCh {
|
2016-02-05 21:22:24 +00:00
|
|
|
n := res.Data.(int)
|
2016-02-04 21:33:11 +00:00
|
|
|
|
|
|
|
if n > 2000 {
|
|
|
|
if res.Error == nil {
|
|
|
|
t.Errorf("expected error not found, result is %v", res)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.Error != errTooLarge {
|
|
|
|
t.Errorf("unexpected error found, result is %v", res)
|
|
|
|
}
|
|
|
|
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if res.Error != nil {
|
2016-02-05 21:22:24 +00:00
|
|
|
t.Errorf("unexpected error for job %v received: %v", res.Data, res.Error)
|
2016-02-04 21:33:11 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m := res.Result.(int)
|
|
|
|
if m != n*n {
|
|
|
|
t.Errorf("wrong value for job %d returned: want %d, got %d", n, n*n, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|