Changeset 11448 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2017-01-10T00:07:50+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java
r11444 r11448 14 14 import org.junit.Rule; 15 15 import org.junit.Test; 16 import org.openstreetmap.josm.Main; 16 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 18 import org.openstreetmap.josm.tools.Utils; … … 22 23 * Simple tests for ThreadPoolExecutor / HostLimitQueue veryfing, that this pair works OK 23 24 * @author Wiktor Niesiobedzki 24 *25 25 */ 26 26 public class HostLimitQueueTest { … … 31 31 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 32 32 public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20 * 1000); 33 34 33 35 34 private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) { … … 53 52 private URL url; 54 53 private AtomicInteger counter; 55 private int id;56 54 57 public Task(ICacheAccess<String, CacheEntry> cache, URL url, AtomicInteger counter, int id) {55 Task(ICacheAccess<String, CacheEntry> cache, URL url, AtomicInteger counter) { 58 56 super(cache, 1, 1, null); 59 57 this.url = url; 60 58 this.counter = counter; 61 this.id = id;62 59 } 63 60 … … 67 64 Thread.sleep(1000); 68 65 } catch (InterruptedException e) { 66 Main.trace(e); 69 67 } finally { 70 68 this.counter.incrementAndGet(); … … 91 89 /** 92 90 * Check if single threaded execution works properly 93 * @throws InterruptedException 94 * @throws IOException 91 * @throws Exception in case of error 95 92 */ 96 93 @Test 97 public void test_single_thread_per_host() throws InterruptedException, IOException {94 public void test_single_thread_per_host() throws Exception { 98 95 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1); 99 96 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, ""); … … 101 98 long start = System.currentTimeMillis(); 102 99 for (int i = 0; i < 10; i++) { 103 tpe.execute(new Task(cache, new URL("http://localhost/"+i), counter , i));100 tpe.execute(new Task(cache, new URL("http://localhost/"+i), counter)); 104 101 } 105 102 tpe.shutdown(); … … 111 108 // so it should take ~10 seconds to finish 112 109 // if it's shorter, it means that host limit does not work 113 assertTrue("Expected duration between 9 and 11 seconds not met. Actual duration: " + (duration /1000), duration < 11*1000 & duration > 9*1000 ); 110 assertTrue("Expected duration between 9 and 11 seconds not met. Actual duration: " + (duration /1000), 111 duration < 11*1000 & duration > 9*1000); 114 112 } 115 113 116 114 /** 117 115 * Check if two threaded execution work properly 118 * @throws InterruptedException 119 * @throws IOException 116 * @throws Exception in case of error 120 117 */ 121 118 @Test 122 public void test_multiple_thread_per_host() throws InterruptedException, IOException {119 public void test_multiple_thread_per_host() throws Exception { 123 120 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 2); 124 121 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, ""); … … 126 123 long start = System.currentTimeMillis(); 127 124 for (int i = 0; i < 10; i++) { 128 tpe.execute(new Task(cache, new URL("http://hostlocal/"+i), counter , i));125 tpe.execute(new Task(cache, new URL("http://hostlocal/"+i), counter)); 129 126 } 130 127 tpe.shutdown(); … … 136 133 // so it should take ~5 seconds to finish 137 134 // if it's shorter, it means that host limit does not work 138 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000), duration < 6*1000 & duration > 4*1000 ); 135 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000), 136 duration < 6*1000 & duration > 4*1000); 139 137 } 140 138 141 139 /** 142 140 * Check two hosts 143 * @throws InterruptedException 144 * @throws IOException 141 * @throws Exception in case of error 145 142 */ 146 143 @Test 147 public void test_two_hosts() throws InterruptedException, IOException {144 public void test_two_hosts() throws Exception { 148 145 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1); 149 146 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, ""); … … 152 149 for (int i = 0; i < 10; i++) { 153 150 String url = (i % 2 == 0) ? "http://localhost" : "http://hostlocal"; 154 tpe.execute(new Task(cache, new URL(url+i), counter , i));151 tpe.execute(new Task(cache, new URL(url+i), counter)); 155 152 } 156 153 tpe.shutdown(); … … 162 159 // so it should take ~5 seconds to finish 163 160 // if it's shorter, it means that host limit does not work 164 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000), duration < 6*1000 & duration > 4*1000 ); 161 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000), 162 duration < 6*1000 & duration > 4*1000); 165 163 } 166 167 164 }
Note:
See TracChangeset
for help on using the changeset viewer.