1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.jcache;
18
19 import edu.emory.mathcs.backport.java.util.concurrent.ExecutionException;
20 import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
21 import edu.emory.mathcs.backport.java.util.concurrent.Future;
22 import net.sf.ehcache.AbstractCacheTest;
23 import net.sf.ehcache.Ehcache;
24 import net.sf.ehcache.StopWatch;
25 import net.sf.ehcache.ThreadKiller;
26 import net.sf.jsr107cache.Cache;
27 import net.sf.jsr107cache.CacheEntry;
28 import net.sf.jsr107cache.CacheException;
29 import net.sf.jsr107cache.CacheManager;
30 import net.sf.jsr107cache.CacheStatistics;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 import java.io.Serializable;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Random;
40 import java.util.Set;
41 import java.util.Date;
42 import java.util.Collection;
43
44
45
46
47
48
49 public class JCacheTest extends AbstractCacheTest {
50 private static final Log LOG = LogFactory.getLog(JCacheTest.class.getName());
51
52
53
54
55 protected void setUp() throws Exception {
56 super.setUp();
57 System.gc();
58 Thread.sleep(100);
59 System.gc();
60 }
61
62
63
64
65
66
67 protected void tearDown() throws Exception {
68 getTest1Cache().clear();
69 getTest2Cache().clear();
70 getTest4Cache().clear();
71 manager.getCache("sampleCache1").removeAll();
72 }
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 protected Cache getTest1Cache() throws CacheException {
88 Cache cache = CacheManager.getInstance().getCache("test1");
89 if (cache == null) {
90
91 Map env = new HashMap();
92 env.put("name", "test1");
93 env.put("maxElementsInMemory", "10000");
94 env.put("maxElementsOnDisk", "1000");
95 env.put("memoryStoreEvictionPolicy", "LRU");
96 env.put("overflowToDisk", "true");
97 env.put("eternal", "false");
98 env.put("timeToLiveSeconds", "1000");
99 env.put("timeToIdleSeconds", "1000");
100 env.put("diskPersistent", "false");
101 env.put("diskExpiryThreadIntervalSeconds", "120");
102 env.put("cacheLoaderFactoryClassName", "net.sf.ehcache.jcache.CountingCacheLoaderFactory");
103 cache = CacheManager.getInstance().getCacheFactory().createCache(env);
104 CacheManager.getInstance().registerCache("test1", cache);
105 }
106 return CacheManager.getInstance().getCache("test1");
107 }
108
109 private Cache getTest2Cache() throws CacheException {
110 Cache cache = CacheManager.getInstance().getCache("test2");
111 if (cache == null) {
112 Map env = new HashMap();
113 env.put("name", "test2");
114 env.put("maxElementsInMemory", "1");
115 env.put("overflowToDisk", "true");
116 env.put("eternal", "false");
117 env.put("timeToLiveSeconds", "1");
118 env.put("timeToIdleSeconds", "0");
119 cache = CacheManager.getInstance().getCacheFactory().createCache(env);
120 CacheManager.getInstance().registerCache("test2", cache);
121 }
122 return CacheManager.getInstance().getCache("test2");
123 }
124
125 private Cache getTest4Cache() throws CacheException {
126 Cache cache = CacheManager.getInstance().getCache("test4");
127 if (cache == null) {
128 Map env = new HashMap();
129 env.put("name", "test4");
130 env.put("maxElementsInMemory", "1000");
131 env.put("overflowToDisk", "true");
132 env.put("eternal", "true");
133 env.put("timeToLiveSeconds", "0");
134 env.put("timeToIdleSeconds", "0");
135 cache = CacheManager.getInstance().getCacheFactory().createCache(env);
136 CacheManager.getInstance().registerCache("test4", cache);
137 }
138 return CacheManager.getInstance().getCache("test4");
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public void testExpiryBasedOnTimeToLiveWhenNoIdle() throws Exception {
183 Cache cache = new JCache(manager.getCache("sampleCacheNoIdle"), null);
184 cache.put("key1", "value1");
185 cache.put("key2", "value1");
186 assertNotNull(cache.get("key1"));
187 assertNotNull(cache.get("key2"));
188
189
190 Thread.sleep(2000);
191 assertNotNull(cache.get("key1"));
192 assertNotNull(cache.get("key2"));
193
194
195 Thread.sleep(5020);
196 assertNull(cache.get("key1"));
197 assertNull(cache.get("key2"));
198 }
199
200
201
202
203
204
205
206
207
208
209 public void testNoOverflowToDisk() throws Exception {
210 Ehcache ehcache = new net.sf.ehcache.Cache("testNoOverflowToDisk", 1, false, true, 500, 200);
211 manager.addCache(ehcache);
212 Cache cache = new JCache(ehcache, null);
213 cache.put("key1", "value1");
214 cache.put("key2", "value1");
215 assertNull(cache.get("key1"));
216 assertNotNull(cache.get("key2"));
217 }
218
219
220
221
222
223 public void testIsEmpty() throws Exception {
224 Ehcache ehcache = new net.sf.ehcache.Cache("testIsEmpty", 1, true, true, 500, 200);
225 manager.addCache(ehcache);
226 Cache cache = new JCache(ehcache, null);
227 assertTrue(cache.isEmpty());
228
229 cache.put("key1", "value1");
230 assertFalse(cache.isEmpty());
231 cache.put("key2", "value1");
232 assertFalse(cache.isEmpty());
233
234
235 assertNotNull(cache.get("key1"));
236 assertNotNull(cache.get("key2"));
237 }
238
239
240
241
242 public void testEvict() throws Exception {
243 Ehcache ehcache = new net.sf.ehcache.Cache("testEvict", 1, true, false, 1, 200);
244 manager.addCache(ehcache);
245 Cache cache = new JCache(ehcache, null);
246 assertTrue(cache.isEmpty());
247
248 cache.put("key1", "value1");
249 cache.put("key2", "value1");
250
251 cache.evict();
252 assertFalse(cache.isEmpty());
253
254 Thread.sleep(1020);
255 cache.evict();
256 assertNull(cache.get("key1"));
257 assertNull(cache.get("key2"));
258
259 cache.put("key1", "value1");
260 cache.put("key2", "value1");
261 Thread.sleep(1020);
262 assertNull(cache.get("key1"));
263 assertNull(cache.get("key2"));
264
265 }
266
267
268
269
270 public void testContainsKey() throws Exception {
271 Ehcache ehcache = new net.sf.ehcache.Cache("testContainsKey", 1, true, true, 500, 200);
272 manager.addCache(ehcache);
273 Cache cache = new JCache(ehcache, null);
274 assertFalse(cache.containsKey("key1"));
275 assertFalse(cache.containsKey("key2"));
276
277 cache.put("key1", "value1");
278 assertTrue(cache.containsKey("key1"));
279 assertFalse(cache.containsKey("key2"));
280
281 cache.put("key2", "value1");
282 assertTrue(cache.containsKey("key1"));
283 assertTrue(cache.containsKey("key2"));
284
285 assertNotNull(cache.get("key1"));
286 assertNotNull(cache.get("key2"));
287 }
288
289
290
291
292
293 public void testContainsValue() throws Exception {
294 Ehcache ehcache = new net.sf.ehcache.Cache("testContainsValue", 2, true, true, 500, 200);
295 manager.addCache(ehcache);
296 Cache cache = new JCache(ehcache, null);
297 assertFalse(cache.containsValue("value1"));
298 assertFalse(cache.containsValue(null));
299
300 cache.put("key1", null);
301 assertFalse(cache.containsValue("value1"));
302 assertTrue(cache.containsValue(null));
303
304 cache.put("key2", "value1");
305 assertTrue(cache.containsValue("value1"));
306 assertTrue(cache.containsValue(null));
307
308 assertNull(cache.get("key1"));
309 assertNotNull(cache.get("key2"));
310 }
311
312
313
314
315
316 public void testGet() throws Exception {
317 Ehcache ehcache = new net.sf.ehcache.Cache("testGet", 10, true, true, 500, 200);
318 manager.addCache(ehcache);
319 JCache jcache = new JCache(manager.getCache("sampleCache1"), null);
320
321
322 jcache.put("key", null);
323 Object value = jcache.get("key");
324 assertNull(value);
325
326
327 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
328 jcache.setCacheLoader(countingCacheLoader);
329 value = jcache.get("key");
330 assertNull(value);
331
332
333 jcache.remove("key");
334 jcache.setCacheLoader(null);
335 jcache.put("key", null);
336 value = jcache.get("key");
337 assertNull(value);
338
339
340 jcache.remove("key");
341 jcache.setCacheLoader(countingCacheLoader);
342 value = jcache.get("key");
343 assertEquals(new Integer(0), value);
344
345
346 jcache.put("key2", "value");
347 value = jcache.get("key2");
348 assertEquals("value", value);
349
350 }
351
352
353
354
355
356 public void testGetValues() throws Exception {
357 Ehcache ehcache = new net.sf.ehcache.Cache("testGetValue", 2, true, true, 500, 200);
358 manager.addCache(ehcache);
359
360 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
361 JCache jcache = new JCache(manager.getCache("sampleCache1"), countingCacheLoader);
362
363
364 class NonSerializable { }
365
366 List keys = new ArrayList();
367 for (int i = 0; i < 1000; i++) {
368 keys.add(new Integer(i));
369 }
370 jcache.loadAll(keys);
371 jcache.put(new Integer(1), new Date());
372 jcache.put(new Integer(2), new NonSerializable());
373 Thread.sleep((long) (3000 * StopWatch.getSpeedAdjustmentFactor()));
374 assertEquals(1000, jcache.size());
375
376 Collection values = jcache.values();
377 assertEquals(1000, values.size());
378
379 }
380
381
382
383
384 public void testPutAll() {
385 Ehcache ehcache = new net.sf.ehcache.Cache("testPutAll", 2, true, true, 500, 200);
386 manager.addCache(ehcache);
387 Cache cache = new JCache(ehcache, null);
388 assertTrue(cache.isEmpty());
389
390 cache.putAll(null);
391 assertTrue(cache.isEmpty());
392
393 cache.putAll(new HashMap());
394 assertTrue(cache.isEmpty());
395
396 Map map = new HashMap();
397 for (int i = 0; i < 10; i++) {
398 map.put(i + "", new Date());
399 }
400
401 cache.putAll(map);
402 assertEquals(10, cache.size());
403 }
404
405
406
407
408 public void testClear() {
409 Ehcache ehcache = new net.sf.ehcache.Cache("testClear", 2, true, true, 500, 200);
410 manager.addCache(ehcache);
411 Cache cache = new JCache(ehcache, null);
412
413 assertTrue(cache.isEmpty());
414 cache.clear();
415 assertTrue(cache.isEmpty());
416
417 cache.put("1", new Date());
418 cache.clear();
419 assertTrue(cache.isEmpty());
420 }
421
422
423
424
425
426 public void testKeySet() {
427 Ehcache ehcache = new net.sf.ehcache.Cache("testKeySet", 2, true, true, 500, 200);
428 manager.addCache(ehcache);
429 Cache cache = new JCache(ehcache, null);
430
431 for (int i = 0; i < 10; i++) {
432 cache.put(i + "", new Date());
433 }
434
435 cache.put(0 + "", new Date());
436 Set set = cache.keySet();
437 assertEquals(10, set.size());
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453 public void testProportionMemoryAndDiskPerformance() throws Exception {
454 StopWatch stopWatch = new StopWatch();
455 long time = 0;
456
457
458 Ehcache ehcache = new net.sf.ehcache.Cache("testMemoryOnly", 5000, false, false, 5, 2);
459 manager.addCache(ehcache);
460 Cache memoryOnlyCache = new JCache(ehcache, null);
461
462 time = stopWatch.getElapsedTime();
463 for (int i = 0; i < 5000; i++) {
464 Integer key = new Integer(i);
465 memoryOnlyCache.put(new Integer(i), "value");
466 memoryOnlyCache.get(key);
467 }
468 time = stopWatch.getElapsedTime();
469 LOG.info("Time for MemoryStore: " + time);
470 assertTrue("Time to put and get 5000 entries into MemoryStore", time < 300);
471
472
473
474
475 Ehcache diskOnlyEhcache = new net.sf.ehcache.Cache("testDiskOnly", 0, true, false, 5, 2);
476 manager.addCache(diskOnlyEhcache);
477 Cache diskOnlyCache = new JCache(ehcache, null);
478 time = stopWatch.getElapsedTime();
479 for (int i = 0; i < 5000; i++) {
480 Integer key = new Integer(i);
481 diskOnlyCache.put(key, "value");
482 diskOnlyCache.get(key);
483 }
484 time = stopWatch.getElapsedTime();
485 LOG.info("Time for DiskStore: " + time);
486 assertTrue("Time to put and get 5000 entries into DiskStore was less than 2 sec", time < 2000);
487
488
489
490
491 Ehcache m1d999Ehcache = new net.sf.ehcache.Cache("m1d999Cache", 1, true, false, 5, 2);
492 manager.addCache(m1d999Ehcache);
493 Cache m1d999Cache = new JCache(m1d999Ehcache, null);
494 time = stopWatch.getElapsedTime();
495 for (int i = 0; i < 5000; i++) {
496 Integer key = new Integer(i);
497 m1d999Cache.put(key, "value");
498 m1d999Cache.get(key);
499 }
500 time = stopWatch.getElapsedTime();
501 LOG.info("Time for m1d999Cache: " + time);
502 assertTrue("Time to put and get 5000 entries into m1d999Cache", time < 2000);
503
504
505
506
507 Ehcache m500d500Ehcache = new net.sf.ehcache.Cache("m500d500Cache", 500, true, false, 5, 2);
508 manager.addCache(m500d500Ehcache);
509 Cache m500d500Cache = new JCache(m1d999Ehcache, null);
510
511 time = stopWatch.getElapsedTime();
512 for (int i = 0; i < 5000; i++) {
513 Integer key = new Integer(i);
514 m500d500Cache.put(key, "value");
515 m500d500Cache.get(key);
516 }
517 time = stopWatch.getElapsedTime();
518 LOG.info("Time for m500d500Cache: " + time);
519 assertTrue("Time to put and get 5000 entries into m500d500Cache", time < 2000);
520
521 }
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537 public void testExpiryBasedOnTimeToLive() throws Exception {
538
539 Ehcache ehcache = new net.sf.ehcache.Cache("testExpiryBasedOnTimeToLive", 1, true, false, 3, 0);
540 manager.addCache(ehcache);
541 Cache cache = new JCache(ehcache, null);
542
543 cache.put("key1", "value1");
544 cache.put("key2", "value1");
545
546
547 assertNotNull(cache.get("key1"));
548 assertNotNull(cache.get("key2"));
549 Thread.sleep(1020);
550
551 assertNotNull(cache.get("key1"));
552 assertNotNull(cache.get("key2"));
553 Thread.sleep(1020);
554
555 assertNotNull(cache.get("key1"));
556 assertNotNull(cache.get("key2"));
557 Thread.sleep(1020);
558 assertNull(cache.get("key1"));
559 assertNull(cache.get("key2"));
560 }
561
562
563
564
565
566
567 public void testExpiryBasedOnTimeToLiveUsingPeek() throws Exception {
568
569 Ehcache ehcache = new net.sf.ehcache.Cache("testExpiryBasedOnTimeToLiveUsingPeek", 1, true, false, 3, 0);
570 manager.addCache(ehcache);
571 Cache cache = new JCache(ehcache, null);
572
573 cache.put("key1", "value1");
574 cache.put("key2", "value1");
575
576
577 assertNotNull(cache.peek("key1"));
578 assertNotNull(cache.peek("key2"));
579 Thread.sleep(1020);
580
581 assertNotNull(cache.peek("key1"));
582 assertNotNull(cache.peek("key2"));
583 Thread.sleep(1020);
584
585 assertNotNull(cache.peek("key1"));
586 assertNotNull(cache.peek("key2"));
587 Thread.sleep(1020);
588 assertNull(cache.peek("key1"));
589 assertNull(cache.peek("key2"));
590 }
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612 public void testNoIdleOrExpiryBasedOnTimeToLiveForEternal() throws Exception {
613
614 Ehcache ehcache = new net.sf.ehcache.Cache("testNoIdleOrExpiryBasedOnTimeToLiveForEternal", 1, true, true, 5, 2);
615 manager.addCache(ehcache);
616 Cache cache = new JCache(ehcache, null);
617
618 cache.put("key1", "value1");
619 cache.put("key2", "value1");
620
621
622 assertNotNull(cache.get("key1"));
623 assertNotNull(cache.get("key2"));
624
625
626 Thread.sleep(2020);
627 assertNotNull(cache.get("key1"));
628 assertNotNull(cache.get("key2"));
629
630
631 Thread.sleep(3020);
632 assertNotNull(cache.get("key1"));
633 assertNotNull(cache.get("key2"));
634 }
635
636
637
638
639 public void testExpiryBasedOnTimeToIdle() throws Exception {
640
641 Ehcache ehcache = new net.sf.ehcache.Cache("testExpiryBasedOnTimeToIdle", 1, true, false, 6, 2);
642 manager.addCache(ehcache);
643 Cache cache = new JCache(ehcache, null);
644
645 cache.put("key1", "value1");
646 cache.put("key2", "value1");
647
648
649 assertNotNull(cache.get("key1"));
650 assertNotNull(cache.get("key2"));
651 Thread.sleep(2020);
652 assertNull(cache.get("key1"));
653 assertNull(cache.get("key2"));
654
655
656 cache.put("key1", "value1");
657 cache.put("key2", "value1");
658 Thread.sleep(1020);
659 assertNotNull(cache.get("key1"));
660 assertNotNull(cache.get("key2"));
661
662 Thread.sleep(2020);
663 assertNull(cache.get("key1"));
664 assertNull(cache.get("key2"));
665 }
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682 public void testElementStatistics() throws Exception {
683 Ehcache ehcache = new net.sf.ehcache.Cache("testElementStatistics", 1, true, false, 5, 2);
684 manager.addCache(ehcache);
685 Cache cache = new JCache(ehcache, null);
686
687 cache.put("key1", "value1");
688 cache.put("key2", "value1");
689
690 CacheEntry cacheEntry = cache.getCacheEntry("key1");
691 assertEquals("Should be one", 1, cacheEntry.getHits());
692
693 cacheEntry = cache.getCacheEntry("key1");
694 assertEquals("Should be two", 2, cacheEntry.getHits());
695 }
696
697
698
699
700 public void testNullCacheEntry() {
701 Ehcache ehcache = new net.sf.ehcache.Cache("testNullCacheEntry", 1, true, false, 5, 2);
702 manager.addCache(ehcache);
703 Cache cache = new JCache(ehcache, null);
704
705 cache.put("key1", "value1");
706
707 CacheEntry cacheEntry = cache.getCacheEntry("key1");
708 assertNotNull(cacheEntry);
709 CacheEntry cacheEntry2 = cache.getCacheEntry("keyDoesNotExist");
710 assertNull(cacheEntry2);
711 }
712
713
714
715
716
717 public void testCacheStatistics() throws Exception {
718 Ehcache ehcache = new net.sf.ehcache.Cache("testCacheStatistics", 1, true, false, 5, 2);
719 manager.addCache(ehcache);
720 Cache cache = new JCache(ehcache, null);
721 cache.put("key1", "value1");
722 cache.put("key2", "value1");
723
724 CacheEntry cacheEntry = cache.getCacheEntry("key1");
725 assertEquals("Should be one", 1, cacheEntry.getHits());
726 assertEquals("Should be one", 1, cache.getCacheStatistics().getCacheHits());
727
728
729 cacheEntry = cache.getCacheEntry("key1");
730 assertEquals("Should be one", 2, cacheEntry.getHits());
731 assertEquals("Should be one", 2, cache.getCacheStatistics().getCacheHits());
732
733 cacheEntry = cache.getCacheEntry("key2");
734 assertEquals("Should be one", 1, cacheEntry.getHits());
735 assertEquals("Should be one", 3, cache.getCacheStatistics().getCacheHits());
736
737 assertEquals("Should be 0", 0, cache.getCacheStatistics().getCacheMisses());
738 cache.get("doesnotexist");
739 assertEquals("Should be 1", 1, cache.getCacheStatistics().getCacheMisses());
740
741
742 }
743
744
745
746
747
748
749
750
751
752
753
754
755 public void testSizeWithPutAndRemove() throws Exception {
756
757 Ehcache ehcache = new net.sf.ehcache.Cache("testSizeWithPutAndRemove", 1, true, true, 0, 0);
758 manager.addCache(ehcache);
759 Cache cache = new JCache(ehcache, null);
760
761 cache.put("key1", "value1");
762 cache.put("key2", "value1");
763
764 int sizeFromGetSize = cache.getCacheStatistics().getObjectCount();
765 int sizeFromKeys = cache.keySet().size();
766 assertEquals(sizeFromGetSize, sizeFromKeys);
767 assertEquals(2, cache.getCacheStatistics().getObjectCount());
768
769
770 class NonSerializable { }
771
772 cache.put("key1", new NonSerializable());
773 cache.put("key1", "value1");
774
775
776 assertEquals(cache.getCacheStatistics().getObjectCount(), cache.keySet().size());
777 assertEquals(2, cache.getCacheStatistics().getObjectCount());
778
779 cache.remove("key1");
780 assertEquals(cache.getCacheStatistics().getObjectCount(), cache.keySet().size());
781 assertEquals(1, cache.getCacheStatistics().getObjectCount());
782 cache.remove("key2");
783 assertEquals(cache.getCacheStatistics().getObjectCount(), cache.keySet().size());
784 assertEquals(0, cache.getCacheStatistics().getObjectCount());
785
786
787 cache.clear();
788 cache.put("nullValue1", null);
789 cache.put("nullValue2", null);
790
791 assertEquals(1, cache.getCacheStatistics().getObjectCount());
792 Object nullValue = cache.get("nullValue2");
793 assertNull(nullValue);
794
795 }
796
797
798
799
800
801
802 public void testGetKeysAfterExpiry() throws Exception {
803
804 Cache cache = getTest2Cache();
805 String key1 = "key1";
806 cache.put(key1, "value1");
807 cache.put("key2", "value1");
808
809 assertEquals(cache.getCacheStatistics().getObjectCount(), cache.keySet().size());
810
811 assertEquals(2, cache.getCacheStatistics().getObjectCount());
812 String keyFromDisk = (String) cache.getCacheEntry(key1).getKey();
813 assertTrue(key1.equals(keyFromDisk));
814 Thread.sleep(1020);
815 assertEquals(2, cache.keySet().size());
816
817 Ehcache ehcache = ((JCache) cache).getBackingCache();
818 ehcache.setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_GUARANTEED);
819 assertEquals(0, cache.getCacheStatistics().getObjectCount());
820 }
821
822
823
824
825 public void testSizeMultipleCallsWithPutAndRemove() throws Exception {
826
827
828 Cache cache = getTest2Cache();
829 cache.put("key1", "value1");
830 cache.put("key2", "value1");
831
832
833 assertEquals(2, cache.getCacheStatistics().getObjectCount());
834 assertEquals(2, cache.getCacheStatistics().getObjectCount());
835 assertEquals(2, cache.getCacheStatistics().getObjectCount());
836 assertEquals(2, cache.getCacheStatistics().getObjectCount());
837 assertEquals(2, cache.getCacheStatistics().getObjectCount());
838 cache.remove("key1");
839 assertEquals(1, cache.getCacheStatistics().getObjectCount());
840 assertEquals(1, cache.getCacheStatistics().getObjectCount());
841 assertEquals(1, cache.getCacheStatistics().getObjectCount());
842 assertEquals(1, cache.getCacheStatistics().getObjectCount());
843 assertEquals(1, cache.getCacheStatistics().getObjectCount());
844 cache.remove("key2");
845 assertEquals(0, cache.getCacheStatistics().getObjectCount());
846 assertEquals(0, cache.getCacheStatistics().getObjectCount());
847 assertEquals(0, cache.getCacheStatistics().getObjectCount());
848 assertEquals(0, cache.getCacheStatistics().getObjectCount());
849 assertEquals(0, cache.getCacheStatistics().getObjectCount());
850 }
851
852
853
854
855
856
857
858
859
860 public void testGetKeysPerformance() throws Exception {
861 Cache cache = getTest2Cache();
862
863 for (int i = 0; i < 2000; i++) {
864 cache.put("key" + i, "value");
865 }
866
867 cache.put("key0", "value");
868 cache.put("key1", "value");
869 cache.put("key2", "value");
870 cache.put("key3", "value");
871 cache.put("key4", "value");
872
873 Thread.sleep(1000);
874 StopWatch stopWatch = new StopWatch();
875 Set keys = cache.keySet();
876 assertTrue("Should be 2000 keys. ", keys.size() == 2000);
877 long getKeysTime = stopWatch.getElapsedTime();
878
879 LOG.info("Time to get 2000 keys: With Duplicate Check: " + getKeysTime);
880 assertTrue("Getting keys took more than 200ms: " + getKeysTime, getKeysTime < 100);
881 }
882
883
884
885
886
887
888
889
890
891
892
893
894 public void testGetSizeAfterExpiry() throws Exception {
895
896 Cache cache = getTest2Cache();
897 cache.put("key1", "value1");
898 cache.put("key2", "value1");
899
900
901 Thread.sleep(1020);
902 assertEquals(null, cache.get("key1"));
903 assertEquals(null, cache.get("key2"));
904
905 assertEquals(0, cache.getCacheStatistics().getObjectCount());
906 }
907
908
909
910
911
912
913
914
915
916
917
918
919
920 public void testNullTreatment() throws Exception {
921 Ehcache ehcache = new net.sf.ehcache.Cache("testNullTreatment", 1, false, false, 5, 1);
922 manager.addCache(ehcache);
923 Cache cache = new JCache(ehcache, null);
924
925 try {
926 cache.put(null, null);
927 assertNull(cache.get(null));
928 cache.put(null, "value");
929 assertEquals("value", cache.get(null));
930 cache.put("key", null);
931 assertEquals(null, cache.get("key"));
932 assertFalse(null instanceof Serializable);
933 } catch (Exception e) {
934 fail("Should not have thrown an Execption");
935 }
936 }
937
938
939
940
941
942 public void testSizes() throws Exception {
943 Cache cache = getTest1Cache();
944 assertEquals(0, cache.getCacheStatistics().getObjectCount());
945
946 for (int i = 0; i < 10010; i++) {
947 cache.put("key" + i, "value1");
948 }
949 assertEquals(10010, cache.getCacheStatistics().getObjectCount());
950
951
952 cache.put(new Object(), Object.class);
953
954 assertEquals(10011, cache.getCacheStatistics().getObjectCount());
955
956 cache.remove("key4");
957 cache.remove("key3");
958
959 assertEquals(10009, cache.getCacheStatistics().getObjectCount());
960
961 cache.clear();
962 assertEquals(0, cache.getCacheStatistics().getObjectCount());
963
964 }
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985 public void testNonSerializableElement() throws Exception {
986 Ehcache ehcache = new net.sf.ehcache.Cache("testElementWithNonSerializableValue", 1, true, false, 100, 200);
987 manager.addCache(ehcache);
988 Cache cache = new JCache(ehcache, null);
989
990 cache.put("key1", new Object());
991 cache.put("key2", new Object());
992
993
994 assertNull(cache.get("key1"));
995
996
997 assertNotNull(cache.get("key2"));
998 }
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012 public void testSpoolThreadHandlesThreadKiller() throws Exception {
1013 Ehcache ehcache = new net.sf.ehcache.Cache("testThreadKiller", 1, true, false, 100, 200);
1014 manager.addCache(ehcache);
1015 Cache cache = new JCache(ehcache, null);
1016
1017 cache.put("key", new ThreadKiller());
1018 cache.put("key1", "one");
1019 cache.put("key2", "two");
1020
1021 Thread.sleep(2000);
1022
1023 assertNotNull(cache.get("key1"));
1024 assertNotNull(cache.get("key2"));
1025 }
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044 public void testToString() throws CacheException {
1045 Cache cache = getTest2Cache();
1046 cache.clear();
1047 LOG.info(cache);
1048 assertTrue(cache.toString().indexOf("test2") > -1);
1049 assertTrue(380 < cache.toString().length());
1050 }
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068 public void testAPIObjectCompatibility() throws CacheException {
1069 Cache cache = getTest1Cache();
1070
1071 Object objectKey = new Object();
1072 Object objectValue = new Object();
1073
1074 cache.put(objectKey, objectValue);
1075
1076
1077 Object retrievedElement = cache.get(objectKey);
1078 assertNotNull(retrievedElement);
1079
1080
1081 assertEquals(objectValue, retrievedElement);
1082
1083 }
1084
1085
1086
1087
1088
1089 public void testAPISerializableCompatibility() throws CacheException {
1090
1091 Cache cache = getTest2Cache();
1092
1093
1094 Serializable key = new String("key");
1095 Serializable serializableValue = new String("retrievedValue");
1096 cache.put(key, serializableValue);
1097 cache.put("key2", serializableValue);
1098 Object retrievedValue = cache.get(key);
1099 assertEquals(serializableValue, retrievedValue);
1100 }
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119 public void testReadWriteThreads() throws Exception {
1120
1121 final int size = 9000;
1122 final int maxTime = (int) (410 * StopWatch.getSpeedAdjustmentFactor());
1123 final Cache cache = getTest1Cache();
1124
1125 long start = System.currentTimeMillis();
1126 final List executables = new ArrayList();
1127 final Random random = new Random();
1128
1129 for (int i = 0; i < size; i++) {
1130 cache.put("" + i, "value");
1131 }
1132
1133
1134 for (int i = 0; i < 30; i++) {
1135 final Executable executable = new Executable() {
1136 public void execute() throws Exception {
1137 final StopWatch stopWatch = new StopWatch();
1138 long start = stopWatch.getElapsedTime();
1139 cache.get("key" + random.nextInt(size));
1140 long end = stopWatch.getElapsedTime();
1141 long elapsed = end - start;
1142 assertTrue("Get time outside of allowed range: " + elapsed, elapsed < maxTime);
1143 }
1144 };
1145 executables.add(executable);
1146 }
1147
1148
1149 for (int i = 0; i < 10; i++) {
1150 final Executable executable = new Executable() {
1151 public void execute() throws Exception {
1152 final StopWatch stopWatch = new StopWatch();
1153 long start = stopWatch.getElapsedTime();
1154 cache.put("key" + random.nextInt(size), "value");
1155 long end = stopWatch.getElapsedTime();
1156 long elapsed = end - start;
1157 assertTrue("Put time outside of allowed range: " + elapsed, elapsed < maxTime);
1158 }
1159 };
1160 executables.add(executable);
1161 }
1162
1163
1164 for (int i = 0; i < 10; i++) {
1165 final Executable executable = new Executable() {
1166 public void execute() throws Exception {
1167 final StopWatch stopWatch = new StopWatch();
1168 long start = stopWatch.getElapsedTime();
1169 cache.remove("key" + random.nextInt(size));
1170 long end = stopWatch.getElapsedTime();
1171 long elapsed = end - start;
1172 assertTrue("Remove time outside of allowed range: " + elapsed, elapsed < maxTime);
1173 }
1174 };
1175 executables.add(executable);
1176 }
1177
1178
1179 for (int i = 0; i < 10; i++) {
1180 final Executable executable = new Executable() {
1181 public void execute() throws Exception {
1182 final StopWatch stopWatch = new StopWatch();
1183 long start = stopWatch.getElapsedTime();
1184 int randomInteger = random.nextInt(20);
1185 if (randomInteger == 3) {
1186 cache.clear();
1187 }
1188 long end = stopWatch.getElapsedTime();
1189 long elapsed = end - start;
1190 assertTrue("RemoveAll time outside of allowed range: " + elapsed, elapsed < maxTime);
1191 }
1192 };
1193 executables.add(executable);
1194 }
1195
1196 runThreads(executables);
1197 long end = System.currentTimeMillis();
1198 LOG.info("Total time for the test: " + (end - start) + " ms");
1199 }
1200
1201
1202
1203
1204 public void testAsynchronousLoad() throws InterruptedException, ExecutionException {
1205
1206 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1207 JCache jcache = new JCache(manager.getCache("sampleCache1"), countingCacheLoader);
1208 ExecutorService executorService = jcache.getExecutorService();
1209
1210
1211 Future future = jcache.asynchronousLoad("key1");
1212 assertFalse(future.isDone());
1213
1214 Object object = future.get();
1215 assertTrue(future.isDone());
1216 assertNull(object);
1217
1218 assertFalse(executorService.isShutdown());
1219
1220 assertEquals(1, jcache.size());
1221 assertEquals(1, countingCacheLoader.getLoadCounter());
1222 }
1223
1224
1225
1226
1227
1228 public void testLoad() throws InterruptedException, ExecutionException, CacheException {
1229
1230 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1231 JCache jcache = new JCache(manager.getCache("sampleCache1"), countingCacheLoader);
1232 ExecutorService executorService = jcache.getExecutorService();
1233
1234 jcache.load("key1");
1235
1236 Thread.sleep(500);
1237
1238 assertFalse(executorService.isShutdown());
1239
1240 assertEquals(1, jcache.size());
1241 assertEquals(1, countingCacheLoader.getLoadCounter());
1242 }
1243
1244
1245
1246
1247
1248 public void testLoadWithDynamicLoaderInjection() throws InterruptedException, ExecutionException, CacheException {
1249
1250
1251 JCache jcache = new JCache(manager.getCache("sampleCache1"), null);
1252 jcache.load("key1");
1253 assertEquals(0, jcache.size());
1254
1255
1256 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1257 jcache.setCacheLoader(countingCacheLoader);
1258
1259 jcache.load("key1");
1260 Thread.sleep(500);
1261
1262 ExecutorService executorService = jcache.getExecutorService();
1263 assertFalse(executorService.isShutdown());
1264
1265 assertEquals(1, jcache.size());
1266 assertEquals(1, countingCacheLoader.getLoadCounter());
1267 }
1268
1269
1270
1271
1272
1273 public void testAsynchronousLoadAll() throws InterruptedException, ExecutionException {
1274
1275 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1276 JCache jcache = new JCache(manager.getCache("sampleCache1"), countingCacheLoader);
1277 ExecutorService executorService = jcache.getExecutorService();
1278
1279 List keys = new ArrayList();
1280 for (int i = 0; i < 1000; i++) {
1281 keys.add(new Integer(i));
1282 }
1283
1284 Future future = jcache.asynchronousLoadAll(keys);
1285 assertFalse(future.isDone());
1286
1287 Object object = future.get();
1288 assertTrue(future.isDone());
1289 assertNull(object);
1290
1291 assertFalse(executorService.isShutdown());
1292
1293 assertEquals(1000, jcache.size());
1294 assertEquals(1000, countingCacheLoader.getLoadAllCounter());
1295 }
1296
1297
1298
1299
1300 public void testLoadAll() throws InterruptedException, ExecutionException, CacheException {
1301
1302 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1303 JCache jcache = new JCache(manager.getCache("sampleCache1"), countingCacheLoader);
1304 ExecutorService executorService = jcache.getExecutorService();
1305
1306
1307 jcache.loadAll(null);
1308
1309 List keys = new ArrayList();
1310 for (int i = 0; i < 999; i++) {
1311 keys.add(new Integer(i));
1312 }
1313
1314 keys.add(null);
1315
1316
1317 jcache.put(new Integer(1), "");
1318
1319 jcache.loadAll(keys);
1320 Thread.sleep((long) (3000 * StopWatch.getSpeedAdjustmentFactor()));
1321
1322 assertFalse(executorService.isShutdown());
1323
1324 assertEquals(1000, jcache.size());
1325 assertEquals(999, countingCacheLoader.getLoadAllCounter());
1326
1327 }
1328
1329
1330
1331
1332
1333 public void testGetAll() throws InterruptedException, ExecutionException, CacheException {
1334
1335 JCache jcache = new JCache(manager.getCache("sampleCache1"), null);
1336
1337
1338 Map map = jcache.getAll(null);
1339 assertNotNull(map);
1340 assertEquals(0, map.size());
1341
1342
1343 CountingCacheLoader countingCacheLoader = new CountingCacheLoader();
1344 jcache.setCacheLoader(countingCacheLoader);
1345 ExecutorService executorService = jcache.getExecutorService();
1346
1347
1348 map = jcache.getAll(null);
1349 assertNotNull(map);
1350 assertEquals(0, map.size());
1351
1352
1353 List keys = new ArrayList();
1354 for (int i = 0; i < 1000; i++) {
1355 keys.add(new Integer(i));
1356 }
1357
1358
1359 jcache.put(new Integer(1), "");
1360
1361 jcache.getAll(keys);
1362
1363 assertFalse(executorService.isShutdown());
1364
1365 assertEquals(1000, jcache.size());
1366 assertEquals(999, countingCacheLoader.getLoadCounter());
1367 }
1368
1369
1370 }