mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-13 02:42:14 +01:00
minor refactoring of const + better timeout handling in pinter package + api package tests
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
// Package timeout helps creating
|
||||
// Package timeout helps managing
|
||||
// context with timeout.
|
||||
package timeout
|
||||
|
||||
@@ -2,7 +2,11 @@ package timeout
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
)
|
||||
|
||||
// Context creates a context with timeout for
|
||||
@@ -15,3 +19,29 @@ func Context(seconds float64) (context.Context, context.CancelFunc) {
|
||||
func Duration(seconds float64) time.Duration {
|
||||
return time.Duration(1000*seconds) * time.Millisecond
|
||||
}
|
||||
|
||||
// Err checks if there is an error in the given context
|
||||
// and wraps the previous error inside a standarderror.Error.
|
||||
func Err(ctx context.Context, previousErr error) error {
|
||||
const op string = "timeout.Err"
|
||||
if previousErr == nil {
|
||||
panic(fmt.Sprintf("%s: previous error should not be nil", op))
|
||||
}
|
||||
err := ctx.Err()
|
||||
if err == nil {
|
||||
return previousErr
|
||||
}
|
||||
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
|
||||
return &standarderror.Error{
|
||||
Code: standarderror.Timeout,
|
||||
Message: "context has timed out",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
return &standarderror.Error{
|
||||
Message: "context finished with an error",
|
||||
Op: op,
|
||||
Err: previousErr,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +1,40 @@
|
||||
package timeout
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
|
||||
"github.com/thecodingmachine/gotenberg/test"
|
||||
)
|
||||
|
||||
func TestContext(t *testing.T) {
|
||||
ctx, cancel := Context(1.5)
|
||||
assert.NotNil(t, ctx)
|
||||
assert.NotNil(t, cancel)
|
||||
}
|
||||
|
||||
func TestDuration(t *testing.T) {
|
||||
expected := time.Duration(1500) * time.Millisecond
|
||||
result := Duration(1.5)
|
||||
assert.Equal(t, expected.String(), result.String())
|
||||
}
|
||||
|
||||
func TestErr(t *testing.T) {
|
||||
previousErr := errors.New("previous error")
|
||||
// should be OK.
|
||||
ctx, cancel := Context(5)
|
||||
defer cancel()
|
||||
assert.NotNil(t, Err(ctx, previousErr))
|
||||
// should timeout.
|
||||
ctx, cancel = Context(0.5)
|
||||
defer cancel()
|
||||
time.Sleep(Duration(1))
|
||||
err := Err(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized := test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Timeout, standardized.Code)
|
||||
// should failed.
|
||||
ctx, cancel = Context(5)
|
||||
cancel()
|
||||
err = Err(ctx, previousErr)
|
||||
assert.NotNil(t, err)
|
||||
standardized = test.RequireStandardError(t, err)
|
||||
assert.Equal(t, standarderror.Internal, standarderror.Code(err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user