diff --git a/internal/scheduler/batch/batch.go b/internal/scheduler/batch/batch.go new file mode 100644 index 000000000..71d0ef128 --- /dev/null +++ b/internal/scheduler/batch/batch.go @@ -0,0 +1,400 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +/* +Package batch implements a batch processor for jobs that update or delete +multiple rows in the database using a single SQL UPDATE or DELETE +statement. + +It defines a type, [Batch], which is used by a job to execute a SQL +statement in batches. SQL commands are executed in batches by providing an +[Exec] function that executes a SQL statement, which must contain a +parameterized LIMIT clause, and returns the number of rows affected by the +query or an error if the query failed. Batch adjusts the batch size in an +effort to get the query execution time close to the [Config.Target] +duration. When the batch size is adjusted, [Store] is called. Jobs using +the batch processor should persist this value and use it as the starting +batch size in subsequent calls to Batch. + +See session.deleteTerminatedJob for an example of how to use the batch +processor. + +# SQL + +The SQL LIMIT clause can only be used in query statements. It cannot be +used directly in a DELETE or UPDATE statement. Instead, a subquery or CTE +must be used to limit the number of rows affected by the query. + +Here is an example SQL DELETE statement that uses a subquery with a LIMIT +clause: + + delete + from alias_target_deleted + where public_id in ( + select public_id + from alias_target_deleted + where delete_time < @delete_time + limit @batch_size + ); +*/ +package batch + +import ( + "context" + "math/rand/v2" + "sync" + "time" + + "github.com/hashicorp/boundary/internal/errors" + "github.com/hashicorp/boundary/internal/scheduler" +) + +const ( + DefaultStatusThreshold = 5 * time.Minute + DefaultTarget = 1 * time.Second +) + +const ( + DefaultSize = 5000 + DefaultMin = 10 + DefaultMax = 10000 +) + +// Exec is the function type used for executing the batch query. An Exec +// function must return the number of rows affected by the query or an +// error if the query failed. +type Exec func(ctx context.Context, batchSize int) (rowCount int, err error) + +// Store is the function type used for storing the batch size in the +// database. A Store function must return an error if the store failed. +type Store func(ctx context.Context, batchSize int) error + +// Config is a structure used to configure a [Batch]. +type Config struct { + // Size is the initial batch size. + // + // If Size is less than 1, the initial batch size will be set to + // DefaultSize. + // + // If Size is less than Min, the initial batch size will be set to Min. + // + // If Size is greater than Max, the initial batch size will be set to + // Max. + Size int + + // Min and Max are the minimum and maximum batch sizes allowed. The + // batch size will be clamped to the range [Min, Max]. + // + // If Min is less than 1, it will be set to DefaultMin. + // + // If Max is less than or equal to Min, it will be set to DefaultMax. + Min int + Max int + + // TotalToComplete is the total number of rows to be processed by the + // job. This is reported in the JobStatus returned by the Status + // method. + TotalToComplete int + + // StatusThreshold is the amount of time the job has to return a + // JobStatus with values different from the previous JobStatus. If a + // call to Exec approaches this threshold, Batch will interrupt the + // call to Exec, reduce the batch size, and call Exec again. It will + // also increment the Retries value reported in the Status method. + // + // If StatusThreshold is less than or equal to zero, it will be set to + // DefaultStatusThreshold. + StatusThreshold time.Duration + + // Target is the target duration for the query to run in. The batch + // size will be adjusted to keep the query duration within the target + // range. + // + // If Target is less than or equal to zero, it will be set to + // DefaultTarget. + // + // If Target is greater than or equal to StatusThreshold, it will be + // set to StatusThreshold - 5ms. + Target time.Duration + + // Exec is called to execute the query. Exec is called by the Run + // method in a loop until the row count returned by Exec is less than + // the current batch size or Exec returns an error other than + // context.DeadlineExceeded. The configuration must supply this + // callback for batch to succeed. + // + // If Exec returns a context.DeadlineExceeded error, the batch size + // will be reduced, the Retries value reported in the Status method + // will be incremented, and then Exec will be called again. + // + // If Exec returns an error other than context.DeadlineExceeded, the + // batch size will not be changed and the Run method will exit + // returning the error. + Exec Exec + + // Store, if not nil, is called when the batch size has changed and the + // new batch size should be stored in the database. If Store returns an + // error, the Run method will exit and return with the error. + Store Store +} + +func (c *Config) size() int { + switch { + case c.Size < 1: + return DefaultSize + case c.Size < c.min(): + return c.min() + case c.Size > c.max(): + return c.max() + } + return c.Size +} + +func (c *Config) min() int { + if c.Min < 1 { + return DefaultMin + } + return c.Min +} + +func (c *Config) max() int { + if c.Max <= c.Min { + return DefaultMax + } + return c.Max +} + +// Arbitrary constants +const ( + statusThresholdBuffer = 250 * time.Millisecond + + // both ranges are a percentage of the target duration + lowerRange = 10 + upperRange = 10 +) + +func (c *Config) statusThreshold() time.Duration { + if c.StatusThreshold <= 0 { + return DefaultStatusThreshold - statusThresholdBuffer + } + return c.StatusThreshold - statusThresholdBuffer +} + +func (c *Config) target() time.Duration { + switch { + case c.Target <= 0: + return DefaultTarget + case c.Target >= c.statusThreshold(): + return c.statusThreshold() + } + return c.Target +} + +func (c *Config) targetRange() (lower time.Duration, upper time.Duration) { + target := c.target() + return target - (target / lowerRange), target + (target / upperRange) +} + +func (c *Config) store() Store { + if c.Store == nil { + return func(_ context.Context, _ int) error { return nil } + } + return c.Store +} + +func (c *Config) clone() *Config { + if c == nil { + return nil + } + return &Config{ + Size: c.Size, + Min: c.Min, + Max: c.Max, + TotalToComplete: c.TotalToComplete, + StatusThreshold: c.StatusThreshold, + Target: c.Target, + Exec: c.Exec, + Store: c.Store, + } +} + +// Batch is a batch job processor for SQL jobs that update or delete +// multiple rows in the database using a single SQL UPDATE or DELETE +// statement. +type Batch struct { + c *Config + + slowExecutions int + fastExecutions int + + mu sync.Mutex + retries int + totalCompleted int +} + +// New creates a [Batch] that uses the given configuration to execute a SQL +// job in batches. An error is returned if c contains a nil Exec. +func New(ctx context.Context, c *Config) (*Batch, error) { + const op = "batch.New" + switch { + case c == nil: + return nil, errors.New(ctx, errors.InvalidParameter, op, "nil Config") + case c.Exec == nil: + return nil, errors.New(ctx, errors.InvalidParameter, op, "nil Exec") + } + return &Batch{ + c: c.clone(), + }, nil +} + +// Status reports the job’s current status. +func (b *Batch) Status() scheduler.JobStatus { + b.mu.Lock() + defer b.mu.Unlock() + return scheduler.JobStatus{ + Completed: b.totalCompleted, + Total: b.c.TotalToComplete, + Retries: b.retries, + } +} + +// Run runs the batch processor. It calls the [Exec] function in a loop +// until the row count returned by Exec is less than the current batch size +// or Exec returns an error other than context.DeadlineExceeded. +// +// Each call to Run resets the values returned in [Batch.Status]. +func (b *Batch) Run(ctx context.Context) error { + const op = "batch.Run" + b.reset() + + for { + count, runDuration, err := b.run(ctx) + if err != nil { + if errors.Is(err, context.DeadlineExceeded) { + if err := b.timedOut(ctx); err != nil { + return errors.Wrap(ctx, err, op) + } + continue + } + return errors.Wrap(ctx, err, op) + } + + b.successful(count) + + // batch is not complete + if count == b.c.size() { + if err := b.adjustSize(ctx, runDuration); err != nil { + return errors.Wrap(ctx, err, op) + } + continue + } + + // batch is complete + return nil + } +} + +func (b *Batch) run(ctx context.Context) (int, time.Duration, error) { + queryCtx, cancel := context.WithTimeout(ctx, b.c.statusThreshold()) + defer cancel() + start := time.Now() + n, err := b.c.Exec(queryCtx, b.c.size()) + return n, time.Since(start), err +} + +func (b *Batch) reset() { + b.mu.Lock() + b.retries = 0 + b.totalCompleted = 0 + b.mu.Unlock() + b.fastExecutions = 0 + b.slowExecutions = 0 +} + +func (b *Batch) timedOut(ctx context.Context) error { + b.mu.Lock() + b.retries++ + b.mu.Unlock() + return b.c.exponentialDecrease(ctx, b.retries) +} + +func (b *Batch) successful(rowCount int) { + b.mu.Lock() + b.totalCompleted += rowCount + b.mu.Unlock() +} + +func (b *Batch) adjustSize(ctx context.Context, runDuration time.Duration) error { + lower, upper := b.c.targetRange() + + switch { + case runDuration < lower: // too fast + // increase the batch size to go slower + b.fastExecutions++ + b.slowExecutions = 0 + return b.c.linearIncrease(ctx, b.fastExecutions) + case runDuration > upper: // too slow + // decrease the batch size to go faster + b.slowExecutions++ + b.fastExecutions = 0 + return b.c.linearDecrease(ctx, b.slowExecutions) + } + + // within target range so reset the counters + b.fastExecutions = 0 + b.slowExecutions = 0 + return nil +} + +func (c *Config) exponentialDecrease(ctx context.Context, attempt int) error { + if attempt < 1 { + attempt = 1 + } + newSize := (c.size() / (1 << uint(attempt))) - c.jitter() + return c.setSize(ctx, newSize) +} + +func (c *Config) linearIncrease(ctx context.Context, attempt int) error { + if attempt < 1 { + attempt = 1 + } + newSize := c.size() + (c.size() / 10 * attempt) + c.jitter() + return c.setSize(ctx, newSize) +} + +func (c *Config) linearDecrease(ctx context.Context, attempt int) error { + if attempt < 1 { + attempt = 1 + } + newSize := c.size() - (c.size() / 10 * attempt) - c.jitter() + return c.setSize(ctx, newSize) +} + +// jitter returns a random number between 0 and 10% of the current batch +// size. +func (c *Config) jitter() int { + return rand.N(c.size() / 10) +} + +// setSize sets the batch size to newSize and calls Store if newSize is +// different from the current size. If newSize is less than Min, the batch +// size will be set to Min. If newSize is greater than Max, the batch size +// will be set to Max. If Store returns an error, it will be returned by +// setSize. +func (c *Config) setSize(ctx context.Context, newSize int) error { + currentSize := c.Size + if newSize == currentSize { + return nil + } + switch { + case newSize < c.min(): + newSize = c.min() + case newSize > c.max(): + newSize = c.max() + } + if newSize != currentSize { + c.Size = newSize + return c.store()(ctx, newSize) + } + return nil +} diff --git a/internal/scheduler/batch/batch_test.go b/internal/scheduler/batch/batch_test.go new file mode 100644 index 000000000..b82352b6a --- /dev/null +++ b/internal/scheduler/batch/batch_test.go @@ -0,0 +1,566 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package batch + +import ( + "context" + "errors" + "fmt" + "testing" + "time" + + "github.com/hashicorp/boundary/internal/scheduler" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestNew(t *testing.T) { + t.Parallel() + t.Run("nil-config", func(t *testing.T) { + t.Parallel() + ctx := context.Background() + got, err := New(ctx, nil) + require.Error(t, err) + assert.Nil(t, got) + }) + t.Run("nil-Exec", func(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := &Config{} + got, err := New(ctx, config) + require.Error(t, err) + assert.Nil(t, got) + }) + t.Run("minimum-config", func(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := &Config{ + Exec: func(ctx context.Context, batchSize int) (int, error) { return 0, nil }, + } + got, err := New(ctx, config) + require.NoError(t, err) + assert.NotNil(t, got) + }) +} + +func TestConfig(t *testing.T) { + t.Parallel() + t.Run("default-config", func(t *testing.T) { + t.Parallel() + assert := assert.New(t) + config := &Config{ + Exec: func(ctx context.Context, batchSize int) (int, error) { return 0, nil }, + } + assert.Equal(DefaultSize, config.size()) + assert.Equal(DefaultMin, config.min()) + assert.Equal(DefaultMax, config.max()) + assert.Equal(DefaultStatusThreshold-statusThresholdBuffer, config.statusThreshold()) + assert.Equal(DefaultTarget, config.target()) + config.StatusThreshold = DefaultTarget + config.Target = DefaultTarget + want := DefaultTarget - statusThresholdBuffer + assert.Equal(want, config.target()) + }) + tests := []struct { + min, max, size int + want int + }{ + {0, 0, 0, DefaultSize}, + {0, 0, DefaultMin - 1, DefaultMin}, + {0, 0, DefaultMax + 1, DefaultMax}, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("min-max-size_%d-%d-%d", tt.min, tt.max, tt.size), func(t *testing.T) { + t.Parallel() + assert := assert.New(t) + config := &Config{ + Min: tt.min, + Max: tt.max, + Size: tt.size, + } + assert.Equal(tt.want, config.size()) + }) + } +} + +type testStore struct { + called bool + batchSize int +} + +func (s *testStore) Store(ctx context.Context, batchSize int) error { + s.called = true + s.batchSize = batchSize + return nil +} + +func TestConfig_setSize(t *testing.T) { + t.Parallel() + ctx := context.Background() + config := &Config{ + Min: 5, + Max: 15, + Size: 10, + } + tests := []struct { + newSize int + wantSize int + wantCalled bool + }{ + {0, 5, true}, + {4, 5, true}, + {5, 5, true}, + {6, 6, true}, + {10, 10, false}, + {20, 15, true}, + {15, 15, true}, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("newSize_%d", tt.newSize), func(t *testing.T) { + t.Parallel() + assert, require := assert.New(t), require.New(t) + c := config.clone() + assert.Equal(config, c) + + ts := &testStore{} + c.Store = ts.Store + + err := c.setSize(ctx, tt.newSize) + require.NoError(err) + assert.Equal(tt.wantSize, c.Size) + assert.Equal(tt.wantCalled, ts.called) + if tt.wantCalled { + assert.Equal(tt.wantSize, ts.batchSize) + } + }) + } +} + +func TestConfig_targetRange(t *testing.T) { + t.Parallel() + tests := []struct { + target time.Duration + wantUpper time.Duration + wantLower time.Duration + }{ + { + target: 1000 * time.Millisecond, + wantLower: 900 * time.Millisecond, + wantUpper: 1100 * time.Millisecond, + }, + { + target: 60 * time.Second, + wantLower: 54 * time.Second, + wantUpper: 66 * time.Second, + }, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("target_%d", tt.target), func(t *testing.T) { + t.Parallel() + assert := assert.New(t) + config := &Config{ + Target: tt.target, + } + assert.Equal(tt.target, config.target()) + lower, upper := config.targetRange() + assert.Equal(tt.wantLower, lower) + assert.Equal(tt.wantUpper, upper) + }) + } +} + +func TestConfig_exponentialDecrease(t *testing.T) { + t.Parallel() + tests := []struct { + batchSize int + attempt int + expected int + }{ + {10, 0, DefaultMin}, + {10, 1, DefaultMin}, + {10, 2, DefaultMin}, + {9, 1, DefaultMin}, + {9, 2, DefaultMin}, + {1000, 0, 500}, + {1000, 1, 500}, + {1000, 2, 250}, + {1000, 3, 125}, + {1000, 4, 62}, + {1000, 5, 31}, + {1000, 6, 15}, + {1000, 7, DefaultMin}, + {500, 1, 250}, + {500, 2, 125}, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("batchSize=%d/attempt=%d", tt.batchSize, tt.attempt), func(t *testing.T) { + assert, require := assert.New(t), require.New(t) + ctx := context.Background() + c := &Config{ + Size: tt.batchSize, + } + delta := c.size() / 10 + err := c.exponentialDecrease(ctx, tt.attempt) + require.NoError(err) + assert.InDelta(tt.expected, c.Size, float64(delta)) + }) + } +} + +func Test_linearDecrease(t *testing.T) { + t.Parallel() + tests := []struct { + batchSize int + attempt int + expected int + }{ + {10, 0, DefaultMin}, + {10, 1, DefaultMin}, + {10, 2, DefaultMin}, + {1000, 0, 900}, + {1000, 1, 900}, + {1000, 2, 800}, + {1000, 3, 700}, + {1000, 4, 600}, + {1000, 5, 500}, + {1000, 6, 400}, + {1000, 7, 300}, + {1000, 10, DefaultMin}, + {1000, 11, DefaultMin}, + {500, 1, 450}, + {500, 2, 400}, + {100, 1, 90}, + {100, 2, 80}, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("batchSize=%d/attempt=%d", tt.batchSize, tt.attempt), func(t *testing.T) { + t.Parallel() + assert, require := assert.New(t), require.New(t) + ctx := context.Background() + c := &Config{ + Size: tt.batchSize, + } + delta := c.size() / 10 + err := c.linearDecrease(ctx, tt.attempt) + require.NoError(err) + assert.InDelta(tt.expected, c.Size, float64(delta)) + }) + } +} + +func Test_linearIncrease(t *testing.T) { + t.Parallel() + tests := []struct { + batchSize int + attempt int + expected int + }{ + {10000, 0, DefaultMax}, + {10000, 1, DefaultMax}, + {10000, 2, DefaultMax}, + {1000, 0, 1100}, + {1000, 1, 1100}, + {1000, 2, 1200}, + {1000, 3, 1300}, + {1000, 4, 1400}, + {1000, 5, 1500}, + {1000, 6, 1600}, + {1000, 7, 1700}, + {500, 1, 550}, + {500, 2, 600}, + {100, 1, 110}, + {100, 2, 120}, + {10, 1, 11}, + {10, 2, 12}, + } + for _, tt := range tests { + tt := tt + t.Run(fmt.Sprintf("batchSize=%d/attempt=%d", tt.batchSize, tt.attempt), func(t *testing.T) { + t.Parallel() + assert, require := assert.New(t), require.New(t) + ctx := context.Background() + c := &Config{ + Size: tt.batchSize, + } + delta := c.size() / 10 + err := c.linearIncrease(ctx, tt.attempt) + require.NoError(err) + assert.InDelta(tt.expected, c.Size, float64(delta)) + }) + } +} + +type recorder struct { + execBatchSize int + storeBatchSize int + status scheduler.JobStatus +} + +func (r *recorder) setup(c *Config) { + c.Store = r.Store +} + +func (r *recorder) Store(ctx context.Context, batchSize int) error { + r.storeBatchSize = batchSize + return nil +} + +func (r *recorder) Exec(ctx context.Context, batchSize int) (int, error) { + r.execBatchSize = batchSize + return 0, nil +} + +type testRun struct { + ret func(context.Context, int, *Config) (int, error) + chk func(*testing.T, *recorder) + rec *recorder +} + +func (tr *testRun) validate(t *testing.T) { + if tr.chk != nil { + tr.chk(t, tr.rec) + } +} + +func (tr *testRun) recorder(cf *Config) *recorder { + if tr.rec == nil { + tr.rec = &recorder{} + tr.rec.setup(cf) + } + return tr.rec +} + +type testRunner struct { + conf *Config + b *Batch + runs []*testRun + t *testing.T + call int +} + +func newTestRunner(t *testing.T, conf *Config, b *Batch) *testRunner { + tr := &testRunner{ + t: t, + conf: conf, + b: b, + } + conf.Exec = tr.Exec + return tr +} + +func (tr *testRunner) Exec(ctx context.Context, batchSize int) (int, error) { + if tr.call > 0 { + prevRun := tr.runs[tr.call-1] + prevRun.rec.status = tr.b.Status() + prevRun.validate(tr.t) + } + run := tr.runs[tr.call] + rec := run.recorder(tr.conf) + if _, err := rec.Exec(ctx, batchSize); err != nil { + return 0, err + } + tr.call++ + return run.ret(ctx, batchSize, tr.conf) +} + +func TestRun(t *testing.T) { + const testStatusTotal = 10 + + t.Parallel() + assertStoreCalled := func() func(*testing.T, *recorder) { + const op = "assertStoreCalled" + return func(t *testing.T, r *recorder) { + assert.Positive(t, r.storeBatchSize, op) + } + } + assertStoreNotCalled := func() func(*testing.T, *recorder) { + const op = "assertStoreNotCalled" + return func(t *testing.T, r *recorder) { + t.Helper() + assert.Zero(t, r.storeBatchSize, op) + } + } + assertRetryCalled := func() func(*testing.T, *recorder) { + const op = "assertRetryCalled" + return func(t *testing.T, r *recorder) { + assert.Positive(t, r.status.Retries, op) + } + } + assertRetryNotCalled := func() func(*testing.T, *recorder) { + const op = "assertRetryNotCalled" + return func(t *testing.T, r *recorder) { + assert.Zero(t, r.status.Retries, op) + } + } + assertCompletedCalled := func() func(*testing.T, *recorder) { + const op = "assertCompletedCalled" + return func(t *testing.T, r *recorder) { + assert.Positive(t, r.status.Completed, op) + } + } + assertCompletedNotCalled := func() func(*testing.T, *recorder) { + const op = "assertCompletedNotCalled" + return func(t *testing.T, r *recorder) { + assert.Zero(t, r.status.Completed, op) + } + } + assertStatusTotal := func() func(*testing.T, *recorder) { + const op = "assertStatusTotal" + return func(t *testing.T, r *recorder) { + assert.Equal(t, testStatusTotal, r.status.Total, op) + } + } + combine := func(fns ...func(*testing.T, *recorder)) func(*testing.T, *recorder) { + return func(t *testing.T, r *recorder) { + for _, fn := range fns { + fn(t, r) + } + } + } + + execLessThanBatch := func() func(context.Context, int, *Config) (int, error) { + return func(ctx context.Context, batchSize int, c *Config) (int, error) { + return batchSize - 1, nil + } + } + execTimeout := func() func(context.Context, int, *Config) (int, error) { + return func(ctx context.Context, batchSize int, c *Config) (int, error) { + select { + case <-ctx.Done(): + return 0, ctx.Err() + } + } + } + execError := func() func(context.Context, int, *Config) (int, error) { + return func(ctx context.Context, batchSize int, c *Config) (int, error) { + return 0, errors.New("fake error") + } + } + execSlow := func() func(context.Context, int, *Config) (int, error) { + return func(ctx context.Context, batchSize int, c *Config) (int, error) { + _, upper := c.targetRange() + time.Sleep(upper + (2 * time.Millisecond)) + return batchSize, nil + } + } + execTargetRange := func() func(context.Context, int, *Config) (int, error) { + return func(ctx context.Context, batchSize int, c *Config) (int, error) { + time.Sleep(c.Target) + return batchSize, nil + } + } + + runMap := map[string]*testRun{ + "execLessThanBatch": { + ret: execLessThanBatch(), + chk: combine(assertStatusTotal(), assertStoreNotCalled(), assertRetryNotCalled(), assertCompletedCalled()), + }, + "execTimeout": { + ret: execTimeout(), + chk: combine(assertStatusTotal(), assertStoreCalled(), assertRetryCalled(), assertCompletedNotCalled()), + }, + "execError": { + ret: execError(), + chk: combine(assertStatusTotal(), assertStoreNotCalled(), assertRetryNotCalled(), assertCompletedNotCalled()), + }, + "execSlow": { + ret: execSlow(), + chk: combine(assertStatusTotal(), assertStoreCalled(), assertRetryNotCalled(), assertCompletedCalled()), + }, + "execTargetRange": { + ret: execTargetRange(), + chk: combine(assertStatusTotal(), assertStoreNotCalled(), assertRetryNotCalled(), assertCompletedCalled()), + }, + } + + tests := []struct { + name string + conf *Config + runs []*testRun + wantErr bool + }{ + { + name: "normal", + conf: &Config{ + TotalToComplete: testStatusTotal, + }, + runs: []*testRun{runMap["execLessThanBatch"]}, + }, + { + name: "error", + conf: &Config{ + TotalToComplete: testStatusTotal, + }, + runs: []*testRun{runMap["execError"]}, + wantErr: true, + }, + { + name: "timeout-normal", + conf: &Config{ + TotalToComplete: testStatusTotal, + StatusThreshold: 5 * time.Millisecond, + }, + runs: []*testRun{runMap["execTimeout"], runMap["execLessThanBatch"]}, + }, + { + name: "slow-normal", + conf: &Config{ + TotalToComplete: testStatusTotal, + StatusThreshold: 5 * time.Millisecond, + }, + runs: []*testRun{runMap["execSlow"], runMap["execLessThanBatch"]}, + }, + { + name: "target-normal", + conf: &Config{ + TotalToComplete: testStatusTotal, + Target: 1 * time.Second, + }, + runs: []*testRun{runMap["execTargetRange"], runMap["execLessThanBatch"]}, + }, + } + for _, tt := range tests { + tt := tt + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + assert := assert.New(t) + ctx := context.Background() + b := &Batch{ + c: tt.conf, + } + tr := newTestRunner(t, tt.conf, b) + tr.runs = tt.runs + + if err := b.Run(ctx); tt.wantErr { + assert.Error(err) + } else { + assert.NoError(err) + } + }) + } +} + +func TestBatch_batchCompleted(t *testing.T) { + t.Parallel() + assert, require := assert.New(t), require.New(t) + ctx := context.Background() + config := &Config{ + Exec: func(ctx context.Context, batchSize int) (int, error) { return 0, nil }, + Store: func(ctx context.Context, batchSize int) error { + fmt.Println("batchSize: ", batchSize) + return nil + }, + } + b, err := New(ctx, config) + require.NoError(err) + assert.NotNil(b) + + err = b.adjustSize(ctx, 10) + require.NoError(err) +}