From 65748093bdedbcf18b65d8efda782f8c04c5dc39 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 4 Jun 2018 18:15:45 +0200 Subject: [PATCH] Fixes #2618 - ProcessorUtilsTest fragile. Introduced ProcessorUtils.setAvailableProcessor(int) to allow usage in embedded code without resorting to system properties. Refactored the static initializer to be testable. Signed-off-by: Simone Bordet --- .../eclipse/jetty/util/ProcessorUtils.java | 36 ++++++++------ .../jetty/util/ProcessorUtilsTest.java | 49 ++++++++++++++----- 2 files changed, 59 insertions(+), 26 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/ProcessorUtils.java b/jetty-util/src/main/java/org/eclipse/jetty/util/ProcessorUtils.java index df0d8e3f1a6..8fcedda39cd 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/ProcessorUtils.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/ProcessorUtils.java @@ -19,40 +19,48 @@ package org.eclipse.jetty.util; /** - * ProcessorUtils provides access to runtime info about processors, that may be - * overridden by system properties of environment variables. This can be useful - * in virtualised environments where the runtime may miss report the available - * resources. + *

ProcessorUtils provides access to runtime info about processors, that may be + * overridden by system properties or environment variables.

+ *

This can be useful in virtualized environments where the runtime may miss + * report the available resources.

*/ public class ProcessorUtils { public static final String AVAILABLE_PROCESSORS = "JETTY_AVAILABLE_PROCESSORS"; - private static int __availableProcessors = Runtime.getRuntime().availableProcessors(); + private static int __availableProcessors = init(); - static + static int init() { - String avlProcEnv = System.getProperty(AVAILABLE_PROCESSORS,System.getenv(AVAILABLE_PROCESSORS)); - if (avlProcEnv != null) + String processors = System.getProperty(AVAILABLE_PROCESSORS, System.getenv(AVAILABLE_PROCESSORS)); + if (processors != null) { try { - __availableProcessors = Integer.parseInt( avlProcEnv ); + return Integer.parseInt( processors ); } - catch ( NumberFormatException e ) + catch (NumberFormatException ignored) { - // ignore } } + return Runtime.getRuntime().availableProcessors(); } /** - * Obtain the number of available processors, from System Property "JETTY_AVAILABLE_PROCESSORS", - * or if not set then environment variable "JETTY_AVAILABLE_PROCESSORS" or if not set then - * {@link Runtime#availableProcessors()}. + * Returns the number of available processors, from System Property "JETTY_AVAILABLE_PROCESSORS", + * or if not set then from environment variable "JETTY_AVAILABLE_PROCESSORS" or if not set then + * from {@link Runtime#availableProcessors()}. + * * @return the number of processors */ public static int availableProcessors() { return __availableProcessors; } + + public static void setAvailableProcessors(int processors) + { + if (processors < 1) + throw new IllegalArgumentException("Invalid number of processors: " + processors); + __availableProcessors = processors; + } } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/ProcessorUtilsTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/ProcessorUtilsTest.java index a9b17822be9..cbb8dc45dfb 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/ProcessorUtilsTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/ProcessorUtilsTest.java @@ -19,23 +19,48 @@ package org.eclipse.jetty.util; import org.junit.Assert; -import org.junit.BeforeClass; import org.junit.Test; -/** - * we cannot really add env var in a unit test... so only test we get default value - */ public class ProcessorUtilsTest { - @BeforeClass - public static void beforeClass() - { - System.setProperty("JETTY_AVAILABLE_PROCESSORS","42"); - } - @Test - public void getPropertyValue() + public void testSystemProperty() { - Assert.assertEquals(42, ProcessorUtils.availableProcessors()); + // Classloading will trigger the static initializer. + int original = ProcessorUtils.availableProcessors(); + + // Verify that the static initializer logic is correct. + System.setProperty(ProcessorUtils.AVAILABLE_PROCESSORS, "42"); + int processors = ProcessorUtils.init(); + Assert.assertEquals(42, processors); + + // Make sure the original value is preserved. + Assert.assertEquals(original, ProcessorUtils.availableProcessors()); + } + + @Test + public void testSetter() + { + // Classloading will trigger the static initializer. + int original = ProcessorUtils.availableProcessors(); + try + { + try + { + ProcessorUtils.setAvailableProcessors(0); + Assert.fail(); + } + catch (IllegalArgumentException expected) + { + } + + int processors = 42; + ProcessorUtils.setAvailableProcessors(processors); + Assert.assertEquals(processors, ProcessorUtils.availableProcessors()); + } + finally + { + ProcessorUtils.setAvailableProcessors(original); + } } }