@@ -1997,3 +1997,66 @@ func (h *testHook) Fire(entry *logrus.Entry) error {
19971997 }
19981998 return nil
19991999}
2000+
2001+ func TestHandlerNewConnectionProcessListInteractions (t * testing.T ) {
2002+ e , pro := setupMemDB (require .New (t ))
2003+ dbFunc := pro .Database
2004+
2005+ handler := & Handler {
2006+ e : e ,
2007+ sm : NewSessionManager (
2008+ sql .NewContext ,
2009+ testSessionBuilder (pro ),
2010+ sql .NoopTracer ,
2011+ dbFunc ,
2012+ sql .NewMemoryManager (nil ),
2013+ sqle .NewProcessList (),
2014+ "foo" ,
2015+ ),
2016+ readTimeout : time .Second ,
2017+ }
2018+
2019+ // Process List starts empty.
2020+ procs := handler .sm .processlist .Processes ()
2021+ assert .Len (t , procs , 0 )
2022+
2023+ // A new connection is in Connect state and shows "unauthenticated user" as the user.
2024+ abortedConn := newConn (1 )
2025+ handler .NewConnection (abortedConn )
2026+ procs = handler .sm .processlist .Processes ()
2027+ if assert .Len (t , procs , 1 ) {
2028+ assert .Equal (t , "unauthenticated user" , procs [0 ].User )
2029+ assert .Equal (t , sql .ProcessCommandConnect , procs [0 ].Command )
2030+ }
2031+
2032+ // The connection being aborted does not effect the process list.
2033+ handler .ConnectionAborted (abortedConn , "" )
2034+ procs = handler .sm .processlist .Processes ()
2035+ assert .Len (t , procs , 1 )
2036+
2037+ // After the ConnectionAborted called, the ConnectionClosed callback does
2038+ // remove the connection from the processlist.
2039+ handler .ConnectionClosed (abortedConn )
2040+ procs = handler .sm .processlist .Processes ()
2041+ assert .Len (t , procs , 0 )
2042+
2043+ // A new connection gets updated with the authenticated user
2044+ // and command Sleep when ConnectionAuthenticated is called.
2045+ authenticatedConn := newConn (2 )
2046+ handler .NewConnection (authenticatedConn )
2047+ authenticatedConn .User = "authenticated_user"
2048+ handler .ConnectionAuthenticated (authenticatedConn )
2049+ procs = handler .sm .processlist .Processes ()
2050+ if assert .Len (t , procs , 1 ) {
2051+ assert .Equal (t , "authenticated_user" , procs [0 ].User )
2052+ assert .Equal (t , sql .ProcessCommandSleep , procs [0 ].Command )
2053+ assert .Equal (t , "" , procs [0 ].Database )
2054+ }
2055+
2056+ // After ComInitDB, the selected database is also reflected.
2057+ handler .ComInitDB (authenticatedConn , "test" )
2058+ procs = handler .sm .processlist .Processes ()
2059+ if assert .Len (t , procs , 1 ) {
2060+ assert .Equal (t , "test" , procs [0 ].Database )
2061+ }
2062+ }
0 commit comments