|
| 1 | +/** |
| 2 | + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General |
| 3 | + * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any |
| 4 | + * later version. |
| 5 | + * |
| 6 | + * <p>This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 7 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 8 | + * |
| 9 | + * <p>You should have received a copy of the GNU Lesser General Public License along with this program. If not, see |
| 10 | + * <http://www.gnu.org/licenses/>. |
| 11 | + */ |
| 12 | +package org.geowebcache.seed; |
| 13 | + |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | + |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Rule; |
| 18 | +import org.junit.Test; |
| 19 | +import org.junit.contrib.java.lang.system.EnvironmentVariables; |
| 20 | +import org.springframework.context.ApplicationContext; |
| 21 | +import org.springframework.context.support.ClassPathXmlApplicationContext; |
| 22 | + |
| 23 | +/** |
| 24 | + * Test class to verify that the SeederThreadPoolExecutor configuration properly reads environment variables for core |
| 25 | + * pool size and maximum pool size. |
| 26 | + */ |
| 27 | +public class SeederThreadPoolExecutorConfigurationTest { |
| 28 | + |
| 29 | + /** Allows to set environment variables for each individual test */ |
| 30 | + @Rule |
| 31 | + public final EnvironmentVariables environmentVariables = new EnvironmentVariables(); |
| 32 | + |
| 33 | + private ApplicationContext applicationContext; |
| 34 | + |
| 35 | + @After |
| 36 | + public void tearDown() { |
| 37 | + if (applicationContext != null) { |
| 38 | + ((ClassPathXmlApplicationContext) applicationContext).close(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + /** Test that default values are used when environment variables are not set. */ |
| 43 | + @Test |
| 44 | + public void testDefaultValuesWhenEnvironmentVariablesNotSet() { |
| 45 | + // Clear any existing environment variables |
| 46 | + environmentVariables.clear("GWC_SEEDER_CORE_POOL_SIZE"); |
| 47 | + environmentVariables.clear("GWC_SEEDER_MAX_POOL_SIZE"); |
| 48 | + |
| 49 | + // Load the Spring context with test configuration |
| 50 | + applicationContext = new ClassPathXmlApplicationContext("seeder-thread-pool-test-context.xml"); |
| 51 | + |
| 52 | + // Get the seeder thread pool executor bean |
| 53 | + SeederThreadPoolExecutor executor = |
| 54 | + applicationContext.getBean("gwcSeederThreadPoolExec", SeederThreadPoolExecutor.class); |
| 55 | + |
| 56 | + // Verify default values are used |
| 57 | + assertEquals("Core pool size should default to 16", 16, executor.getCorePoolSize()); |
| 58 | + assertEquals("Maximum pool size should default to 32", 32, executor.getMaximumPoolSize()); |
| 59 | + |
| 60 | + // Clean up |
| 61 | + executor.shutdown(); |
| 62 | + } |
| 63 | + |
| 64 | + /** Test that custom environment variable values are properly loaded. */ |
| 65 | + @Test |
| 66 | + public void testCustomEnvironmentVariableValues() { |
| 67 | + // Set custom environment variables |
| 68 | + environmentVariables.set("GWC_SEEDER_CORE_POOL_SIZE", "8"); |
| 69 | + environmentVariables.set("GWC_SEEDER_MAX_POOL_SIZE", "16"); |
| 70 | + |
| 71 | + // Load the Spring context with test configuration |
| 72 | + applicationContext = new ClassPathXmlApplicationContext("seeder-thread-pool-test-context.xml"); |
| 73 | + |
| 74 | + // Get the seeder thread pool executor bean |
| 75 | + SeederThreadPoolExecutor executor = |
| 76 | + applicationContext.getBean("gwcSeederThreadPoolExec", SeederThreadPoolExecutor.class); |
| 77 | + |
| 78 | + // Verify custom values are used |
| 79 | + assertEquals("Core pool size should be set to 8", 8, executor.getCorePoolSize()); |
| 80 | + assertEquals("Maximum pool size should be set to 16", 16, executor.getMaximumPoolSize()); |
| 81 | + |
| 82 | + // Clean up |
| 83 | + executor.shutdown(); |
| 84 | + } |
| 85 | + |
| 86 | + /** Test that only one environment variable is set (partial configuration). */ |
| 87 | + @Test |
| 88 | + public void testPartialEnvironmentVariableConfiguration() { |
| 89 | + // Set only core pool size environment variable |
| 90 | + environmentVariables.set("GWC_SEEDER_CORE_POOL_SIZE", "12"); |
| 91 | + environmentVariables.clear("GWC_SEEDER_MAX_POOL_SIZE"); |
| 92 | + |
| 93 | + // Load the Spring context with test configuration |
| 94 | + applicationContext = new ClassPathXmlApplicationContext("seeder-thread-pool-test-context.xml"); |
| 95 | + |
| 96 | + // Get the seeder thread pool executor bean |
| 97 | + SeederThreadPoolExecutor executor = |
| 98 | + applicationContext.getBean("gwcSeederThreadPoolExec", SeederThreadPoolExecutor.class); |
| 99 | + |
| 100 | + // Verify mixed configuration |
| 101 | + assertEquals("Core pool size should be set to 12", 12, executor.getCorePoolSize()); |
| 102 | + assertEquals("Maximum pool size should default to 32", 32, executor.getMaximumPoolSize()); |
| 103 | + |
| 104 | + // Clean up |
| 105 | + executor.shutdown(); |
| 106 | + } |
| 107 | + |
| 108 | + /** Test that invalid environment variable values fall back to defaults. */ |
| 109 | + @Test |
| 110 | + public void testInvalidEnvironmentVariableValues() { |
| 111 | + // Set invalid environment variables |
| 112 | + environmentVariables.set("GWC_SEEDER_CORE_POOL_SIZE", "invalid"); |
| 113 | + environmentVariables.set("GWC_SEEDER_MAX_POOL_SIZE", "not_a_number"); |
| 114 | + |
| 115 | + // Load the Spring context with test configuration |
| 116 | + applicationContext = new ClassPathXmlApplicationContext("seeder-thread-pool-test-context.xml"); |
| 117 | + |
| 118 | + // Get the seeder thread pool executor bean |
| 119 | + SeederThreadPoolExecutor executor = |
| 120 | + applicationContext.getBean("gwcSeederThreadPoolExec", SeederThreadPoolExecutor.class); |
| 121 | + |
| 122 | + // Verify default values are used when invalid values are provided |
| 123 | + assertEquals("Core pool size should default to 16 when invalid value provided", 16, executor.getCorePoolSize()); |
| 124 | + assertEquals( |
| 125 | + "Maximum pool size should default to 32 when invalid value provided", |
| 126 | + 32, |
| 127 | + executor.getMaximumPoolSize()); |
| 128 | + |
| 129 | + // Clean up |
| 130 | + executor.shutdown(); |
| 131 | + } |
| 132 | + |
| 133 | + /** Test that the SeederThreadPoolExecutor can be created with various configurations. */ |
| 134 | + @Test |
| 135 | + public void testSeederThreadPoolExecutorCreation() { |
| 136 | + // Test with different pool sizes |
| 137 | + SeederThreadPoolExecutor executor1 = new SeederThreadPoolExecutor(4, 8); |
| 138 | + assertEquals("Core pool size should be 4", 4, executor1.getCorePoolSize()); |
| 139 | + assertEquals("Maximum pool size should be 8", 8, executor1.getMaximumPoolSize()); |
| 140 | + executor1.shutdown(); |
| 141 | + |
| 142 | + SeederThreadPoolExecutor executor2 = new SeederThreadPoolExecutor(1, 1); |
| 143 | + assertEquals("Core pool size should be 1", 1, executor2.getCorePoolSize()); |
| 144 | + assertEquals("Maximum pool size should be 1", 1, executor2.getMaximumPoolSize()); |
| 145 | + executor2.shutdown(); |
| 146 | + |
| 147 | + SeederThreadPoolExecutor executor3 = new SeederThreadPoolExecutor(64, 128); |
| 148 | + assertEquals("Core pool size should be 64", 64, executor3.getCorePoolSize()); |
| 149 | + assertEquals("Maximum pool size should be 128", 128, executor3.getMaximumPoolSize()); |
| 150 | + executor3.shutdown(); |
| 151 | + } |
| 152 | +} |
0 commit comments