fixes waitTimeout, waitDelay not working with float

This commit is contained in:
Julien Neuhart
2019-07-08 17:24:17 +02:00
parent 186c09f29b
commit 9af00d6d58
11 changed files with 110 additions and 18 deletions

View File

@@ -13,6 +13,7 @@ import (
"github.com/mafredri/cdp/protocol/target"
"github.com/mafredri/cdp/rpcc"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
"golang.org/x/sync/errgroup"
)
@@ -39,9 +40,7 @@ type ChromeOptions struct {
func (p *chrome) Print(destination string) error {
const op = "printer.chrome.Print"
// FIXME duration not working with float
duration := time.Duration(p.opts.WaitTimeout+p.opts.WaitDelay) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), duration)
ctx, cancel := timeout.Context(p.opts.WaitTimeout + p.opts.WaitDelay)
defer cancel()
devt, err := devtool.New("http://localhost:9222").Version(ctx)
if err != nil {
@@ -153,8 +152,7 @@ func (p *chrome) navigate(ctx context.Context, client *cdp.Client) error {
return &standarderror.Error{Op: op, Err: err}
}
// wait for a given amount of time (useful for javascript delay).
// FIXME duration not working with float
time.Sleep(time.Duration(p.opts.WaitDelay) * time.Second)
time.Sleep(timeout.Duration(p.opts.WaitDelay))
return nil
}

View File

@@ -3,9 +3,9 @@ package printer
import (
"context"
"os/exec"
"time"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
)
type merge struct {
@@ -31,8 +31,7 @@ func NewMerge(fpaths []string, opts *MergeOptions) Printer {
func (p *merge) Print(destination string) error {
const op = "printer.merge.Print"
if p.ctx == nil {
// FIXME duration not working with float
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.opts.WaitTimeout)*time.Second)
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
defer cancel()
p.ctx = ctx
}

View File

@@ -7,10 +7,10 @@ import (
"os/exec"
"path/filepath"
"sync"
"time"
"github.com/labstack/gommon/random"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/internal/pkg/timeout"
)
type office struct {
@@ -35,8 +35,7 @@ func NewOffice(fpaths []string, opts *OfficeOptions) Printer {
func (p *office) Print(destination string) error {
const op = "printer.office.Print"
// FIXME duration not working with float
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.opts.WaitTimeout)*time.Second)
ctx, cancel := timeout.Context(p.opts.WaitTimeout)
defer cancel()
fpaths := make([]string, len(p.fpaths))
dirPath := filepath.Dir(destination)

View File

@@ -0,0 +1,3 @@
// Package timeout helps managing
// context with timeout.
package timeout

View File

@@ -0,0 +1,43 @@
package timeout
import (
"context"
"strings"
"time"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
)
// Context creates a context with timeout for
// given second.
func Context(seconds float64) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), Duration(seconds))
}
// Duration creates a duration from seconds.
func Duration(seconds float64) time.Duration {
return time.Duration(1000*seconds) * time.Millisecond
}
// Err returns a standarderror.Error
// if the context has an error.
func Err(ctx context.Context) error {
const op = "timeout.Err"
err := ctx.Err()
if err == nil {
return nil
}
if strings.Contains(err.Error(), context.DeadlineExceeded.Error()) {
return &standarderror.Error{
Code: standarderror.Timeout,
Message: "context has timed out",
Op: op,
Err: err,
}
}
return &standarderror.Error{
Message: "context finished with an error",
Op: op,
Err: err,
}
}

View File

@@ -0,0 +1,38 @@
package timeout
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/thecodingmachine/gotenberg/internal/pkg/standarderror"
"github.com/thecodingmachine/gotenberg/test"
)
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) {
// should be OK.
ctx, cancel := Context(5)
defer cancel()
assert.Nil(t, Err(ctx))
// should timeout.
ctx, cancel = Context(0.5)
defer cancel()
time.Sleep(Duration(1))
err := Err(ctx)
assert.NotNil(t, err)
standardized := test.RequireStandardError(t, err)
assert.Equal(t, standardized.Code, standarderror.Timeout)
// should failed.
ctx, cancel = Context(5)
cancel()
err = Err(ctx)
assert.NotNil(t, err)
standardized = test.RequireStandardError(t, err)
assert.Equal(t, standarderror.Code(err), standarderror.Internal)
}