Skip to content

Commit

Permalink
added missing options
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrik-haase committed Sep 5, 2024
1 parent 58cc4f8 commit a980f73
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type LoadTestOptions struct {
resultHandlers []resultHandler
weightOverrides map[string]int
reportInterval time.Duration
ctxModifier func(ctx context.Context) context.Context
defaultTimeout time.Duration
}
type LoadTestOption func(*LoadTestOptions)

Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goload

import (
"context"
"github.com/HenriBeck/goload/pacer"
"time"
)
Expand Down Expand Up @@ -53,6 +54,18 @@ func WithAdditionalResultHandler(handler resultHandler) LoadTestOption {
}
}

func WithContextModifier(fn func(ctx context.Context) context.Context) LoadTestOption {
return func(options *LoadTestOptions) {
options.ctxModifier = fn
}
}

func WithDefaultTimeout(timeout time.Duration) LoadTestOption {
return func(options *LoadTestOptions) {
options.defaultTimeout = timeout
}
}

func WithWeightOverrides(overrides map[string]int) LoadTestOption {
return func(options *LoadTestOptions) {
options.weightOverrides = overrides
Expand Down
13 changes: 13 additions & 0 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Runner struct {
maxWorkers int
weightOverrides map[string]int

ctxModifier func(ctx context.Context) context.Context
defaultTimeout time.Duration

startedAt *time.Time
}

Expand All @@ -26,6 +29,8 @@ func NewRunner(loadTestOptions LoadTestOptions) *Runner {
workers: loadTestOptions.initialWorkers,
maxWorkers: loadTestOptions.maxWorkers,
weightOverrides: loadTestOptions.weightOverrides,
ctxModifier: loadTestOptions.ctxModifier,
defaultTimeout: loadTestOptions.defaultTimeout,
startedAt: nil,
}

Expand Down Expand Up @@ -155,6 +160,14 @@ func (r *Runner) hit(ex Executor, began time.Time) *Result {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), ex.Options().Timeout)
defer cancel()
} else if r.defaultTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), ex.Options().Timeout)
defer cancel()
}

if r.ctxModifier != nil {
ctx = r.ctxModifier(ctx)
}

resp := ex.Execute(ctx)
Expand Down

0 comments on commit a980f73

Please sign in to comment.