Skip to content

Commit

Permalink
test for leaked connections (connection not closed when database name…
Browse files Browse the repository at this point in the history
… is incorrect #173
  • Loading branch information
parMaster committed Oct 11, 2024
1 parent 3b47a4f commit 5b9d44f
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,52 @@ func TestBadCredentials(t *testing.T) {
_ = testConnectionBad(t, params.URL().String())
}

func TestLeakedConnections(t *testing.T) {
goodParams := testConnParams(t)
badParams := testConnParams(t)
badParams.Database = "unknown_db"

// Connecting with good credentials should not fail
goodConn, err := sql.Open("mssql", goodParams.URL().String())
if err != nil {
t.Fatal("Open connection failed:", err.Error())
}
err = goodConn.Ping()
if err != nil {
t.Fatal("Ping with good credentials should not fail, but got error:", err.Error())
}
// Remember the number of open connections, excluding the current one
var openConnections int
err = goodConn.QueryRow("SELECT COUNT(*) AS openConnections FROM sys.dm_exec_connections WHERE session_id!=@@SPID").Scan(&openConnections)
if err != nil {
t.Fatal("cannot scan value", err)
}

// Open 10 connections to the unknown database, all should be closed immediately
for i := 0; i < 10; i++ {
conn, err := sql.Open("mssql", badParams.URL().String())
if err != nil {
// should not fail here
t.Fatal("sql.Open failed:", err.Error())
}
err = conn.Ping()
if err == nil {
t.Fatalf("Pinging %s should fail, but it succeeded", badParams.Database)
}
conn.Close() // force close the connection
}

// Check if the number of open connections is the same as before
var newOpenConnections int
err = goodConn.QueryRow("SELECT COUNT(*) AS openConnections FROM sys.dm_exec_connections WHERE session_id!=@@SPID").Scan(&newOpenConnections)
if err != nil {
t.Fatal("cannot scan value", err)
}
if openConnections != newOpenConnections {
t.Fatalf("Number of open connections should be the same as before, %d leaked connections found", newOpenConnections-openConnections)
}
}

func TestBadHost(t *testing.T) {
params := testConnParams(t)
params.Host = "badhost"
Expand Down

0 comments on commit 5b9d44f

Please sign in to comment.