vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletserver/query_list_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  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  type testConn struct {
    28  	id     int64
    29  	query  string
    30  	killed bool
    31  }
    32  
    33  func (tc *testConn) Current() string { return tc.query }
    34  
    35  func (tc *testConn) ID() int64 { return tc.id }
    36  
    37  func (tc *testConn) Kill(string, time.Duration) error {
    38  	tc.killed = true
    39  	return nil
    40  }
    41  
    42  func (tc *testConn) IsKilled() bool {
    43  	return tc.killed
    44  }
    45  
    46  func TestQueryList(t *testing.T) {
    47  	ql := NewQueryList("test")
    48  	connID := int64(1)
    49  	qd := NewQueryDetail(context.Background(), &testConn{id: connID})
    50  	ql.Add(qd)
    51  
    52  	if qd1, ok := ql.queryDetails[connID]; !ok || qd1[0].connID != connID {
    53  		t.Errorf("failed to add to QueryList")
    54  	}
    55  
    56  	conn2ID := int64(2)
    57  	qd2 := NewQueryDetail(context.Background(), &testConn{id: conn2ID})
    58  	ql.Add(qd2)
    59  
    60  	rows := ql.AppendQueryzRows(nil)
    61  	if len(rows) != 2 || rows[0].ConnID != 1 || rows[1].ConnID != 2 {
    62  		t.Errorf("wrong rows returned %v", rows)
    63  	}
    64  
    65  	ql.Remove(qd)
    66  	if _, ok := ql.queryDetails[connID]; ok {
    67  		t.Errorf("failed to remove from QueryList")
    68  	}
    69  }
    70  
    71  func TestQueryListChangeConnIDInMiddle(t *testing.T) {
    72  	ql := NewQueryList("test")
    73  	connID := int64(1)
    74  	qd1 := NewQueryDetail(context.Background(), &testConn{id: connID})
    75  	ql.Add(qd1)
    76  
    77  	conn := &testConn{id: connID}
    78  	qd2 := NewQueryDetail(context.Background(), conn)
    79  	ql.Add(qd2)
    80  
    81  	require.Len(t, ql.queryDetails[1], 2)
    82  
    83  	// change the connID in the middle
    84  	conn.id = 2
    85  
    86  	// remove the same object.
    87  	ql.Remove(qd2)
    88  
    89  	require.Len(t, ql.queryDetails[1], 1)
    90  	require.Equal(t, qd1, ql.queryDetails[1][0])
    91  	require.NotEqual(t, qd2, ql.queryDetails[1][0])
    92  }