KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > loader > AsyncFileCacheLoaderTest


1 package org.jboss.cache.loader;
2
3 import junit.framework.Assert;
4 import junit.framework.Test;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.CacheException;
7 import org.jboss.cache.CacheImpl;
8 import org.jboss.cache.Fqn;
9 import org.jboss.cache.Modification;
10 import org.jboss.cache.config.Configuration;
11 import org.jboss.cache.statetransfer.StateTransferManager;
12 import org.jboss.util.stream.MarshalledValueInputStream;
13 import org.jboss.util.stream.MarshalledValueOutputStream;
14
15 import java.io.ByteArrayInputStream JavaDoc;
16 import java.io.ByteArrayOutputStream JavaDoc;
17 import java.util.Collections JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 public class AsyncFileCacheLoaderTest extends AbstractCacheLoaderTestBase
21 {
22
23    private CacheImpl cache;
24
25    protected void configureCache() throws Exception JavaDoc
26    {
27       configureCache("");
28    }
29
30    protected void configureCache(String JavaDoc props) throws Exception JavaDoc
31    {
32       cache = new CacheImpl();
33       cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
34       // cache.setCacheLoaderConfiguration(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.jdbm.JdbmCacheLoader",
35
cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader",
36               props, true, false, true));
37       cache.create();
38       cache.start();
39    }
40
41    public static Test suite()
42    {
43       return new TestSuite(AsyncFileCacheLoaderTest.class);
44    }
45
46    protected void tearDown() throws Exception JavaDoc
47    {
48       if (cache != null)
49       {
50          cache.stop();
51       }
52    }
53
54    public void testRestrictionOnAddingToQueue() throws Exception JavaDoc
55    {
56       configureCache();
57       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
58       loader.remove(Fqn.fromString("/blah"));
59
60       loader.put(Fqn.fromString("/blah"), "one", "two");
61       loader.put(Fqn.fromString("/blah"), "three", "four");
62       loader.put(Fqn.fromString("/blah"), "five", "six");
63       loader.put(Fqn.fromString("/blah"), "seven", "eight");
64
65       // stop the cache loader
66
loader.stop();
67       try
68       {
69          loader.remove(Fqn.fromString("/blah"));
70          Assert.assertTrue("Should have restricted this entry from being made", false);
71       }
72       catch (CacheException e)
73       {
74          Assert.assertTrue(true);
75       }
76
77       // clean up
78
loader.start();
79       loader.remove(Fqn.fromString("/blah"));
80    }
81
82    public void testPutImmediate() throws Exception JavaDoc
83    {
84       configureCache(
85               "cache.async.put=false\n" +
86                       "cache.async.pollWait=10000\n" +
87                       "");
88       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
89       Fqn fqn = Fqn.fromString("/a/b/c/d");
90       HashMap JavaDoc map = new HashMap JavaDoc();
91       map.put("c", "d");
92       // Three kinds of puts!
93
Modification mod = new Modification(Modification.ModificationType.PUT_KEY_VALUE, fqn, "e", "f");
94       loader.put(fqn, "a", "b");
95       loader.put(fqn, map);
96       loader.put(Collections.singletonList(mod));
97       assertEquals("put right away", 3, loader.get(fqn).size());
98       loader.remove(fqn);
99    }
100
101    public void testBounded() throws Exception JavaDoc
102    {
103       configureCache(
104               "cache.async.queueSize=1\n" +
105                       "cache.async.pollWait=10\n" +
106                       "");
107       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
108       Fqn fqn = Fqn.fromString("/bound");
109       loader.remove(fqn);
110       // You can't really see it block though :-/
111
for (int i = 0; i < 100; i++)
112       {
113          cache.put(fqn, "key" + i, "value1");
114       }
115       Thread.sleep(1000);
116       assertEquals(100, loader.get(fqn).size());
117       loader.remove(fqn);
118    }
119
120    public void testNoReturnOld() throws Exception JavaDoc
121    {
122       configureCache(
123               "cache.async.returnOld=false\n" +
124                       "cache.async.pollWait=10\n" +
125                       "");
126       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
127       System.out.println("Loader " + loader);
128       cache.put(Fqn.ROOT, "key1", "value1");
129       Thread.sleep(100);
130       assertEquals(null, loader.put(Fqn.ROOT, "key1", "value1"));
131       assertEquals(null, loader.remove(Fqn.ROOT, "key1"));
132       loader.remove(Fqn.ROOT);
133    }
134
135    public void testStoreState() throws Exception JavaDoc
136    {
137       configureCache();
138       Fqn X = Fqn.fromString("/x");
139       CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
140       loader.remove(X);
141       cache.put(X, "key1", "value1");
142       Thread.sleep(1000);
143       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc(1024);
144       MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos);
145       loader.loadEntireState(os);
146       os.writeObject(StateTransferManager.STREAMING_DELIMETER_NODE);
147       //os.close();
148
assertTrue(baos.size() > 0);
149       loader.remove(X);
150
151       ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
152       MarshalledValueInputStream is = new MarshalledValueInputStream(bais);
153       loader.storeEntireState(is);
154       //is.close();
155
assertEquals("X found", true, loader.exists(X));
156       loader.remove(X);
157    }
158
159    /*public void testWritesGetWritten() throws Exception
160    {
161        configureCache();
162        Fqn fqn = Fqn.fromString("/blah");
163        CacheLoader loader = cache.getCacheLoader();
164        loader.remove(fqn);
165        // a bit brute-force,unfortunately ...
166        // enqueue a crap load of writes...
167        int numPuts = 750;
168        long start = System.currentTimeMillis();
169        for (int i=0; i<numPuts; i++)
170            cache.put(fqn, "key" + i, "value" + i);
171
172        System.out.println("Finished " + numPuts + " in " + (System.currentTimeMillis() - start) + "ms");
173        // stop the cache loader
174        // blocks till pending writes are completed
175        start = System.currentTimeMillis();
176        loader.stop();
177        System.out.println("Finished stopping the loader in " + (System.currentTimeMillis() - start) + "ms");
178        System.out.println(loader);
179
180        // the loader should have written everything to file now.
181        loader.start();
182        Map m = loader.get(fqn);
183        for (int i=0;i<numPuts; i++)
184        {
185            Assert.assertEquals("value" + i, m.get("key" + i));
186        }
187
188        // clean up
189        loader.remove(fqn);
190    }*/

191
192 }
193
Popular Tags