vitess.io/vitess@v0.16.2/go/vt/vtgate/buffer/variables_test.go (about)

     1  /*
     2  Copyright 2019 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 buffer
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/spf13/pflag"
    26  
    27  	"vitess.io/vitess/go/stats"
    28  )
    29  
    30  func TestVariables(t *testing.T) {
    31  	fs := pflag.NewFlagSet("vtgate_buffer_variables_test", pflag.ContinueOnError)
    32  	registerFlags(fs)
    33  	if err := fs.Parse(nil); err != nil {
    34  		t.Errorf("failed to parse with default values: %v", err)
    35  	}
    36  
    37  	fs.Set("buffer_size", "23")
    38  	defer func() {
    39  		fs.Set("buffer_size", "1")
    40  	}()
    41  
    42  	// Create new buffer which will the flags.
    43  	NewConfigFromFlags()
    44  
    45  	if got, want := bufferSizeStat.Get(), int64(23); got != want {
    46  		t.Fatalf("BufferSize variable not set during initilization: got = %v, want = %v", got, want)
    47  	}
    48  }
    49  
    50  func TestVariablesAreInitialized(t *testing.T) {
    51  	// Create a new buffer and make a call which will create the shardBuffer object.
    52  	// After that, the variables should be initialized for that shard.
    53  	b := New(NewDefaultConfig())
    54  	_, err := b.WaitForFailoverEnd(context.Background(), "init_test", "0", nil /* err */)
    55  	if err != nil {
    56  		t.Fatalf("buffer should just passthrough and not return an error: %v", err)
    57  	}
    58  
    59  	statsKey := []string{"init_test", "0"}
    60  	type testCase struct {
    61  		desc     string
    62  		counter  *stats.CountersWithMultiLabels
    63  		statsKey []string
    64  	}
    65  	testCases := []testCase{
    66  		{"starts", starts, statsKey},
    67  		{"failoverDurationSumMs", failoverDurationSumMs, statsKey},
    68  		{"utilizationSum", &utilizationSum.CountersWithMultiLabels, statsKey},
    69  		{"utilizationDryRunSum", utilizationDryRunSum, statsKey},
    70  		{"requestsBuffered", requestsBuffered, statsKey},
    71  		{"requestsBufferedDryRun", requestsBufferedDryRun, statsKey},
    72  		{"requestsDrained", requestsDrained, statsKey},
    73  	}
    74  	for _, r := range stopReasons {
    75  		testCases = append(testCases, testCase{"stops", stops, append(statsKey, string(r))})
    76  	}
    77  	for _, r := range evictReasons {
    78  		testCases = append(testCases, testCase{"evicted", requestsEvicted, append(statsKey, string(r))})
    79  	}
    80  	for _, r := range skippedReasons {
    81  		testCases = append(testCases, testCase{"skipped", requestsSkipped, append(statsKey, string(r))})
    82  	}
    83  
    84  	for _, tc := range testCases {
    85  		wantValue := 0
    86  		if len(tc.statsKey) == 3 && tc.statsKey[2] == string(skippedDisabled) {
    87  			// The request passed through above was registered as skipped.
    88  			wantValue = 1
    89  		}
    90  		if err := checkEntry(tc.counter, tc.statsKey, wantValue); err != nil {
    91  			t.Fatalf("variable: %v not correctly initialized: %v", tc.desc, err)
    92  		}
    93  	}
    94  }
    95  
    96  func checkEntry(counters *stats.CountersWithMultiLabels, statsKey []string, want int) error {
    97  	name := strings.Join(statsKey, ".")
    98  	got, ok := counters.Counts()[name]
    99  	if !ok {
   100  		return fmt.Errorf("no entry for: %v", name)
   101  	}
   102  	if got != int64(want) {
   103  		return fmt.Errorf("wrong value for entry: %v got = %v, want = %v", name, got, want)
   104  	}
   105  
   106  	return nil
   107  }