KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > common > util > TwoStageCacheTest


1 package org.objectweb.celtix.common.util;
2
3 import java.util.LinkedList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import junit.framework.TestCase;
7
8 public class TwoStageCacheTest extends TestCase {
9
10     public TwoStageCacheTest(String JavaDoc arg0) {
11         super(arg0);
12     }
13
14     public void testToString() {
15         TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
16         assertEquals("AbstractTwoStageCache", cache.toString());
17     }
18     
19     /*
20      * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.get()'
21      */

22     public void testGet() throws Throwable JavaDoc {
23         TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
24         cache.populateCache();
25
26         for (int x = 0; x < 10; x++) {
27             assertNotNull(cache.get());
28         }
29         
30         cache = new TestTwoStageCache(3, 5, 5);
31         cache.populateCache();
32
33         for (int x = 0; x < 10; x++) {
34             assertNotNull(cache.get());
35         }
36     }
37
38     /*
39      * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.poll()'
40      */

41     public void testPoll() throws Throwable JavaDoc {
42         TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
43         cache.populateCache();
44         int count = 0;
45         while (cache.poll() != null) {
46             count++;
47         }
48         assertEquals(0, count);
49         
50         cache = new TestTwoStageCache(3, 5, 3);
51         cache.populateCache();
52         count = 0;
53         while (cache.poll() != null) {
54             count++;
55         }
56         assertEquals(3, count);
57
58         cache = new TestTwoStageCache(3, 5, 5);
59         cache.populateCache();
60         count = 0;
61         while (cache.poll() != null) {
62             count++;
63         }
64         assertEquals(5, count);
65
66         // try to prealloc more than high water mark...
67
cache = new TestTwoStageCache(3, 5, 9);
68         cache.populateCache();
69         count = 0;
70         while (cache.poll() != null) {
71             count++;
72         }
73         assertEquals(5, count);
74         
75         
76     }
77
78     /*
79      * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.recycle(E)'
80      */

81     public void testRecycle() throws Throwable JavaDoc {
82         TestTwoStageCache cache = new TestTwoStageCache(3, 8, 5, new Object JavaDoc());
83         cache.populateCache();
84
85         Object JavaDoc objs[] = new Object JavaDoc[10];
86         
87         for (int x = 0; x < 10; x++) {
88             objs[x] = cache.get();
89         }
90         for (int x = 0; x < 10; x++) {
91             cache.recycle(objs[x]);
92         }
93         int count = 0;
94         while (cache.poll() != null) {
95             count++;
96         }
97         assertEquals(8, count);
98
99         count = 0;
100         for (int x = 0; x < 10; x++) {
101             cache.recycle(objs[x]);
102             objs[x] = null;
103             System.gc();
104         }
105         objs = null;
106         
107         
108         
109         List JavaDoc<byte[]> list = new LinkedList JavaDoc<byte[]>();
110         int allocCount = 0;
111         try {
112             while (allocCount++ < 1000) {
113                 System.gc();
114                 long memFree = Runtime.getRuntime().freeMemory();
115                 int memToAlloc = memFree > 512 * 1024 * 1024
116                                     ? 512 * 1024 * 1024 : (int)memFree;
117                 list.add(new byte[memToAlloc]);
118             }
119             fail("cannot trigger OutOfMemoryError within a reasonable timeframe");
120         } catch (OutOfMemoryError JavaDoc ex) {
121             System.gc();
122             list = null;
123             System.gc();
124         }
125         cache.recycle(cache.create());
126         
127         System.gc();
128         while (cache.poll() != null) {
129             count++;
130         }
131         assertEquals(4, count);
132     
133     }
134
135     
136     static class TestTwoStageCache extends AbstractTwoStageCache<Object JavaDoc> {
137         public TestTwoStageCache(int pCacheSize, int highWaterMark, int prealloc) {
138             super(pCacheSize, highWaterMark, prealloc);
139         }
140         public TestTwoStageCache(int pCacheSize, int highWaterMark,
141                                  int prealloc, Object JavaDoc mutex) {
142             super(pCacheSize, highWaterMark, prealloc, mutex);
143         }
144         public Object JavaDoc create() {
145             return new Object JavaDoc();
146         }
147     }
148 }
149
Popular Tags