test(infrastructure): Configure pgxpool with max_conns=1
Fix database connection exhaustion in tests by setting max_conns=1 when creating pgxpool via pgxpool.ParseConfig(). - Update setupTestServer() in test_helpers.go - Update setupSyncTestDB() in sync_integration_test.go This reduces per-test connection usage from 4 to 1, keeping total connections well under PostgreSQL's default max_connections=100. 78 tests × 1 connection = 78 connections (down from 312 potential) Fixes test failures: "FATAL: sorry, too many clients already" See PROJECT_GUIDELINES.md Testing section for details.
This commit is contained in:
@@ -13,9 +13,11 @@ import (
|
||||
|
||||
// TestCollectionsBulkOperations tests bulk collection operations
|
||||
func TestCollectionsBulkOperations(t *testing.T) {
|
||||
t.Run("BulkAddBooks_WithoutAuth", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
setup := setupTestServer(t)
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
client := &http.Client{}
|
||||
|
||||
t.Run("BulkAddBooks_WithoutAuth", func(t *testing.T) {
|
||||
req := map[string]interface{}{
|
||||
"operations": []map[string]interface{}{
|
||||
{
|
||||
@@ -29,7 +31,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(body))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -38,10 +39,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_EmptyOperations", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
req := map[string]interface{}{
|
||||
"operations": []map[string]interface{}{},
|
||||
}
|
||||
@@ -51,7 +48,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -60,9 +56,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_InvalidCollectionID", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
bookID := createTestMediaItemID(t, setup.Server, token)
|
||||
|
||||
req := map[string]interface{}{
|
||||
@@ -79,7 +72,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -102,10 +94,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_InvalidBookID", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Create a collection first
|
||||
collectionReq := map[string]interface{}{
|
||||
"name": "Test Collection",
|
||||
@@ -117,7 +105,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(collectionHTTP)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -156,10 +143,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_SingleOperation", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Create a collection
|
||||
collectionReq := map[string]interface{}{
|
||||
"name": "Test Collection",
|
||||
@@ -171,7 +154,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(collectionHTTP)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -218,10 +200,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_MultipleBooksSingleCollection", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Create a collection
|
||||
collectionReq := map[string]interface{}{
|
||||
"name": "Test Collection",
|
||||
@@ -233,7 +211,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(collectionHTTP)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -276,10 +253,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_MultipleCollections", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Create multiple collections
|
||||
collectionReq := map[string]interface{}{
|
||||
"name": "Test Collection 1",
|
||||
@@ -291,7 +264,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(collectionHTTP)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -355,10 +327,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_DuplicateBooks", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Create a collection
|
||||
collectionReq := map[string]interface{}{
|
||||
"name": "Test Collection",
|
||||
@@ -370,7 +338,6 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
collectionHTTP.Header.Set("Content-Type", "application/json")
|
||||
collectionHTTP.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(collectionHTTP)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
@@ -401,7 +368,7 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Try to add the same book again - create new request with fresh body
|
||||
// Try to add same book again - create new request with fresh body
|
||||
addBody2, _ := json.Marshal(addReq)
|
||||
addHTTP2, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer(addBody2))
|
||||
addHTTP2.Header.Set("Content-Type", "application/json")
|
||||
@@ -416,16 +383,11 @@ func TestCollectionsBulkOperations(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("BulkAddBooks_InvalidRequestBody", func(t *testing.T) {
|
||||
setup := setupTestServer(t)
|
||||
|
||||
token := loginTestUser(t, setup.Server, setup.DB)
|
||||
|
||||
// Send invalid JSON
|
||||
httpReq, _ := http.NewRequest("POST", setup.Server.URL+"/api/collections/bulk-add-books", bytes.NewBuffer([]byte("invalid json")))
|
||||
httpReq.Header.Set("Content-Type", "application/json")
|
||||
httpReq.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(httpReq)
|
||||
require.NoError(t, err)
|
||||
defer resp.Body.Close()
|
||||
|
||||
Reference in New Issue
Block a user