vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/livequeryz_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 tabletserver 18 19 import ( 20 "net/http" 21 "net/http/httptest" 22 "testing" 23 24 "context" 25 ) 26 27 func TestLiveQueryzHandlerJSON(t *testing.T) { 28 resp := httptest.NewRecorder() 29 req, _ := http.NewRequest("GET", "/livequeryz/?format=json", nil) 30 31 queryList := NewQueryList("test") 32 queryList.Add(NewQueryDetail(context.Background(), &testConn{id: 1})) 33 queryList.Add(NewQueryDetail(context.Background(), &testConn{id: 2})) 34 35 livequeryzHandler([]*QueryList{queryList}, resp, req) 36 } 37 38 func TestLiveQueryzHandlerHTTP(t *testing.T) { 39 resp := httptest.NewRecorder() 40 req, _ := http.NewRequest("GET", "/livequeryz/", nil) 41 42 queryList := NewQueryList("test") 43 queryList.Add(NewQueryDetail(context.Background(), &testConn{id: 1})) 44 queryList.Add(NewQueryDetail(context.Background(), &testConn{id: 2})) 45 46 livequeryzHandler([]*QueryList{queryList}, resp, req) 47 } 48 49 func TestLiveQueryzHandlerHTTPFailedInvalidForm(t *testing.T) { 50 resp := httptest.NewRecorder() 51 req, _ := http.NewRequest("POST", "/livequeryz/", nil) 52 53 livequeryzHandler([]*QueryList{NewQueryList("test")}, resp, req) 54 if resp.Code != http.StatusInternalServerError { 55 t.Fatalf("http call should fail and return code: %d, but got: %d", 56 http.StatusInternalServerError, resp.Code) 57 } 58 } 59 60 func TestLiveQueryzHandlerTerminateConn(t *testing.T) { 61 resp := httptest.NewRecorder() 62 req, _ := http.NewRequest("GET", "/livequeryz//terminate?connID=1", nil) 63 64 queryList := NewQueryList("test") 65 testConn := &testConn{id: 1} 66 queryList.Add(NewQueryDetail(context.Background(), testConn)) 67 if testConn.IsKilled() { 68 t.Fatalf("conn should still be alive") 69 } 70 livequeryzTerminateHandler([]*QueryList{queryList}, resp, req) 71 if !testConn.IsKilled() { 72 t.Fatalf("conn should be killed") 73 } 74 } 75 76 func TestLiveQueryzHandlerTerminateFailedInvalidConnID(t *testing.T) { 77 resp := httptest.NewRecorder() 78 req, _ := http.NewRequest("GET", "/livequeryz//terminate?connID=invalid", nil) 79 80 livequeryzTerminateHandler([]*QueryList{NewQueryList("test")}, resp, req) 81 if resp.Code != http.StatusInternalServerError { 82 t.Fatalf("http call should fail and return code: %d, but got: %d", 83 http.StatusInternalServerError, resp.Code) 84 } 85 } 86 87 func TestLiveQueryzHandlerTerminateFailedInvalidForm(t *testing.T) { 88 resp := httptest.NewRecorder() 89 req, _ := http.NewRequest("POST", "/livequeryz//terminate?inva+lid=2", nil) 90 91 livequeryzTerminateHandler([]*QueryList{NewQueryList("test")}, resp, req) 92 if resp.Code != http.StatusInternalServerError { 93 t.Fatalf("http call should fail and return code: %d, but got: %d", 94 http.StatusInternalServerError, resp.Code) 95 } 96 }