Skip to content

Commit 4e2eed2

Browse files
author
James Cor
committed
handler test
1 parent 34c470d commit 4e2eed2

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

server/handler_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,92 @@ func TestHandlerKill(t *testing.T) {
742742
require.Len(handler.sm.sessions, 1)
743743
}
744744

745+
func TestHandlerKillQuery(t *testing.T) {
746+
require := require.New(t)
747+
e, pro := setupMemDB(require)
748+
dbFunc := pro.Database
749+
750+
handler := &Handler{
751+
e: e,
752+
sm: NewSessionManager(
753+
func(ctx context.Context, conn *mysql.Conn, addr string) (sql.Session, error) {
754+
return sql.NewBaseSessionWithClientServer(addr, sql.Client{Capabilities: conn.Capabilities}, conn.ConnectionID), nil
755+
},
756+
sql.NoopTracer,
757+
dbFunc,
758+
e.MemoryManager,
759+
e.ProcessList,
760+
"foo",
761+
),
762+
}
763+
764+
var err error
765+
conn1 := newConn(1)
766+
handler.NewConnection(conn1)
767+
768+
conn2 := newConn(2)
769+
handler.NewConnection(conn2)
770+
771+
require.Len(handler.sm.connections, 2)
772+
require.Len(handler.sm.sessions, 0)
773+
774+
handler.ComInitDB(conn1, "test")
775+
err = handler.sm.SetDB(conn1, "test")
776+
require.NoError(err)
777+
778+
err = handler.sm.SetDB(conn2, "test")
779+
require.NoError(err)
780+
781+
require.False(conn1.Conn.(*mockConn).closed)
782+
require.False(conn2.Conn.(*mockConn).closed)
783+
require.Len(handler.sm.connections, 2)
784+
require.Len(handler.sm.sessions, 2)
785+
786+
787+
sleepQuery := "SELECT SLEEP(1000)"
788+
go func() {
789+
err = handler.ComQuery(context.Background(), conn1, sleepQuery, func(res *sqltypes.Result, more bool) error {
790+
return nil
791+
})
792+
require.Error(err)
793+
}()
794+
795+
time.Sleep(100 * time.Millisecond)
796+
var sleepQueryID string
797+
err = handler.ComQuery(context.Background(), conn2, "SHOW PROCESSLIST", func(res *sqltypes.Result, more bool) error {
798+
// 1, , , test, Query, 0, ... , SELECT SLEEP(1000)
799+
// 2, , , test, Query, 0, running, SHOW PROCESSLIST
800+
require.Equal(2, len(res.Rows))
801+
sleepQueryID = res.Rows[0][0].ToString()
802+
require.Equal("Query", res.Rows[0][4].ToString())
803+
require.Equal(sleepQuery, res.Rows[0][7].ToString())
804+
return nil
805+
})
806+
require.NoError(err)
807+
808+
time.Sleep(100 * time.Millisecond)
809+
err = handler.ComQuery(context.Background(), conn2, "KILL QUERY " + sleepQueryID, func(res *sqltypes.Result, more bool) error {
810+
return nil
811+
})
812+
require.NoError(err)
813+
814+
time.Sleep(100 * time.Millisecond)
815+
err = handler.ComQuery(context.Background(), conn2, "SHOW PROCESSLIST", func(res *sqltypes.Result, more bool) error {
816+
// 1, , , test, Sleep, 0, ,
817+
// 2, , , test, Query, 0, running, SHOW PROCESSLIST
818+
require.Equal(2, len(res.Rows))
819+
require.Equal("Sleep", res.Rows[0][4].ToString())
820+
require.Equal("", res.Rows[0][7].ToString())
821+
return nil
822+
})
823+
require.NoError(err)
824+
825+
require.False(conn1.Conn.(*mockConn).closed)
826+
require.False(conn2.Conn.(*mockConn).closed)
827+
require.Len(handler.sm.connections, 2)
828+
require.Len(handler.sm.sessions, 2)
829+
}
830+
745831
func TestSchemaToFields(t *testing.T) {
746832
require := require.New(t)
747833

0 commit comments

Comments
 (0)