vitess.io/vitess@v0.16.2/go/vt/topo/locks_test.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package topo
    18  
    19  import (
    20  	"os"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/spf13/pflag"
    25  	"github.com/stretchr/testify/require"
    26  
    27  	"vitess.io/vitess/go/internal/flag"
    28  )
    29  
    30  // TestGetLockTimeout tests the behaviour of
    31  // getLockTimeout function in different situations where
    32  // the two flags `remote_operation_timeout` and `lock-timeout` are
    33  // provided or not.
    34  func TestGetLockTimeout(t *testing.T) {
    35  	tests := []struct {
    36  		description                 string
    37  		lockTimeoutValue            string
    38  		remoteOperationTimeoutValue string
    39  		expectedLockTimeout         time.Duration
    40  	}{
    41  		{
    42  			description:                 "no flags specified",
    43  			lockTimeoutValue:            "",
    44  			remoteOperationTimeoutValue: "",
    45  			expectedLockTimeout:         45 * time.Second,
    46  		}, {
    47  			description:                 "lock-timeout flag specified",
    48  			lockTimeoutValue:            "33s",
    49  			remoteOperationTimeoutValue: "",
    50  			expectedLockTimeout:         33 * time.Second,
    51  		}, {
    52  			description:                 "remote operation timeout flag specified",
    53  			lockTimeoutValue:            "",
    54  			remoteOperationTimeoutValue: "33s",
    55  			expectedLockTimeout:         33 * time.Second,
    56  		}, {
    57  			description:                 "both flags specified",
    58  			lockTimeoutValue:            "33s",
    59  			remoteOperationTimeoutValue: "22s",
    60  			expectedLockTimeout:         33 * time.Second,
    61  		}, {
    62  			description:                 "remote operation timeout flag specified to the default",
    63  			lockTimeoutValue:            "",
    64  			remoteOperationTimeoutValue: "15s",
    65  			expectedLockTimeout:         15 * time.Second,
    66  		}, {
    67  			description:                 "lock-timeout flag specified to the default",
    68  			lockTimeoutValue:            "45s",
    69  			remoteOperationTimeoutValue: "33s",
    70  			expectedLockTimeout:         45 * time.Second,
    71  		},
    72  	}
    73  
    74  	for _, tt := range tests {
    75  		t.Run(tt.description, func(t *testing.T) {
    76  			oldLockTimeout := LockTimeout
    77  			oldRemoteOpsTimeout := RemoteOperationTimeout
    78  			defer func() {
    79  				LockTimeout = oldLockTimeout
    80  				RemoteOperationTimeout = oldRemoteOpsTimeout
    81  			}()
    82  			var args []string
    83  			if tt.lockTimeoutValue != "" {
    84  				args = append(args, "--lock-timeout", tt.lockTimeoutValue)
    85  			}
    86  			if tt.remoteOperationTimeoutValue != "" {
    87  				args = append(args, "--remote_operation_timeout", tt.remoteOperationTimeoutValue)
    88  			}
    89  			os.Args = os.Args[0:1]
    90  			os.Args = append(os.Args, args...)
    91  
    92  			fs := pflag.NewFlagSet("test", pflag.ExitOnError)
    93  			registerTopoLockFlags(fs)
    94  			flag.Parse(fs)
    95  
    96  			val := getLockTimeout()
    97  			require.Equal(t, tt.expectedLockTimeout, val)
    98  		})
    99  	}
   100  
   101  }