github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/internal/operation/params_test.go (about)

     1  package operation
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
    10  	"google.golang.org/protobuf/types/known/durationpb"
    11  
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xcontext"
    13  )
    14  
    15  func TestParams(t *testing.T) {
    16  	for _, tt := range []struct {
    17  		ctx                  context.Context //nolint:containedctx
    18  		preferContextTimeout bool
    19  		timeout              time.Duration
    20  		cancelAfter          time.Duration
    21  		mode                 Mode
    22  		exp                  *Ydb_Operations.OperationParams
    23  	}{
    24  		{
    25  			ctx:         context.Background(),
    26  			timeout:     0,
    27  			cancelAfter: 0,
    28  			mode:        0,
    29  			exp:         nil,
    30  		},
    31  		{
    32  			ctx: WithTimeout(
    33  				context.Background(),
    34  				time.Second*5,
    35  			),
    36  			timeout:     0,
    37  			cancelAfter: 0,
    38  			mode:        0,
    39  			exp: &Ydb_Operations.OperationParams{
    40  				OperationTimeout: durationpb.New(time.Second * 5),
    41  			},
    42  		},
    43  		{
    44  			ctx: WithTimeout(
    45  				WithTimeout(
    46  					WithTimeout(
    47  						WithTimeout(
    48  							WithTimeout(
    49  								context.Background(),
    50  								time.Second*1,
    51  							),
    52  							time.Second*2,
    53  						),
    54  						time.Second*3,
    55  					),
    56  					time.Second*4,
    57  				),
    58  				time.Second*5,
    59  			),
    60  			timeout:     0,
    61  			cancelAfter: 0,
    62  			mode:        0,
    63  			exp: &Ydb_Operations.OperationParams{
    64  				OperationTimeout: durationpb.New(time.Second * 1),
    65  			},
    66  		},
    67  		{
    68  			ctx: WithTimeout(
    69  				WithTimeout(
    70  					WithTimeout(
    71  						WithTimeout(
    72  							WithTimeout(
    73  								context.Background(),
    74  								time.Second*5,
    75  							),
    76  							time.Second*4,
    77  						),
    78  						time.Second*3,
    79  					),
    80  					time.Second*2,
    81  				),
    82  				time.Second*1,
    83  			),
    84  			timeout:     0,
    85  			cancelAfter: 0,
    86  			mode:        0,
    87  			exp: &Ydb_Operations.OperationParams{
    88  				OperationTimeout: durationpb.New(time.Second * 1),
    89  			},
    90  		},
    91  		{
    92  			ctx: WithCancelAfter(
    93  				context.Background(),
    94  				time.Second*5,
    95  			),
    96  			timeout:     0,
    97  			cancelAfter: 0,
    98  			mode:        0,
    99  			exp: &Ydb_Operations.OperationParams{
   100  				CancelAfter: durationpb.New(time.Second * 5),
   101  			},
   102  		},
   103  		{
   104  			ctx: WithCancelAfter(
   105  				WithCancelAfter(
   106  					WithCancelAfter(
   107  						WithCancelAfter(
   108  							WithCancelAfter(
   109  								context.Background(),
   110  								time.Second*1,
   111  							),
   112  							time.Second*2,
   113  						),
   114  						time.Second*3,
   115  					),
   116  					time.Second*4,
   117  				),
   118  				time.Second*5,
   119  			),
   120  			timeout:     0,
   121  			cancelAfter: 0,
   122  			mode:        0,
   123  			exp: &Ydb_Operations.OperationParams{
   124  				CancelAfter: durationpb.New(time.Second * 1),
   125  			},
   126  		},
   127  		{
   128  			ctx: WithCancelAfter(
   129  				WithCancelAfter(
   130  					WithCancelAfter(
   131  						WithCancelAfter(
   132  							WithCancelAfter(
   133  								context.Background(),
   134  								time.Second*5,
   135  							),
   136  							time.Second*4,
   137  						),
   138  						time.Second*3,
   139  					),
   140  					time.Second*2,
   141  				),
   142  				time.Second*1,
   143  			),
   144  			timeout:     0,
   145  			cancelAfter: 0,
   146  			mode:        0,
   147  			exp: &Ydb_Operations.OperationParams{
   148  				CancelAfter: durationpb.New(time.Second * 1),
   149  			},
   150  		},
   151  		{
   152  			ctx: WithCancelAfter(
   153  				WithTimeout(
   154  					context.Background(),
   155  					time.Second*5,
   156  				),
   157  				time.Second*5,
   158  			),
   159  			timeout:     0,
   160  			cancelAfter: 0,
   161  			mode:        0,
   162  			exp: &Ydb_Operations.OperationParams{
   163  				OperationTimeout: durationpb.New(time.Second * 5),
   164  				CancelAfter:      durationpb.New(time.Second * 5),
   165  			},
   166  		},
   167  		{
   168  			ctx: WithCancelAfter(
   169  				WithTimeout(
   170  					context.Background(),
   171  					time.Second*5,
   172  				),
   173  				time.Second*5,
   174  			),
   175  			timeout:     0,
   176  			cancelAfter: 0,
   177  			mode:        ModeSync,
   178  			exp: &Ydb_Operations.OperationParams{
   179  				OperationMode:    Ydb_Operations.OperationParams_SYNC,
   180  				OperationTimeout: durationpb.New(time.Second * 5),
   181  				CancelAfter:      durationpb.New(time.Second * 5),
   182  			},
   183  		},
   184  		{
   185  			ctx: WithCancelAfter(
   186  				WithTimeout(
   187  					context.Background(),
   188  					time.Second*5,
   189  				),
   190  				time.Second*5,
   191  			),
   192  			timeout:     time.Second * 2,
   193  			cancelAfter: 0,
   194  			mode:        ModeSync,
   195  			exp: &Ydb_Operations.OperationParams{
   196  				OperationMode:    Ydb_Operations.OperationParams_SYNC,
   197  				OperationTimeout: durationpb.New(time.Second * 5),
   198  				CancelAfter:      durationpb.New(time.Second * 5),
   199  			},
   200  		},
   201  		{
   202  			ctx: WithCancelAfter(
   203  				WithTimeout(
   204  					context.Background(),
   205  					time.Second*5,
   206  				),
   207  				time.Second*5,
   208  			),
   209  			timeout:     time.Second * 2,
   210  			cancelAfter: 0,
   211  			mode:        ModeAsync,
   212  			exp: &Ydb_Operations.OperationParams{
   213  				OperationMode:    Ydb_Operations.OperationParams_ASYNC,
   214  				OperationTimeout: durationpb.New(time.Second * 5),
   215  				CancelAfter:      durationpb.New(time.Second * 5),
   216  			},
   217  		},
   218  		{
   219  			ctx: func() context.Context {
   220  				ctx, _ := xcontext.WithTimeout(WithCancelAfter(
   221  					WithTimeout(
   222  						context.Background(),
   223  						time.Second*5,
   224  					),
   225  					time.Second*5,
   226  				), time.Second*10)
   227  
   228  				return ctx
   229  			}(),
   230  			timeout:     time.Second * 2,
   231  			cancelAfter: 0,
   232  			mode:        ModeAsync,
   233  			exp: &Ydb_Operations.OperationParams{
   234  				OperationMode:    Ydb_Operations.OperationParams_ASYNC,
   235  				OperationTimeout: durationpb.New(time.Second * 5),
   236  				CancelAfter:      durationpb.New(time.Second * 5),
   237  			},
   238  		},
   239  		{
   240  			ctx: func() context.Context {
   241  				ctx, _ := xcontext.WithTimeout(WithCancelAfter(
   242  					WithTimeout(
   243  						context.Background(),
   244  						time.Second*5,
   245  					),
   246  					time.Second*5,
   247  				), time.Second*1)
   248  
   249  				return ctx
   250  			}(),
   251  			timeout:     time.Second * 2,
   252  			cancelAfter: 0,
   253  			mode:        ModeAsync,
   254  			exp: &Ydb_Operations.OperationParams{
   255  				OperationMode:    Ydb_Operations.OperationParams_ASYNC,
   256  				OperationTimeout: durationpb.New(time.Second * 5),
   257  				CancelAfter:      durationpb.New(time.Second * 5),
   258  			},
   259  		},
   260  		{
   261  			ctx: func() context.Context {
   262  				ctx, _ := xcontext.WithTimeout(WithCancelAfter(
   263  					WithTimeout(
   264  						context.Background(),
   265  						time.Second*5,
   266  					),
   267  					time.Second*5,
   268  				), time.Second*1)
   269  
   270  				return ctx
   271  			}(),
   272  			preferContextTimeout: true,
   273  			timeout:              time.Second * 2,
   274  			cancelAfter:          0,
   275  			mode:                 ModeSync,
   276  			exp: &Ydb_Operations.OperationParams{
   277  				OperationMode:    Ydb_Operations.OperationParams_SYNC,
   278  				OperationTimeout: durationpb.New(time.Second * 1),
   279  				CancelAfter:      durationpb.New(time.Second * 5),
   280  			},
   281  		},
   282  		{
   283  			ctx: func() context.Context {
   284  				ctx, _ := xcontext.WithTimeout(context.Background(), time.Second*1)
   285  
   286  				return ctx
   287  			}(),
   288  			preferContextTimeout: true,
   289  			timeout:              time.Second * 2,
   290  			cancelAfter:          0,
   291  			mode:                 ModeSync,
   292  			exp: &Ydb_Operations.OperationParams{
   293  				OperationMode:    Ydb_Operations.OperationParams_SYNC,
   294  				OperationTimeout: durationpb.New(time.Second * 1),
   295  			},
   296  		},
   297  	} {
   298  		t.Run("", func(t *testing.T) {
   299  			got := Params(tt.ctx, tt.timeout, tt.cancelAfter, tt.mode)
   300  			t.Logf(
   301  				"Params(): {%v}, compare to: {%v}",
   302  				got,
   303  				tt.exp,
   304  			)
   305  			if !tt.preferContextTimeout {
   306  				if !reflect.DeepEqual(got, tt.exp) {
   307  					t.Errorf(
   308  						"Params(): {%v}, want: {%v}",
   309  						got,
   310  						tt.exp,
   311  					)
   312  				}
   313  
   314  				return
   315  			}
   316  			if !reflect.DeepEqual(got.GetOperationMode(), tt.exp.GetOperationMode()) {
   317  				t.Errorf(
   318  					"Params().OperationMode: %v, want: %v",
   319  					got.GetOperationMode(),
   320  					tt.exp.GetOperationMode(),
   321  				)
   322  			}
   323  			if !reflect.DeepEqual(got.GetCancelAfter(), tt.exp.GetCancelAfter()) {
   324  				t.Errorf(
   325  					"Params().CancelAfter: %v, want: %v",
   326  					got.GetCancelAfter().AsDuration(),
   327  					tt.exp.GetCancelAfter().AsDuration(),
   328  				)
   329  			}
   330  			if got.GetOperationTimeout().AsDuration() > tt.exp.GetOperationTimeout().AsDuration() {
   331  				t.Errorf(
   332  					"Params().OperationTimeout: %v, want: <= %v",
   333  					got.GetOperationTimeout().AsDuration(),
   334  					tt.exp.GetOperationTimeout().AsDuration(),
   335  				)
   336  			}
   337  		})
   338  	}
   339  }