2019-08-07 15:28:27 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package errs2_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/internal/errs2"
|
2019-09-19 05:46:39 +01:00
|
|
|
"storj.io/storj/pkg/rpc/rpcstatus"
|
2019-08-07 15:28:27 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIsCanceled(t *testing.T) {
|
|
|
|
nestedErr := errs.Class("nested error")
|
|
|
|
combinedErr := errs.New("combined error")
|
|
|
|
parentCtx, cancel := context.WithCancel(context.Background())
|
|
|
|
childCtx, childCancel := context.WithTimeout(parentCtx, 30*time.Second)
|
|
|
|
defer childCancel()
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// context errors
|
|
|
|
parentErr := parentCtx.Err()
|
|
|
|
childErr := childCtx.Err()
|
|
|
|
|
|
|
|
require.Equal(t, parentErr, context.Canceled)
|
|
|
|
require.Equal(t, childErr, context.Canceled)
|
|
|
|
|
|
|
|
require.True(t, errs2.IsCanceled(parentErr))
|
|
|
|
require.True(t, errs2.IsCanceled(childErr))
|
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
// rpc errors
|
|
|
|
rpcErr := rpcstatus.Error(rpcstatus.Canceled, context.Canceled.Error())
|
2019-08-07 15:28:27 +01:00
|
|
|
|
2019-09-19 05:46:39 +01:00
|
|
|
require.NotEqual(t, rpcErr, context.Canceled)
|
|
|
|
require.True(t, errs2.IsCanceled(rpcErr))
|
2019-08-07 15:28:27 +01:00
|
|
|
|
|
|
|
// nested errors
|
|
|
|
nestedParentErr := nestedErr.Wrap(parentErr)
|
|
|
|
nestedChildErr := nestedErr.Wrap(childErr)
|
2019-09-19 05:46:39 +01:00
|
|
|
nestedRPCErr := nestedErr.Wrap(rpcErr)
|
2019-08-07 15:28:27 +01:00
|
|
|
|
|
|
|
require.NotEqual(t, nestedParentErr, context.Canceled)
|
|
|
|
require.NotEqual(t, nestedChildErr, context.Canceled)
|
2019-09-19 05:46:39 +01:00
|
|
|
require.NotEqual(t, nestedRPCErr, context.Canceled)
|
2019-08-07 15:28:27 +01:00
|
|
|
|
|
|
|
require.True(t, errs2.IsCanceled(nestedParentErr))
|
|
|
|
require.True(t, errs2.IsCanceled(nestedChildErr))
|
|
|
|
require.True(t, errs2.IsCanceled(nestedChildErr))
|
|
|
|
|
|
|
|
// combined errors
|
|
|
|
combinedParentErr := errs.Combine(combinedErr, parentErr)
|
|
|
|
combinedChildErr := errs.Combine(combinedErr, childErr)
|
2019-09-19 05:46:39 +01:00
|
|
|
combinedRPCErr := errs.Combine(combinedErr, childErr)
|
2019-08-07 15:28:27 +01:00
|
|
|
|
|
|
|
require.NotEqual(t, combinedParentErr, context.Canceled)
|
|
|
|
require.NotEqual(t, combinedChildErr, context.Canceled)
|
2019-09-19 05:46:39 +01:00
|
|
|
require.NotEqual(t, combinedRPCErr, context.Canceled)
|
2019-08-07 15:28:27 +01:00
|
|
|
|
|
|
|
require.True(t, errs2.IsCanceled(combinedParentErr))
|
|
|
|
require.True(t, errs2.IsCanceled(combinedChildErr))
|
2019-09-19 05:46:39 +01:00
|
|
|
require.True(t, errs2.IsCanceled(combinedRPCErr))
|
2019-08-07 15:28:27 +01:00
|
|
|
}
|