vitess.io/vitess@v0.16.2/go/vt/vtgate/status_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 vtgate
    18  
    19  import (
    20  	"bytes"
    21  	"html/template"
    22  	"reflect"
    23  	"testing"
    24  	"time"
    25  
    26  	topodatapb "vitess.io/vitess/go/vt/proto/topodata"
    27  )
    28  
    29  func TestTabletStatusAggregator(t *testing.T) {
    30  	aggr := &TabletStatusAggregator{
    31  		Keyspace:   "k",
    32  		Shard:      "s",
    33  		TabletType: topodatapb.TabletType_REPLICA,
    34  		Name:       "n",
    35  		Addr:       "a",
    36  	}
    37  	qi := &queryInfo{
    38  		aggr:       aggr,
    39  		addr:       "",
    40  		tabletType: topodatapb.TabletType_REPLICA,
    41  		elapsed:    10 * time.Millisecond,
    42  		hasError:   false,
    43  	}
    44  	aggr.processQueryInfo(qi)
    45  	aggr.resetNextSlot()
    46  	qi = &queryInfo{
    47  		aggr:       aggr,
    48  		addr:       "",
    49  		tabletType: topodatapb.TabletType_REPLICA,
    50  		elapsed:    8 * time.Millisecond,
    51  		hasError:   false,
    52  	}
    53  	aggr.processQueryInfo(qi)
    54  	qi = &queryInfo{
    55  		aggr:       aggr,
    56  		addr:       "",
    57  		tabletType: topodatapb.TabletType_REPLICA,
    58  		elapsed:    3 * time.Millisecond,
    59  		hasError:   true,
    60  	}
    61  	aggr.processQueryInfo(qi)
    62  	want := &TabletCacheStatus{
    63  		Keyspace:   "k",
    64  		Shard:      "s",
    65  		Name:       "n",
    66  		TabletType: topodatapb.TabletType_REPLICA,
    67  		Addr:       "a",
    68  		QueryCount: 3,
    69  		QueryError: 1,
    70  		QPS:        0.05,
    71  		AvgLatency: 7,
    72  	}
    73  	got := aggr.GetCacheStatus()
    74  	if !reflect.DeepEqual(got, want) {
    75  		t.Errorf("aggr.GetCacheStatus() =\n%+v, want =\n%+v", got, want)
    76  	}
    77  	// reset values in idx=0
    78  	for i := 0; i < 59; i++ {
    79  		aggr.resetNextSlot()
    80  	}
    81  	qi = &queryInfo{
    82  		aggr:       aggr,
    83  		addr:       "b",
    84  		tabletType: topodatapb.TabletType_PRIMARY,
    85  		elapsed:    9 * time.Millisecond,
    86  		hasError:   false,
    87  	}
    88  	aggr.processQueryInfo(qi)
    89  	qi = &queryInfo{
    90  		aggr:       aggr,
    91  		addr:       "",
    92  		tabletType: topodatapb.TabletType_PRIMARY,
    93  		elapsed:    6 * time.Millisecond,
    94  		hasError:   true,
    95  	}
    96  	aggr.processQueryInfo(qi)
    97  	want = &TabletCacheStatus{
    98  		Keyspace:   "k",
    99  		Shard:      "s",
   100  		Name:       "n",
   101  		TabletType: topodatapb.TabletType_PRIMARY,
   102  		Addr:       "b",
   103  		QueryCount: 2,
   104  		QueryError: 1,
   105  		QPS:        0.03333333333333333,
   106  		AvgLatency: 7.5,
   107  	}
   108  	got = aggr.GetCacheStatus()
   109  	if !reflect.DeepEqual(got, want) {
   110  		t.Errorf("aggr.GetCacheStatus() =\n%+v, want =\n%+v", got, want)
   111  	}
   112  
   113  	// Make sure the HTML rendering of the cache works.
   114  	// This will catch most typos.
   115  	templ, err := template.New("").Parse(StatusTemplate)
   116  	if err != nil {
   117  		t.Fatalf("error parsing template: %v", err)
   118  	}
   119  	wr := &bytes.Buffer{}
   120  	if err := templ.Execute(wr, []*TabletCacheStatus{got}); err != nil {
   121  		t.Fatalf("error executing template: %v", err)
   122  	}
   123  }