Skip to content

Commit

Permalink
refactor constructor to raise panic on invalid conf
Browse files Browse the repository at this point in the history
  • Loading branch information
utku-caglayan committed Dec 30, 2021
1 parent 99b7971 commit 67d42e5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
15 changes: 6 additions & 9 deletions internal/invocation/stripe_executor.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package invocation

import (
"fmt"
"math/rand"
"runtime"
"sync"
Expand Down Expand Up @@ -29,18 +28,16 @@ type stripeExecutor struct {

// newStripeExecutor returns a new stripeExecutor with default configuration.
func newStripeExecutor() stripeExecutor {
// ignore error, default values do not raise error.
ex, _ := newStripeExecutorWithConfig(defaultEventWorkerCount, defaultEventQueueCapacity)
return ex
return newStripeExecutorWithConfig(defaultEventWorkerCount, defaultEventQueueCapacity)
}

// newStripeExecutor returns a new stripeExecutor with configured queueCount and queueSize.
func newStripeExecutorWithConfig(queueCount, queueSize int) (stripeExecutor, error) {
// newStripeExecutor returns a new stripeExecutor with configured queueCount and queueSize. If parameters are not greater than zero, it panics.
func newStripeExecutorWithConfig(queueCount, queueSize int) stripeExecutor {
if queueCount <= 0 {
return stripeExecutor{}, fmt.Errorf("queueCount must be greater than 0")
panic("queueCount must be greater than 0")
}
if queueSize <= 0 {
return stripeExecutor{}, fmt.Errorf("queueSize must be greater than 0")
panic("queueSize must be greater than 0")
}
se := stripeExecutor{
taskQueues: make([]chan func(), queueCount),
Expand All @@ -52,7 +49,7 @@ func newStripeExecutorWithConfig(queueCount, queueSize int) (stripeExecutor, err
se.quit = make(chan struct{})
se.wg = &sync.WaitGroup{}
se.execFn = defaultExecFn
return se, nil
return se
}

// start fires up the workers for each queue.
Expand Down
11 changes: 3 additions & 8 deletions internal/invocation/stripe_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ func TestStripeExecutor_dispatch(t *testing.T) {
}
for i, tt := range tests {
t.Run(fmt.Sprintf("QueueCount: %d, Key: %d", tt.queueCount, tt.key), func(t *testing.T) {
se, err := newStripeExecutorWithConfig(tt.queueCount, 10_000)
assert.Nil(t, err)
se := newStripeExecutorWithConfig(tt.queueCount, 10_000)
task := func() {
panic(i)
}
Expand All @@ -104,10 +103,7 @@ func TestStripeExecutor_dispatch(t *testing.T) {
}

func TestStripeExecutor_dispatchZeroAndNegative(t *testing.T) {
se, err := newStripeExecutorWithConfig(3, 10_000)
if err != nil {
t.Fatal(err)
}
se := newStripeExecutorWithConfig(3, 10_000)
se.start()
for i := 0; i <= 3; i++ {
var job sync.WaitGroup
Expand All @@ -125,8 +121,7 @@ func TestStripeExecutor_dispatchZeroAndNegative(t *testing.T) {
}

func TestStripeExecutor_dispatchQueueFull(t *testing.T) {
se, err := newStripeExecutorWithConfig(1, 1)
assert.Nil(t, err)
se := newStripeExecutorWithConfig(1, 1)
// executor not running, make the queue full
ok := se.dispatch(1, func() {})
assert.True(t, ok)
Expand Down

0 comments on commit 67d42e5

Please sign in to comment.