Skip to content

Commit 53a3390

Browse files
muhmelaoneo
andauthored
[cypress] PostgreSQL Connection pool for System Tests (joomla#43924)
* Use Connection pool for PostgreSQL in System Tests Sometimes single specs in the System Tests fail with: CypressError: `cy.task('queryDB')` failed with the following error: > duplicate key value violates unique constraint "cpostgresmax_users_pkey" The error happens on different specs in System Tests especially running on drone. It could be reproduced in running the System Tests tree times (first with installation, 2nd and 3rd run w/o): npx cypress run npx cypress run --spec 'tests/System/integration/administrator/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/site/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/api/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/plugins/**/*.cy.{js,jsx,ts,tsx}' npx cypress run --spec 'tests/System/integration/administrator/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/site/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/api/**/*.cy.{js,jsx,ts,tsx},tests/System/integration/plugins/**/*.cy.{js,jsx,ts,tsx}' Checking the session usage in pgAdmin shows the maximum number of 100 configured sessions appears to have been reached. Therefore the usage of postgres connection pool is implemented. The session usage is reduced to max 12 and the error could no more reproduced. * Simplify implementation - Delete function as only called once - Renamed to postgresConnectionPool Contributed by @laoneo --------- Co-authored-by: Allon Moritz <[email protected]>
1 parent e491457 commit 53a3390

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

tests/System/plugins/db.mjs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import postgres from 'postgres';
44
// Items cache which are added by an insert statement
55
let insertedItems = [];
66

7+
// Use of the PostgreSQL connection pool to limit the number of sessions, see
8+
// https://github.com/porsager/postgres?tab=readme-ov-file#connection-details
9+
let postgresConnectionPool = null;
10+
711
/**
812
* Does run the given query against the database from the configuration. It caches all inserted items.
913
*
@@ -29,17 +33,20 @@ function queryTestDB(joomlaQuery, config) {
2933
insertedItems.push(insertItem);
3034
}
3135

32-
// Check if the DB is from postgres
36+
// Do we use PostgreSQL?
3337
if (config.env.db_type === 'pgsql' || config.env.db_type === 'PostgreSQL (PDO)') {
34-
const connection = postgres({
35-
host: config.env.db_host,
36-
port: config.env.db_port,
37-
database: config.env.db_name,
38-
username: config.env.db_user,
39-
password: config.env.db_password,
40-
idle_timeout: 1,
41-
max_lifetime: 1,
42-
});
38+
39+
if (postgresConnectionPool === null) {
40+
// Initialisation on the first call
41+
postgresConnectionPool = postgres({
42+
host: config.env.db_host,
43+
port: config.env.db_port,
44+
database: config.env.db_name,
45+
username: config.env.db_user,
46+
password: config.env.db_password,
47+
max: 10, // Use only this (unchanged default) maximum number of connections in the pool
48+
});
49+
}
4350

4451
// Postgres delivers the data direct as result of the insert query
4552
if (insertItem) {
@@ -49,7 +56,7 @@ function queryTestDB(joomlaQuery, config) {
4956
// Postgres needs double quotes
5057
query = query.replaceAll('`', '"');
5158

52-
return connection.unsafe(query).then((result) => {
59+
return postgresConnectionPool.unsafe(query).then((result) => {
5360
// Select query should always return an array
5461
if (query.indexOf('SELECT') === 0 && !Array.isArray(result)) {
5562
return [result];
@@ -64,12 +71,12 @@ function queryTestDB(joomlaQuery, config) {
6471
insertItem.rows.push(result[0].id);
6572
}
6673

67-
// Normalize the object
74+
// Normalize the object and return from PostgreSQL
6875
return { insertId: result[0].id };
6976
});
7077
}
7178

72-
// Return a promise which runs the query
79+
// Return a promise which runs the query for MariaDB / MySQL
7380
return new Promise((resolve, reject) => {
7481
// Create the connection and connect
7582
const connection = mysql.createConnection({
@@ -94,7 +101,7 @@ function queryTestDB(joomlaQuery, config) {
94101
insertItem.rows.push(results.insertId);
95102
}
96103

97-
// Resolve the result
104+
// Resolve the result from MariaDB / MySQL
98105
return resolve(results);
99106
});
100107
});

0 commit comments

Comments
 (0)