source: josm/trunk/test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java@ 13733

Last change on this file since 13733 was 13733, checked in by wiktorn, 6 years ago

Imagery definition refactor

Extend imagery definitions by:

  • allowing setting default layers for WMS_ENDPOINT and WMTS
  • allowing setting minimum expires time for tile for this imagery
  • allowing setting custom headers that will be sent for all requests

(get map, get capabilities) for this imagery

Additional changes in code:

  • use TileJobOptions to pass miscellaneous options to loaders
  • refactor WMSImagery to use SAX parser

See: #15981, #7953, #16224, #15940, #16249

File size: 6.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.io.IOException;
8import java.net.URL;
9import java.util.concurrent.ThreadPoolExecutor;
10import java.util.concurrent.TimeUnit;
11import java.util.concurrent.atomic.AtomicInteger;
12
13import org.apache.commons.jcs.access.behavior.ICacheAccess;
14import org.junit.Rule;
15import org.junit.Test;
16import org.openstreetmap.josm.data.imagery.TileJobOptions;
17import org.openstreetmap.josm.testutils.JOSMTestRules;
18import org.openstreetmap.josm.tools.Logging;
19import org.openstreetmap.josm.tools.Utils;
20
21import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22
23/**
24 * Simple tests for ThreadPoolExecutor / HostLimitQueue veryfing, that this pair works OK
25 * @author Wiktor Niesiobedzki
26 */
27public class HostLimitQueueTest {
28 /**
29 * Setup test.
30 */
31 @Rule
32 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
33 public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20 * 1000);
34
35 private static ThreadPoolExecutor getNewThreadPoolExecutor(String nameFormat, int workers, int queueLimit) {
36 HostLimitQueue workQueue = new HostLimitQueue(queueLimit);
37 ThreadPoolExecutor executor = new ThreadPoolExecutor(
38 0, // 0 so for unused thread pools threads will eventually die, freeing also the threadpool
39 workers, // do not this number of threads
40 300, // keepalive for thread
41 TimeUnit.SECONDS,
42 workQueue,
43 Utils.newThreadFactory(nameFormat, Thread.NORM_PRIORITY)
44 );
45 workQueue.setExecutor(executor);
46 return executor;
47 }
48
49 /**
50 * Mock class for tests
51 */
52 static class Task extends JCSCachedTileLoaderJob<String, CacheEntry> {
53 private URL url;
54 private AtomicInteger counter;
55
56 Task(ICacheAccess<String, CacheEntry> cache, URL url, AtomicInteger counter) {
57 super(cache, new TileJobOptions(1, 1, null, 10));
58 this.url = url;
59 this.counter = counter;
60 }
61
62 @Override
63 public void run() {
64 try {
65 Thread.sleep(1000);
66 } catch (InterruptedException e) {
67 Logging.trace(e);
68 } finally {
69 this.counter.incrementAndGet();
70 executionFinished();
71 }
72 }
73
74 @Override
75 public String getCacheKey() {
76 return "";
77 }
78
79 @Override
80 public URL getUrl() throws IOException {
81 return this.url;
82 }
83
84 @Override
85 protected CacheEntry createCacheEntry(byte[] content) {
86 return null;
87 }
88 }
89
90 /**
91 * Check if single threaded execution works properly
92 * @throws Exception in case of error
93 */
94 @Test
95 public void testSingleThreadPerHost() throws Exception {
96 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1);
97 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
98 AtomicInteger counter = new AtomicInteger(0);
99 long start = System.currentTimeMillis();
100 for (int i = 0; i < 10; i++) {
101 tpe.execute(new Task(cache, new URL("http://localhost/"+i), counter));
102 }
103 tpe.shutdown();
104 tpe.awaitTermination(15, TimeUnit.SECONDS); // at most it should take ~10 seconds, so after 15 it's already failed
105 long duration = System.currentTimeMillis() - start;
106 // check that all tasks were executed
107 assertEquals(10, counter.get());
108 // although there are 3 threads, we can make only 1 parallel call to localhost
109 // so it should take ~10 seconds to finish
110 // if it's shorter, it means that host limit does not work
111 assertTrue("Expected duration between 9 and 11 seconds not met. Actual duration: " + (duration /1000),
112 duration < 11*1000 & duration > 9*1000);
113 }
114
115 /**
116 * Check if two threaded execution work properly
117 * @throws Exception in case of error
118 */
119 @Test
120 public void testMultipleThreadPerHost() throws Exception {
121 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 2);
122 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
123 AtomicInteger counter = new AtomicInteger(0);
124 long start = System.currentTimeMillis();
125 for (int i = 0; i < 10; i++) {
126 tpe.execute(new Task(cache, new URL("http://hostlocal/"+i), counter));
127 }
128 tpe.shutdown();
129 tpe.awaitTermination(15, TimeUnit.SECONDS);
130 long duration = System.currentTimeMillis() - start;
131 // check that all tasks were executed
132 assertEquals(10, counter.get());
133 // although there are 3 threads, we can make only 2 parallel call to localhost
134 // so it should take ~5 seconds to finish
135 // if it's shorter, it means that host limit does not work
136 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000),
137 duration < 6*1000 & duration > 4*1000);
138 }
139
140 /**
141 * Check two hosts
142 * @throws Exception in case of error
143 */
144 @Test
145 public void testTwoHosts() throws Exception {
146 ThreadPoolExecutor tpe = getNewThreadPoolExecutor("test-%d", 3, 1);
147 ICacheAccess<String, CacheEntry> cache = JCSCacheManager.getCache("test", 3, 0, "");
148 AtomicInteger counter = new AtomicInteger(0);
149 long start = System.currentTimeMillis();
150 for (int i = 0; i < 10; i++) {
151 String url = (i % 2 == 0) ? "http://localhost" : "http://hostlocal";
152 tpe.execute(new Task(cache, new URL(url+i), counter));
153 }
154 tpe.shutdown();
155 tpe.awaitTermination(15, TimeUnit.SECONDS);
156 long duration = System.currentTimeMillis() - start;
157 // check that all tasks were executed
158 assertEquals(10, counter.get());
159 // although there are 3 threads, we can make only 1 parallel per host, and we have 2 hosts
160 // so it should take ~5 seconds to finish
161 // if it's shorter, it means that host limit does not work
162 assertTrue("Expected duration between 4 and 6 seconds not met. Actual duration: " + (duration /1000),
163 duration < 6*1000 & duration > 4*1000);
164 }
165}
Note: See TracBrowser for help on using the repository browser.