From f8de5565f424c74fc2d2f2e7f492668dfdf0cf8d Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Tue, 11 Aug 2026 10:46:44 -0600 Subject: [PATCH] Testing: cap spinner threads in concurrent socket close tests --- .../provider/jsse/test/WolfSSLSocketTest.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java b/src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java index 564643d..1fb21ae 100644 --- a/src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java +++ b/src/test/com/wolfssl/provider/jsse/test/WolfSSLSocketTest.java @@ -3591,14 +3591,18 @@ public class WolfSSLSocketTest { ExecutorService es = Executors.newCachedThreadPool(); - /* Busy spinners force thread preemption inside write()/close() */ + /* Busy spinners force thread preemption inside write()/close(). + * Capped so load does not grow with machine size. */ final AtomicBoolean spinStop = new AtomicBoolean(false); - int numSpin = Runtime.getRuntime().availableProcessors() * 2; + final long spinDeadline = System.nanoTime() + + TimeUnit.MILLISECONDS.toNanos(budgetMs + 10000L); + int numSpin = Math.min(Runtime.getRuntime().availableProcessors(), 4); for (i = 0; i < numSpin; i++) { Thread spin = new Thread(new Runnable() { @Override public void run() { - while (!spinStop.get()) { + while (!spinStop.get() && + (System.nanoTime() < spinDeadline)) { /* busy loop to create CPU load */ } } @@ -3608,10 +3612,11 @@ public class WolfSSLSocketTest { } try { - long startMs = System.currentTimeMillis(); + long start = System.nanoTime(); + long budget = TimeUnit.MILLISECONDS.toNanos(budgetMs); for (i = 0; (i < maxIterations) && - ((System.currentTimeMillis() - startMs) < budgetMs); + ((System.nanoTime() - start) < budget); i++) { SSLServerSocket ss = null; @@ -3749,14 +3754,18 @@ public class WolfSSLSocketTest { ExecutorService es = Executors.newCachedThreadPool(); - /* Busy spinners force thread preemption inside read()/close() */ + /* Busy spinners force thread preemption inside read()/close(). + * Capped so load does not grow with machine size. */ final AtomicBoolean spinStop = new AtomicBoolean(false); - int numSpin = Runtime.getRuntime().availableProcessors() * 2; + final long spinDeadline = System.nanoTime() + + TimeUnit.MILLISECONDS.toNanos(budgetMs + 10000L); + int numSpin = Math.min(Runtime.getRuntime().availableProcessors(), 4); for (i = 0; i < numSpin; i++) { Thread spin = new Thread(new Runnable() { @Override public void run() { - while (!spinStop.get()) { + while (!spinStop.get() && + (System.nanoTime() < spinDeadline)) { /* busy loop to create CPU load */ } } @@ -3766,10 +3775,11 @@ public class WolfSSLSocketTest { } try { - long startMs = System.currentTimeMillis(); + long start = System.nanoTime(); + long budget = TimeUnit.MILLISECONDS.toNanos(budgetMs); for (i = 0; (i < maxIterations) && - ((System.currentTimeMillis() - startMs) < budgetMs); + ((System.nanoTime() - start) < budget); i++) { SSLServerSocket ss = null;