001: package org.objectweb.celtix.common.util;
002:
003: import java.util.LinkedList;
004: import java.util.List;
005:
006: import junit.framework.TestCase;
007:
008: public class TwoStageCacheTest extends TestCase {
009:
010: public TwoStageCacheTest(String arg0) {
011: super (arg0);
012: }
013:
014: public void testToString() {
015: TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
016: assertEquals("AbstractTwoStageCache", cache.toString());
017: }
018:
019: /*
020: * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.get()'
021: */
022: public void testGet() throws Throwable {
023: TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
024: cache.populateCache();
025:
026: for (int x = 0; x < 10; x++) {
027: assertNotNull(cache.get());
028: }
029:
030: cache = new TestTwoStageCache(3, 5, 5);
031: cache.populateCache();
032:
033: for (int x = 0; x < 10; x++) {
034: assertNotNull(cache.get());
035: }
036: }
037:
038: /*
039: * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.poll()'
040: */
041: public void testPoll() throws Throwable {
042: TestTwoStageCache cache = new TestTwoStageCache(3, 5, 0);
043: cache.populateCache();
044: int count = 0;
045: while (cache.poll() != null) {
046: count++;
047: }
048: assertEquals(0, count);
049:
050: cache = new TestTwoStageCache(3, 5, 3);
051: cache.populateCache();
052: count = 0;
053: while (cache.poll() != null) {
054: count++;
055: }
056: assertEquals(3, count);
057:
058: cache = new TestTwoStageCache(3, 5, 5);
059: cache.populateCache();
060: count = 0;
061: while (cache.poll() != null) {
062: count++;
063: }
064: assertEquals(5, count);
065:
066: // try to prealloc more than high water mark...
067: cache = new TestTwoStageCache(3, 5, 9);
068: cache.populateCache();
069: count = 0;
070: while (cache.poll() != null) {
071: count++;
072: }
073: assertEquals(5, count);
074:
075: }
076:
077: /*
078: * Test method for 'org.objectweb.celtix.common.util.AbstractTwoStageCache.recycle(E)'
079: */
080: public void testRecycle() throws Throwable {
081: TestTwoStageCache cache = new TestTwoStageCache(3, 8, 5,
082: new Object());
083: cache.populateCache();
084:
085: Object objs[] = new Object[10];
086:
087: for (int x = 0; x < 10; x++) {
088: objs[x] = cache.get();
089: }
090: for (int x = 0; x < 10; x++) {
091: cache.recycle(objs[x]);
092: }
093: int count = 0;
094: while (cache.poll() != null) {
095: count++;
096: }
097: assertEquals(8, count);
098:
099: 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: List<byte[]> list = new LinkedList<byte[]>();
108: int allocCount = 0;
109: try {
110: while (allocCount++ < 1000) {
111: System.gc();
112: long memFree = Runtime.getRuntime().freeMemory();
113: int memToAlloc = memFree > 512 * 1024 * 1024 ? 512 * 1024 * 1024
114: : (int) memFree;
115: list.add(new byte[memToAlloc]);
116: }
117: fail("cannot trigger OutOfMemoryError within a reasonable timeframe");
118: } catch (OutOfMemoryError ex) {
119: System.gc();
120: list = null;
121: System.gc();
122: }
123: cache.recycle(cache.create());
124:
125: System.gc();
126: while (cache.poll() != null) {
127: count++;
128: }
129: assertEquals(4, count);
130:
131: }
132:
133: static class TestTwoStageCache extends
134: AbstractTwoStageCache<Object> {
135: public TestTwoStageCache(int pCacheSize, int highWaterMark,
136: int prealloc) {
137: super (pCacheSize, highWaterMark, prealloc);
138: }
139:
140: public TestTwoStageCache(int pCacheSize, int highWaterMark,
141: int prealloc, Object mutex) {
142: super (pCacheSize, highWaterMark, prealloc, mutex);
143: }
144:
145: public Object create() {
146: return new Object();
147: }
148: }
149: }
|