File 3841-EUNIT-feature-add-randomDelay-macro-for-race-conditi.patch of Package erlang

From 36a7cfc9cf10d358eeb491e9034b9e61c5cee527 Mon Sep 17 00:00:00 2001
From: Wade Mealing <wmealing@gmail.com>
Date: Fri, 30 Jan 2026 22:05:11 +1000
Subject: [PATCH 1/3] EUNIT: feature - add ?randomDelay macro for race
 condition testing

Introduces a new eunit macro to inject non-deterministic delays during
test execution. This assists in uncovering timing-dependent bugs and
race conditions by artificially shifting process execution order.

- Defined as a no-op when NODEBUG is set.
- Takes Probability (0.0-1.0), Min, and Max duration in seconds.
- Includes documentation in the instrumentation section.

This was discussed with Richard Carlsson's and submitted here
by request:

	https://github.com/richcarl/eunit/issues/23
---
 lib/eunit/doc/guides/chapter.md | 17 +++++++++++++++++
 lib/eunit/include/eunit.hrl     | 19 +++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/lib/eunit/doc/guides/chapter.md b/lib/eunit/doc/guides/chapter.md
index 97af1152c7..ca611692aa 100644
--- a/lib/eunit/doc/guides/chapter.md
+++ b/lib/eunit/doc/guides/chapter.md
@@ -486,6 +486,7 @@ compiled with testing enabled or disabled.
 - [Assert macros](chapter.md#Assert_macros)
 - [Macros for checking output](chapter.md#Macros_for_checking_output)
 - [Macros for running external commands](chapter.md#Macros_for_running_external_commands)
+- [Macros for instrumentation](chapter.md#Macros_for_instrumentation)
 - [Debugging macros](chapter.md#Debugging_macros)
 
 [](){: #Basic_macros }
@@ -754,6 +755,22 @@ compiled with testing enabled.
       ...}
   ```
 
+[](){: #Macros_for_instrumentation }
+
+### Macros for instrumentation
+
+To help with instrumenting, EUnit defines a macro that can introduce
+delays into the runtime.  This can help uncover subtle timing bugs
+that may not be evident when timing is perfect.
+
+If the macro `NODEBUG` is defined before the EUnit header file is included,
+these macros have no effect; see
+[Compilation control macros](chapter.md#Compilation_control_macros) for details.
+
+- **`randomDelay(Probability, Min, Max)`** This macro is used to cause
+non-deterministic latency into execution, which is useful for uncovering 
+race conditions and timing-dependent bugs during testing.
+
 [](){: #Debugging_macros }
 
 ### Debugging macros
diff --git a/lib/eunit/include/eunit.hrl b/lib/eunit/include/eunit.hrl
index 7ec42981de..3733a104a0 100644
--- a/lib/eunit/include/eunit.hrl
+++ b/lib/eunit/include/eunit.hrl
@@ -235,6 +235,25 @@
 -define(_assertCmd(Cmd), ?_test(?assertCmd(Cmd))).
 -define(_assertCmdOutput(T, Cmd), ?_test(?assertCmdOutput(T, Cmd))).
 
+%% Macros to assist in finding race / time dependant bugs
+%% in exercised code.
+
+-ifdef(NODEBUG).
+-define(randomDelay(Prob, MinSec, MaxSec),  ok).
+-else.
+-define(randomDelay(Prob, MinSec, MaxSec),
+        (fun() ->
+            case rand:uniform() < (Prob) of
+                true ->
+                    %% Convert seconds to milliseconds for timer:sleep
+                    Delay = (MinSec) +   () * ((MaxSec) - (MinSec)),
+                    timer:sleep(round(Delay * 1000));
+                false ->
+                    ok
+            end
+        end)()).
+-endif.
+
 %% Macros to simplify debugging (in particular, they work even when the
 %% standard output is being redirected by EUnit while running tests)
 
-- 
2.51.0

openSUSE Build Service is sponsored by