github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/xcontext/context_with_cancel_test.go (about)

     1  package xcontext
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func TestContextWithCancel(t *testing.T) {
    11  	t.Run("SimpleCancel", func(t *testing.T) {
    12  		ctx, cancel := WithCancel(context.Background())
    13  		cancel()
    14  		require.ErrorIs(t, ctx.Err(), context.Canceled)
    15  	})
    16  
    17  	t.Run("CancelBeforeParent", func(t *testing.T) {
    18  		parent, parentCancel := context.WithCancel(context.Background())
    19  		ctx, cancel := WithCancel(parent)
    20  
    21  		cancel()
    22  		parentCancel()
    23  
    24  		require.ErrorIs(t, ctx.Err(), context.Canceled)
    25  	})
    26  
    27  	t.Run("CancelAfterParent", func(t *testing.T) {
    28  		parent, parentCancel := context.WithCancel(context.Background())
    29  		ctx, cancel := WithCancel(parent)
    30  
    31  		parentCancel()
    32  		cancel()
    33  
    34  		require.ErrorIs(t, ctx.Err(), context.Canceled)
    35  	})
    36  }
    37  
    38  func TestContextWithCancelError(t *testing.T) {
    39  	for _, tt := range []struct {
    40  		err error
    41  		str string
    42  	}{
    43  		{
    44  			err: func() error {
    45  				parentCtx, parentCancel := WithCancel(context.Background())
    46  				childCtx, childCancel := WithCancel(parentCtx)
    47  				parentCancel()
    48  				childCancel()
    49  
    50  				return childCtx.Err()
    51  			}(),
    52  			str: "'context canceled' at `github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext.TestContextWithCancelError.func1(context_with_cancel_test.go:47)`", //nolint:lll
    53  		},
    54  		{
    55  			err: func() error {
    56  				ctx, cancel := WithCancel(context.Background())
    57  				cancel()
    58  
    59  				return ctx.Err()
    60  			}(),
    61  			str: "'context canceled' at `github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext.TestContextWithCancelError.func2(context_with_cancel_test.go:57)`", //nolint:lll
    62  		},
    63  		{
    64  			err: func() error {
    65  				parentCtx, _ := WithTimeout(context.Background(), 0)
    66  				childCtx, cancel := WithCancel(parentCtx)
    67  				cancel()
    68  
    69  				return childCtx.Err()
    70  			}(),
    71  			str: "'context deadline exceeded' from `github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext.TestContextWithCancelError.func3(context_with_cancel_test.go:65)`", //nolint:lll
    72  		},
    73  		{
    74  			err: func() error {
    75  				parentCtx, _ := context.WithTimeout(context.Background(), 0) //nolint:govet
    76  				childCtx, cancel := WithCancel(parentCtx)
    77  				cancel()
    78  
    79  				return childCtx.Err()
    80  			}(),
    81  			str: "context deadline exceeded",
    82  		},
    83  	} {
    84  		t.Run("", func(t *testing.T) {
    85  			require.Equal(t, tt.str, tt.err.Error())
    86  		})
    87  	}
    88  }