From 27379c12d3bddfb15c383fe808eb8d966097ca61 Mon Sep 17 00:00:00 2001
From: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Date: Fri, 3 Apr 2026 10:17:21 +0000
Subject: [PATCH v1] meson: adjust test timeout for Valgrind builds

When building with -DUSE_VALGRIND, tests run significantly slower due
to Valgrind's instrumentation overhead, causing the default 1000s timeout
to be exceeded.

This commit detects Valgrind builds using a compiler check, which correctly
handles USE_VALGRIND being passed via -Dc_args, CPPFLAGS or CFLAGS and increases
the test timeout to 10000s in that case.

No changes are needed for autoconf as it has no test timeout mechanism.

Author: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>
Discussion: https://postgr.es/m/
---
 meson.build                    | 10 ++++++++--
 src/test/isolation/meson.build |  8 +++++++-
 src/test/regress/meson.build   |  7 ++++++-
 3 files changed, 21 insertions(+), 4 deletions(-)
  29.3% src/test/isolation/
  29.2% src/test/regress/

diff --git a/meson.build b/meson.build
index 6bc74c2ba79..7d376495e20 100644
--- a/meson.build
+++ b/meson.build
@@ -3888,6 +3888,12 @@ install_suites = []
 
 testwrap = files('src/tools/testwrap')
 
+# Detect if built with Valgrind support to adjust test timeouts
+is_valgrind_build = cc.compiles('''
+#ifndef USE_VALGRIND
+choke me
+#endif''', name: 'USE_VALGRIND check', args: test_c_args)
+
 foreach test_dir : tests
   testwrap_base = [
     testwrap,
@@ -3955,7 +3961,7 @@ foreach test_dir : tests
       test_kwargs = {
         'protocol': 'tap',
         'priority': 10,
-        'timeout': 1000,
+        'timeout': is_valgrind_build ? 10000 : 1000,
         'depends': test_deps + t.get('deps', []),
         'env': env,
       } + t.get('test_kwargs', {})
@@ -4028,7 +4034,7 @@ foreach test_dir : tests
       test_kwargs = {
         'protocol': 'tap',
         'suite': test_group,
-        'timeout': 1000,
+        'timeout': is_valgrind_build ? 10000 : 1000,
         'depends': test_deps + t.get('deps', []),
         'env': env,
       } + t.get('test_kwargs', {})
diff --git a/src/test/isolation/meson.build b/src/test/isolation/meson.build
index c55b8d71848..87786dacf81 100644
--- a/src/test/isolation/meson.build
+++ b/src/test/isolation/meson.build
@@ -59,6 +59,12 @@ isolationtester = executable('isolationtester',
 )
 bin_targets += isolationtester
 
+# Detect if built with Valgrind support to adjust test timeouts
+is_valgrind_build = cc.compiles('''
+#ifndef USE_VALGRIND
+choke me
+#endif''', name: 'USE_VALGRIND check')
+
 tests += {
   'name': 'isolation',
   'sd': meson.current_source_dir(),
@@ -67,7 +73,7 @@ tests += {
     'schedule': files('isolation_schedule'),
     'test_kwargs': {
       'priority': 40,
-      'timeout': 1000,
+      'timeout': is_valgrind_build ? 10000 : 1000,
     },
     'dbname': 'isolation_regression',
   },
diff --git a/src/test/regress/meson.build b/src/test/regress/meson.build
index a5f2222e83a..196cf263c6d 100644
--- a/src/test/regress/meson.build
+++ b/src/test/regress/meson.build
@@ -43,6 +43,11 @@ regress_module = shared_module('regress',
 )
 test_install_libs += regress_module
 
+# Detect if built with Valgrind support to adjust test timeouts
+is_valgrind_build = cc.compiles('''
+#ifndef USE_VALGRIND
+choke me
+#endif''', name: 'USE_VALGRIND check')
 
 tests += {
   'name': 'regress',
@@ -52,7 +57,7 @@ tests += {
     'schedule': files('parallel_schedule'),
     'test_kwargs': {
       'priority': 50,
-      'timeout': 1000,
+      'timeout': is_valgrind_build ? 10000 : 1000,
     },
     'dbname': 'regression',
   },
-- 
2.34.1

