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

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