vitess.io/vitess@v0.16.2/go/vt/throttler/throttlerlogz_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 throttler
    18  
    19  import (
    20  	"net/http"
    21  	"net/http/httptest"
    22  	"strings"
    23  	"testing"
    24  )
    25  
    26  func TestThrottlerlogzHandler_MissingSlash(t *testing.T) {
    27  	request, _ := http.NewRequest("GET", "/throttlerlogz", nil)
    28  	response := httptest.NewRecorder()
    29  	m := newManager()
    30  
    31  	throttlerlogzHandler(response, request, m)
    32  
    33  	if got, want := response.Body.String(), "invalid /throttlerlogz path"; !strings.Contains(got, want) {
    34  		t.Fatalf("/throttlerlogz without the slash does not work (the Go HTTP server does automatically redirect in practice though). got = %v, want = %v", got, want)
    35  	}
    36  }
    37  
    38  func TestThrottlerlogzHandler_NonExistantThrottler(t *testing.T) {
    39  	request, _ := http.NewRequest("GET", "/throttlerlogz/t1", nil)
    40  	response := httptest.NewRecorder()
    41  
    42  	throttlerlogzHandler(response, request, newManager())
    43  
    44  	if got, want := response.Body.String(), `throttler: t1 does not exist`; !strings.Contains(got, want) {
    45  		t.Fatalf("/throttlerlogz page for non-existent t1 should not succeed. got = %v, want = %v", got, want)
    46  	}
    47  }
    48  
    49  func TestThrottlerlogzHandler(t *testing.T) {
    50  	f := &managerTestFixture{}
    51  	if err := f.setUp(); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	defer f.tearDown()
    55  
    56  	testcases := []struct {
    57  		desc string
    58  		r    result
    59  		want string
    60  	}{
    61  		{
    62  			"increased rate",
    63  			resultIncreased,
    64  			`    <tr class="low">
    65        <td>00:00:01</td>
    66        <td>increased</td>
    67        <td>100</td>
    68        <td>100</td>
    69        <td>cell1-0000000101</td>
    70        <td>1s</td>
    71        <td>1.2s</td>
    72        <td>99</td>
    73        <td>good</td>
    74        <td></td>
    75        <td>95</td>
    76        <td>0</td>
    77        <td>I</td>
    78        <td>I</td>
    79        <td>I</td>
    80        <td>n/a</td>
    81        <td>n/a</td>
    82        <td>99</td>
    83        <td>0</td>
    84        <td>0</td>
    85        <td>0</td>
    86        <td>increased the rate</td>
    87      </tr>`,
    88  		},
    89  		{
    90  			"decreased rate",
    91  			resultDecreased,
    92  			`    <tr class="medium">
    93        <td>00:00:05</td>
    94        <td>decreased</td>
    95        <td>200</td>
    96        <td>100</td>
    97        <td>cell1-0000000101</td>
    98        <td>2s</td>
    99        <td>3.8s</td>
   100        <td>200</td>
   101        <td>bad</td>
   102        <td></td>
   103        <td>95</td>
   104        <td>200</td>
   105        <td>I</td>
   106        <td>D</td>
   107        <td>D</td>
   108        <td>1s</td>
   109        <td>3.8s</td>
   110        <td>200</td>
   111        <td>150</td>
   112        <td>10</td>
   113        <td>20</td>
   114        <td>decreased the rate</td>
   115      </tr>`,
   116  		},
   117  		{
   118  			"emergency state decreased the rate",
   119  			resultEmergency,
   120  			`    <tr class="high">
   121        <td>00:00:10</td>
   122        <td>decreased</td>
   123        <td>100</td>
   124        <td>50</td>
   125        <td>cell1-0000000101</td>
   126        <td>23s</td>
   127        <td>5.1s</td>
   128        <td>100</td>
   129        <td>bad</td>
   130        <td></td>
   131        <td>95</td>
   132        <td>100</td>
   133        <td>D</td>
   134        <td>E</td>
   135        <td>E</td>
   136        <td>2s</td>
   137        <td>5.1s</td>
   138        <td>0</td>
   139        <td>0</td>
   140        <td>0</td>
   141        <td>0</td>
   142        <td>emergency state decreased the rate</td>
   143      </tr>`,
   144  		},
   145  	}
   146  
   147  	for _, tc := range testcases {
   148  		request, _ := http.NewRequest("GET", "/throttlerlogz/t1", nil)
   149  		response := httptest.NewRecorder()
   150  
   151  		f.t1.maxReplicationLagModule.results.add(tc.r)
   152  		throttlerlogzHandler(response, request, f.m)
   153  
   154  		got := response.Body.String()
   155  		if !strings.Contains(got, tc.want) {
   156  			t.Fatalf("testcase '%v': result not shown in log. got = %v, want = %v", tc.desc, got, tc.want)
   157  		}
   158  	}
   159  }