2017-05-31 00:45:58 +00:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-05-31 17:01:28 +00:00
|
|
|
"fmt"
|
2017-05-31 00:45:58 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestProgress(t *testing.T) {
|
2017-05-31 17:01:28 +00:00
|
|
|
s, err := calc(context.TODO(), 4, "calc")
|
2017-05-31 00:45:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 10, s)
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
|
|
pr, ctx, cancelProgress := NewContext(ctx)
|
|
|
|
var trace trace
|
|
|
|
eg.Go(func() error {
|
|
|
|
return saveProgress(ctx, pr, &trace)
|
|
|
|
})
|
2017-05-31 17:01:28 +00:00
|
|
|
s, err = calc(ctx, 5, "calc")
|
2017-05-31 00:45:58 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 15, s)
|
|
|
|
|
|
|
|
cancelProgress()
|
|
|
|
err = eg.Wait()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2017-06-13 21:42:51 +00:00
|
|
|
assert.True(t, len(trace.items) > 5)
|
|
|
|
assert.True(t, len(trace.items) <= 7)
|
2017-05-31 00:45:58 +00:00
|
|
|
assert.Equal(t, trace.items[len(trace.items)-1].Done, true)
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:01:28 +00:00
|
|
|
func TestProgressNested(t *testing.T) {
|
|
|
|
eg, ctx := errgroup.WithContext(context.Background())
|
|
|
|
pr, ctx, cancelProgress := NewContext(ctx)
|
|
|
|
var trace trace
|
|
|
|
eg.Go(func() error {
|
|
|
|
return saveProgress(ctx, pr, &trace)
|
|
|
|
})
|
|
|
|
s, err := reduceCalc(ctx, 3)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, 6, s)
|
|
|
|
|
|
|
|
cancelProgress()
|
|
|
|
|
|
|
|
err = eg.Wait()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.True(t, len(trace.items) > 9) // usually 14
|
2017-06-13 21:42:51 +00:00
|
|
|
assert.True(t, len(trace.items) <= 15)
|
2017-05-31 17:01:28 +00:00
|
|
|
streams := 0
|
|
|
|
for _, t := range trace.items {
|
|
|
|
if t.Done {
|
|
|
|
streams++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert.Equal(t, streams, 4)
|
|
|
|
}
|
|
|
|
|
|
|
|
func calc(ctx context.Context, total int, name string) (int, error) {
|
|
|
|
pw, _, ctx := FromContext(ctx, name)
|
2017-05-31 00:45:58 +00:00
|
|
|
defer pw.Done()
|
|
|
|
|
|
|
|
sum := 0
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.Write(Status{Action: "starting", Total: total})
|
2017-05-31 00:45:58 +00:00
|
|
|
for i := 1; i <= total; i++ {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, ctx.Err()
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
2017-05-31 17:01:28 +00:00
|
|
|
if i == total {
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.Write(Status{Action: "done", Total: total, Current: total})
|
2017-05-31 17:01:28 +00:00
|
|
|
} else {
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.Write(Status{Action: "calculating", Total: total, Current: i})
|
2017-05-31 17:01:28 +00:00
|
|
|
}
|
2017-05-31 00:45:58 +00:00
|
|
|
sum += i
|
|
|
|
}
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.Done()
|
2017-05-31 00:45:58 +00:00
|
|
|
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2017-05-31 17:01:28 +00:00
|
|
|
func reduceCalc(ctx context.Context, total int) (int, error) {
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
pw, _, ctx := FromContext(ctx, "reduce")
|
|
|
|
defer pw.Done()
|
|
|
|
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.Write(Status{Action: "starting"})
|
2017-05-31 17:01:28 +00:00
|
|
|
|
|
|
|
// sync step
|
|
|
|
sum, err := calc(ctx, total, "synccalc")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// parallel steps
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
func(i int) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
_, err := calc(ctx, total, fmt.Sprintf("calc-%d", i))
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sum, nil
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:45:58 +00:00
|
|
|
type trace struct {
|
|
|
|
items []Progress
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveProgress(ctx context.Context, pr ProgressReader, t *trace) error {
|
|
|
|
for {
|
|
|
|
p, err := pr.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
t.items = append(t.items, *p)
|
|
|
|
}
|
|
|
|
}
|