1 | package com.hammurapi.common.concurrent.tests; |
2 | |
3 | import static org.junit.Assert.assertEquals; |
4 | import static org.junit.Assert.assertNull; |
5 | import static org.junit.Assert.*; |
6 | |
7 | import java.util.Set; |
8 | import java.util.concurrent.Callable; |
9 | import java.util.concurrent.ExecutorService; |
10 | import java.util.concurrent.Executors; |
11 | import java.util.concurrent.Future; |
12 | import java.util.concurrent.ScheduledExecutorService; |
13 | import java.util.concurrent.TimeUnit; |
14 | |
15 | import org.junit.After; |
16 | import org.junit.Before; |
17 | import org.junit.Test; |
18 | |
19 | import com.hammurapi.common.concurrent.Invocable; |
20 | import com.hammurapi.common.concurrent.Invocation; |
21 | import com.hammurapi.common.concurrent.LocalStringPropertySet; |
22 | import com.hammurapi.common.concurrent.PropertySet; |
23 | |
24 | public class LocalStringPropertySetTests { |
25 | |
26 | private ExecutorService executorService; |
27 | private ScheduledExecutorService scheduledExecutorService; |
28 | |
29 | @Before |
30 | public void setUp() throws Exception { |
31 | executorService = Executors.newFixedThreadPool(5); |
32 | scheduledExecutorService = Executors.newScheduledThreadPool(5); |
33 | } |
34 | |
35 | @After |
36 | public void tearDown() throws Exception { |
37 | executorService.shutdown(); |
38 | scheduledExecutorService.shutdown(); |
39 | } |
40 | |
41 | private LocalStringPropertySet[] createPropertySets(PropertySet<String>... chain) { |
42 | return new LocalStringPropertySet[] { |
43 | new LocalStringPropertySet(null, null, null, null, chain), |
44 | new LocalStringPropertySet(executorService, null, null, null, chain), |
45 | new LocalStringPropertySet(scheduledExecutorService, null, null, null, chain) |
46 | }; |
47 | } |
48 | |
49 | public interface TestInterface { |
50 | |
51 | int getCount(); |
52 | |
53 | void setCount(int count); |
54 | |
55 | int increment(int delta); |
56 | |
57 | } |
58 | |
59 | @Test |
60 | public void testGetAdapter() { |
61 | for (LocalStringPropertySet ps: createPropertySets()) { |
62 | ps.set("count", "33"); |
63 | |
64 | Invocable<PropertySet<String>, Object, String> invocable = new Invocable<PropertySet<String>, Object, String>() { |
65 | |
66 | @Override |
67 | public Object invoke( |
68 | PropertySet<String> context, |
69 | Invocation<String> invocation, |
70 | ExecutorService executorService) { |
71 | |
72 | Integer current = context.get("count", Integer.class); |
73 | Integer delta = (Integer) invocation.getArguments()[0]; |
74 | Integer value = current + delta; |
75 | context.set("count", value); |
76 | return value; |
77 | } |
78 | |
79 | @Override |
80 | public Class getReturnType() { |
81 | return Integer.class; |
82 | } |
83 | |
84 | @Override |
85 | public Class<?>[] getParameterTypes() { |
86 | return new Class[] {Integer.class}; |
87 | } |
88 | |
89 | }; |
90 | |
91 | ps.setInvocable("increment", invocable); |
92 | |
93 | TestInterface adapter = ps.getAdapter(TestInterface.class, null); |
94 | |
95 | assertEquals(33, adapter.getCount()); |
96 | |
97 | adapter.setCount(88); |
98 | assertEquals(88, ps.get("count")); |
99 | |
100 | int newCount = adapter.increment(12); |
101 | assertEquals(100, newCount); |
102 | assertEquals(100, ps.get("count")); |
103 | } |
104 | } |
105 | |
106 | @Test |
107 | public void testGetFromInvocable() { |
108 | for (LocalStringPropertySet ps: createPropertySets()) { |
109 | |
110 | Invocable<PropertySet<String>, Object, String> invocable = new Invocable<PropertySet<String>, Object, String>() { |
111 | |
112 | @Override |
113 | public Object invoke( |
114 | PropertySet<String> context, |
115 | Invocation<String> invocation, |
116 | ExecutorService executorService) { |
117 | |
118 | return 33; |
119 | } |
120 | |
121 | @Override |
122 | public Class getReturnType() { |
123 | return Integer.class; |
124 | } |
125 | |
126 | @Override |
127 | public Class<?>[] getParameterTypes() { |
128 | return new Class[] {}; |
129 | } |
130 | |
131 | }; |
132 | |
133 | ps.setInvocable("closure", invocable); |
134 | assertEquals(33, ps.get("closure")); |
135 | } |
136 | } |
137 | |
138 | @Test |
139 | public void testKeySet() { |
140 | for (LocalStringPropertySet ps: createPropertySets()) { |
141 | // Own keyset |
142 | ps.set("v", 33); |
143 | ps.set("z/x", 88); |
144 | Set<String> ks = ps.keySet(); |
145 | assertEquals(2, ks.size()); |
146 | assertTrue(ks.contains("v")); |
147 | assertTrue(ks.contains("z/x")); |
148 | |
149 | // subsets' |
150 | // mounts |
151 | // chains |
152 | } |
153 | } |
154 | |
155 | @Test |
156 | public void testSubset() { |
157 | for (LocalStringPropertySet ps: createPropertySets()) { |
158 | PropertySet<String> subSet = ps.subset("root"); |
159 | subSet.set("child", 33); |
160 | assertEquals(33, ps.get("root/child")); |
161 | ps.set("root/sibling", 88); |
162 | assertEquals(88, subSet.get("sibling")); |
163 | } |
164 | } |
165 | |
166 | @Test |
167 | public void testRemove() { |
168 | for (LocalStringPropertySet ps: createPropertySets()) { |
169 | ps.set("root/sibling", 88); |
170 | assertEquals(88, ps.get("root/sibling")); |
171 | ps.remove("root/sibling"); |
172 | assertNull(ps.get("root/sibling")); |
173 | |
174 | // TODO - test with mounts. |
175 | } |
176 | } |
177 | |
178 | @Test |
179 | public void testClear() { |
180 | for (LocalStringPropertySet ps: createPropertySets()) { |
181 | ps.set("root/sibling", 88); |
182 | assertEquals(88, ps.get("root/sibling")); |
183 | ps.clear(); |
184 | assertNull(ps.get("root/sibling")); |
185 | |
186 | // TODO - test with mounts. |
187 | } |
188 | } |
189 | |
190 | @Test |
191 | public void testMount() { |
192 | for (LocalStringPropertySet ps: createPropertySets()) { |
193 | PropertySet<String> subSet = ps.subset("root"); |
194 | subSet.set("child", 33); |
195 | subSet.set("s3", 888); |
196 | assertEquals(33, ps.get("root/child")); |
197 | |
198 | LocalStringPropertySet mount = new LocalStringPropertySet(executorService, null, null, null); |
199 | ps.mount("root", mount); |
200 | assertNull(ps.get("root/child")); |
201 | |
202 | ps.set("root/sibling", 88); |
203 | assertNull(subSet.get("sibling")); |
204 | assertEquals(88, mount.get("sibling")); |
205 | |
206 | mount.set("s2", 777); |
207 | assertEquals(777, ps.get("root/s2")); |
208 | assertNull(ps.get("root/s3")); |
209 | |
210 | ps.unmount("root"); |
211 | assertNull(ps.get("root/s2")); |
212 | assertEquals(888, ps.get("root/s3")); |
213 | } |
214 | } |
215 | |
216 | @Test |
217 | public void testSetAll() { |
218 | for (LocalStringPropertySet ps: createPropertySets()) { |
219 | LocalStringPropertySet src = new LocalStringPropertySet(executorService, null, null, null); |
220 | src.set("v1", 33); |
221 | src.set("v2", 88); |
222 | ps.setAll(src); |
223 | |
224 | assertEquals(33, ps.get("v1")); |
225 | assertEquals(88, ps.get("v2")); |
226 | } |
227 | } |
228 | |
229 | @Test |
230 | public void testGet() { |
231 | for (LocalStringPropertySet ps: createPropertySets()) { |
232 | ps.set("v", 33); |
233 | assertEquals(33, ps.get("v")); |
234 | } |
235 | } |
236 | |
237 | @Test |
238 | public void testGetConverted() { |
239 | for (LocalStringPropertySet ps: createPropertySets()) { |
240 | ps.set("v", "33"); |
241 | assertEquals(33, (Object) ps.get("v", Integer.class)); |
242 | } |
243 | } |
244 | |
245 | @Test |
246 | public void testGetDefaultValue() { |
247 | for (LocalStringPropertySet ps: createPropertySets()) { |
248 | ps.set("v", "33"); |
249 | assertEquals("33", ps.get("v", "77")); |
250 | assertEquals("77", ps.get("v2", "77")); |
251 | } |
252 | } |
253 | |
254 | @Test |
255 | public void testGetConvertedDefaultValue() { |
256 | for (LocalStringPropertySet ps: createPropertySets()) { |
257 | ps.set("v", "33"); |
258 | assertEquals(33, (Object) ps.get("v", Integer.class, 77)); |
259 | assertEquals(77, (Object) ps.get("v2", Integer.class, 77)); |
260 | } |
261 | } |
262 | |
263 | @Test |
264 | public void testCookCallable() throws InterruptedException { |
265 | LocalStringPropertySet[] pss = createPropertySets(); |
266 | for (int i=0; i<pss.length; ++i) { |
267 | final boolean[] called = new boolean[] {false}; |
268 | |
269 | pss[i].cook("v", new Callable<String>() { |
270 | |
271 | @Override |
272 | public String call() throws Exception { |
273 | called[0] = true; |
274 | return "Hello"; |
275 | } |
276 | |
277 | }); |
278 | |
279 | Thread.sleep(500); |
280 | if (i>0) { |
281 | assertTrue(called[0]); |
282 | } |
283 | |
284 | assertEquals("Hello", pss[i].get("v")); |
285 | } |
286 | } |
287 | |
288 | @Test |
289 | public void testDelayedCook() throws InterruptedException { |
290 | LocalStringPropertySet[] pss = createPropertySets(); |
291 | for (int i=0; i<pss.length; ++i) { |
292 | final boolean[] called = new boolean[] {false}; |
293 | |
294 | pss[i].cook("v", new Callable<String>() { |
295 | |
296 | @Override |
297 | public String call() throws Exception { |
298 | called[0] = true; |
299 | return "Hello"; |
300 | } |
301 | |
302 | }, 100, TimeUnit.MILLISECONDS); |
303 | |
304 | Thread.sleep(50); |
305 | if (i>1) { |
306 | assertFalse(called[0]); |
307 | } |
308 | Thread.sleep(200); |
309 | if (i>0) { |
310 | assertTrue(called[0]); |
311 | } |
312 | |
313 | assertEquals("Hello", pss[i].get("v")); |
314 | } |
315 | } |
316 | |
317 | @Test |
318 | public void testCooking() throws InterruptedException { |
319 | LocalStringPropertySet[] pss = createPropertySets(); |
320 | for (int i=0; i<pss.length; ++i) { |
321 | final boolean[] called = new boolean[] {false}; |
322 | |
323 | Future<String> future = executorService.submit(new Callable<String>() { |
324 | |
325 | @Override |
326 | public String call() throws Exception { |
327 | called[0] = true; |
328 | return "Hello"; |
329 | } |
330 | |
331 | }); |
332 | |
333 | pss[i].cooking("v", future); |
334 | |
335 | Thread.sleep(500); |
336 | assertTrue(called[0]); |
337 | assertEquals("Hello", pss[i].get("v")); |
338 | } |
339 | } |
340 | |
341 | @Test |
342 | public void testLazy() throws InterruptedException { |
343 | LocalStringPropertySet[] pss = createPropertySets(); |
344 | for (int i=0; i<pss.length; ++i) { |
345 | final boolean[] called = new boolean[] {false}; |
346 | |
347 | pss[i].lazy("v", new Callable<String>() { |
348 | |
349 | @Override |
350 | public String call() throws Exception { |
351 | called[0] = true; |
352 | return "Hello"; |
353 | } |
354 | |
355 | }); |
356 | |
357 | Thread.sleep(500); |
358 | assertFalse(called[0]); |
359 | assertEquals("Hello", pss[i].get("v")); |
360 | assertTrue(called[0]); |
361 | } |
362 | } |
363 | |
364 | @Test |
365 | public void testCancel() throws InterruptedException { |
366 | LocalStringPropertySet[] pss = createPropertySets(); |
367 | for (int i=0; i<pss.length; ++i) { |
368 | final boolean[] called = new boolean[] {false}; |
369 | |
370 | pss[i].cook("v", new Callable<String>() { |
371 | |
372 | @Override |
373 | public String call() throws Exception { |
374 | called[0] = true; |
375 | return "Hello"; |
376 | } |
377 | |
378 | }, 100, TimeUnit.MILLISECONDS); |
379 | |
380 | pss[i].cancel("v"); |
381 | Thread.sleep(200); |
382 | if (i!=1) { |
383 | assertFalse(called[0]); |
384 | } |
385 | |
386 | assertEquals("Hello", pss[i].get("v")); |
387 | } |
388 | } |
389 | |
390 | @Test |
391 | public void testCancelAll() throws InterruptedException { |
392 | LocalStringPropertySet[] pss = createPropertySets(); |
393 | for (int i=0; i<pss.length; ++i) { |
394 | final boolean[] called = new boolean[] {false}; |
395 | |
396 | pss[i].cook("v", new Callable<String>() { |
397 | |
398 | @Override |
399 | public String call() throws Exception { |
400 | called[0] = true; |
401 | return "Hello"; |
402 | } |
403 | |
404 | }, 100, TimeUnit.MILLISECONDS); |
405 | |
406 | pss[i].cancelAll(); |
407 | Thread.sleep(200); |
408 | assertFalse(called[0]); |
409 | |
410 | assertEquals("Hello", pss[i].get("v")); |
411 | } |
412 | } |
413 | |
414 | @Test |
415 | public void testInvoke() { |
416 | for (LocalStringPropertySet ps: createPropertySets()) { |
417 | |
418 | Invocable<PropertySet<String>, Object, String> invocable = new Invocable<PropertySet<String>, Object, String>() { |
419 | |
420 | @Override |
421 | public Object invoke( |
422 | PropertySet<String> context, |
423 | Invocation<String> invocation, |
424 | ExecutorService executorService) { |
425 | |
426 | return "33"+invocation.getArguments()[0]; |
427 | } |
428 | |
429 | @Override |
430 | public Class getReturnType() { |
431 | return Integer.class; |
432 | } |
433 | |
434 | @Override |
435 | public Class<?>[] getParameterTypes() { |
436 | return new Class[] {Integer.class}; |
437 | } |
438 | |
439 | }; |
440 | |
441 | ps.setInvocable("concat", invocable); |
442 | |
443 | Object ret = ps.invoke("concat", "88"); |
444 | assertEquals("3388", ret); |
445 | } |
446 | } |
447 | |
448 | @Test |
449 | public void testChain() { |
450 | LocalStringPropertySet chain = new LocalStringPropertySet(executorService, null, null, null); |
451 | chain.set("v", 33); |
452 | |
453 | for (LocalStringPropertySet ps: createPropertySets(chain)) { |
454 | assertEquals(33, ps.get("v")); |
455 | ps.set("v1", 88); |
456 | assertEquals(88, ps.get("v1")); |
457 | assertNull(chain.get("v1")); |
458 | } |
459 | } |
460 | |
461 | } |