-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from hellofresh/hotfix/my-pg-connections-leak
PT-4880 Fixed MySQL and PG connections leak
- Loading branch information
Showing
13 changed files
with
205 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ services: | |
|
||
go: | ||
- "1.11" | ||
- "1.12" | ||
- "stable" | ||
|
||
before_install: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,68 @@ | ||
package mysql | ||
|
||
import ( | ||
"database/sql" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const mysqlDSNEnv = "HEALTH_GO_MS_DSN" | ||
|
||
func TestNew(t *testing.T) { | ||
if os.Getenv(mysqlDSNEnv) == "" { | ||
t.SkipNow() | ||
func getDSN(t *testing.T) string { | ||
if mysqlDSN, ok := os.LookupEnv(mysqlDSNEnv); ok { | ||
return mysqlDSN | ||
} | ||
|
||
t.Fatalf("required env variable missing: %s", mysqlDSNEnv) | ||
return "" | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
check := New(Config{ | ||
DSN: getDSN(t), | ||
}) | ||
|
||
err := check() | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestEnsureConnectionIsClosed(t *testing.T) { | ||
mysqlDSN := getDSN(t) | ||
|
||
db, err := sql.Open("mysql", mysqlDSN) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
err := db.Close() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
var ( | ||
varName string | ||
initialConnections int | ||
) | ||
row := db.QueryRow(`SHOW STATUS WHERE variable_name = 'Threads_connected'`) | ||
err = row.Scan(&varName, &initialConnections) | ||
require.NoError(t, err) | ||
|
||
check := New(Config{ | ||
DSN: os.Getenv(mysqlDSNEnv), | ||
DSN: mysqlDSN, | ||
}) | ||
|
||
if err := check(); err != nil { | ||
t.Fatalf("MySQL check failed: %s", err.Error()) | ||
for i := 0; i < 10; i++ { | ||
err := check() | ||
assert.NoError(t, err) | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
var currentConnections int | ||
row = db.QueryRow(`SHOW STATUS WHERE variable_name = 'Threads_connected'`) | ||
err = row.Scan(&varName, ¤tConnections) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, initialConnections, currentConnections) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,66 @@ | ||
package postgres | ||
|
||
import ( | ||
"database/sql" | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
_ "github.com/lib/pq" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
const pgDSNEnv = "HEALTH_GO_PG_DSN" | ||
|
||
func TestNew(t *testing.T) { | ||
if os.Getenv(pgDSNEnv) == "" { | ||
t.SkipNow() | ||
func getDSN(t *testing.T) string { | ||
if pgDSN, ok := os.LookupEnv(pgDSNEnv); ok { | ||
return pgDSN | ||
} | ||
|
||
t.Fatalf("required env variable missing: %s", pgDSNEnv) | ||
return "" | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
check := New(Config{ | ||
DSN: getDSN(t), | ||
}) | ||
|
||
err := check() | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestEnsureConnectionIsClosed(t *testing.T) { | ||
pgDSN := getDSN(t) | ||
|
||
db, err := sql.Open("postgres", pgDSN) | ||
require.NoError(t, err) | ||
|
||
defer func() { | ||
err := db.Close() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
var initialConnections int | ||
row := db.QueryRow(`SELECT sum(numbackends) FROM pg_stat_database`) | ||
err = row.Scan(&initialConnections) | ||
require.NoError(t, err) | ||
|
||
check := New(Config{ | ||
DSN: os.Getenv(pgDSNEnv), | ||
DSN: pgDSN, | ||
}) | ||
|
||
if err := check(); err != nil { | ||
t.Fatalf("PostgreSQL check failed: %s", err.Error()) | ||
for i := 0; i < 10; i++ { | ||
err := check() | ||
assert.NoError(t, err) | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
|
||
var currentConnections int | ||
row = db.QueryRow(`SELECT sum(numbackends) FROM pg_stat_database`) | ||
err = row.Scan(¤tConnections) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, initialConnections, currentConnections) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.