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 <simone.bordet@gmail.com>
This commit is contained in:
parent
77df9e1d87
commit
65748093bd
|
@ -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.
|
||||
* <p>ProcessorUtils provides access to runtime info about processors, that may be
|
||||
* overridden by system properties or environment variables.</p>
|
||||
* <p>This can be useful in virtualized environments where the runtime may miss
|
||||
* report the available resources.</p>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue