Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wbrowne committed Aug 27, 2024
1 parent 6e9d714 commit 46675be
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 4 deletions.
16 changes: 16 additions & 0 deletions backend/data_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
return RequestStatusCancelled, nil
}

if isHTTPTimeoutError(innerErr) {
return RequestStatusError, nil
}

// Set downstream status source in the context if there's at least one response with downstream status source,
// and if there's no plugin error
var hasPluginError bool
var hasDownstreamError bool
var hasCancelledError bool
var hasHTTPTimeoutError bool
for _, r := range resp.Responses {
if r.Error == nil {
continue
Expand All @@ -50,6 +55,10 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
if isCancelledError(r.Error) {
hasCancelledError = true
}
if isHTTPTimeoutError(r.Error) {
hasHTTPTimeoutError = true
}

if r.ErrorSource == ErrorSourceDownstream {
hasDownstreamError = true
} else {
Expand All @@ -64,6 +73,13 @@ func (a *dataSDKAdapter) QueryData(ctx context.Context, req *pluginv2.QueryDataR
return RequestStatusCancelled, nil
}

if hasHTTPTimeoutError {
if err := WithDownstreamErrorSource(ctx); err != nil {
return RequestStatusError, fmt.Errorf("failed to set downstream status source: %w", errors.Join(innerErr, err))
}
return RequestStatusError, nil
}

// A plugin error has higher priority than a downstream error,
// so set to downstream only if there's no plugin error
if hasDownstreamError && !hasPluginError {
Expand Down
5 changes: 1 addition & 4 deletions backend/error_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"net"
"net/http"
)

Expand Down Expand Up @@ -63,9 +62,7 @@ func IsDownstreamError(err error) bool {
return true
}

// if error is HTTP network timeout error, we should treat it as downstream error
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
if isHTTPTimeoutError(err) {
return true
}

Expand Down
85 changes: 85 additions & 0 deletions backend/error_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package backend

import (
"fmt"
"net"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsDownstreamError(t *testing.T) {
tcs := []struct {
name string
err error
expected bool
}{
{
name: "nil",
err: nil,
expected: false,
},
{
name: "downstream error",
err: DownstreamError(nil),
expected: true,
},
{
name: "timeout network error",
err: newFakeNetworkError(true, false),
expected: true,
},
{
name: "temporary timeout network error",
err: newFakeNetworkError(true, true),
expected: true,
},
{
name: "non-timeout network error",
err: newFakeNetworkError(false, false),
expected: false,
},
{
name: "os.ErrDeadlineExceeded",
err: os.ErrDeadlineExceeded,
expected: true,
},
{
name: "other error",
err: fmt.Errorf("other error"),
expected: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
assert.Equalf(t, tc.expected, IsDownstreamError(tc.err), "IsDownstreamError(%v)", tc.err)
})
}
}

var _ net.Error = &fakeNetworkError{}

type fakeNetworkError struct {
timeout bool
temporary bool
}

func newFakeNetworkError(timeout, temporary bool) *fakeNetworkError {
return &fakeNetworkError{
timeout: timeout,
temporary: temporary,
}
}

func (d *fakeNetworkError) Error() string {
return "dummy timeout error"
}

func (d *fakeNetworkError) Timeout() bool {
return d.timeout
}

func (d *fakeNetworkError) Temporary() bool {
return d.temporary
}
11 changes: 11 additions & 0 deletions backend/request_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package backend
import (
"context"
"errors"
"net"
"os"
"strings"

grpccodes "google.golang.org/grpc/codes"
Expand Down Expand Up @@ -105,3 +107,12 @@ func RequestStatusFromProtoQueryDataResponse(res *pluginv2.QueryDataResponse, er
func isCancelledError(err error) bool {
return errors.Is(err, context.Canceled) || grpcstatus.Code(err) == grpccodes.Canceled
}

func isHTTPTimeoutError(err error) bool {
var netErr net.Error
if errors.As(err, &netErr) && netErr.Timeout() {
return true
}

return errors.Is(err, os.ErrDeadlineExceeded) // relacement for os.IsTimeout(err)
}

0 comments on commit 46675be

Please sign in to comment.