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.DefaultCacheFactory; |
9 |
| import org.jboss.cache.Fqn; |
10 |
| import org.jboss.cache.Modification; |
11 |
| import org.jboss.cache.config.Configuration; |
12 |
| import org.jboss.cache.statetransfer.StateTransferManager; |
13 |
| import org.jboss.util.stream.MarshalledValueInputStream; |
14 |
| import org.jboss.util.stream.MarshalledValueOutputStream; |
15 |
| |
16 |
| import java.io.ByteArrayInputStream; |
17 |
| import java.io.ByteArrayOutputStream; |
18 |
| import java.util.Collections; |
19 |
| import java.util.HashMap; |
20 |
| |
21 |
| public class AsyncFileCacheLoaderTest extends AbstractCacheLoaderTestBase |
22 |
| { |
23 |
| |
24 |
| private CacheImpl cache; |
25 |
| |
26 |
2
| protected void configureCache() throws Exception
|
27 |
| { |
28 |
2
| configureCache("");
|
29 |
| } |
30 |
| |
31 |
5
| protected void configureCache(String props) throws Exception
|
32 |
| { |
33 |
5
| cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
34 |
5
| cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
|
35 |
| |
36 |
5
| cache.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", props, true, false, true));
|
37 |
5
| cache.create();
|
38 |
5
| cache.start();
|
39 |
| } |
40 |
| |
41 |
1
| public static Test suite()
|
42 |
| { |
43 |
1
| return new TestSuite(AsyncFileCacheLoaderTest.class);
|
44 |
| } |
45 |
| |
46 |
5
| protected void tearDown() throws Exception
|
47 |
| { |
48 |
5
| if (cache != null)
|
49 |
| { |
50 |
5
| cache.stop();
|
51 |
| } |
52 |
| } |
53 |
| |
54 |
1
| public void testRestrictionOnAddingToQueue() throws Exception
|
55 |
| { |
56 |
1
| configureCache();
|
57 |
1
| CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
|
58 |
1
| loader.remove(Fqn.fromString("/blah"));
|
59 |
| |
60 |
1
| loader.put(Fqn.fromString("/blah"), "one", "two");
|
61 |
1
| loader.put(Fqn.fromString("/blah"), "three", "four");
|
62 |
1
| loader.put(Fqn.fromString("/blah"), "five", "six");
|
63 |
1
| loader.put(Fqn.fromString("/blah"), "seven", "eight");
|
64 |
| |
65 |
| |
66 |
1
| loader.stop();
|
67 |
1
| try
|
68 |
| { |
69 |
1
| loader.remove(Fqn.fromString("/blah"));
|
70 |
0
| Assert.assertTrue("Should have restricted this entry from being made", false);
|
71 |
| } |
72 |
| catch (CacheException e) |
73 |
| { |
74 |
1
| Assert.assertTrue(true);
|
75 |
| } |
76 |
| |
77 |
| |
78 |
1
| loader.start();
|
79 |
1
| loader.remove(Fqn.fromString("/blah"));
|
80 |
| } |
81 |
| |
82 |
1
| public void testPutImmediate() throws Exception
|
83 |
| { |
84 |
1
| configureCache(
|
85 |
| "cache.async.put=false\n" + |
86 |
| "cache.async.pollWait=10000\n" + |
87 |
| ""); |
88 |
1
| CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
|
89 |
1
| Fqn fqn = Fqn.fromString("/a/b/c/d");
|
90 |
1
| HashMap map = new HashMap();
|
91 |
1
| map.put("c", "d");
|
92 |
| |
93 |
1
| Modification mod = new Modification(Modification.ModificationType.PUT_KEY_VALUE, fqn, "e", "f");
|
94 |
1
| loader.put(fqn, "a", "b");
|
95 |
1
| loader.put(fqn, map);
|
96 |
1
| loader.put(Collections.singletonList(mod));
|
97 |
1
| assertEquals("put right away", 3, loader.get(fqn).size());
|
98 |
1
| loader.remove(fqn);
|
99 |
| } |
100 |
| |
101 |
1
| public void testBounded() throws Exception
|
102 |
| { |
103 |
1
| configureCache(
|
104 |
| "cache.async.queueSize=1\n" + |
105 |
| "cache.async.pollWait=10\n" + |
106 |
| ""); |
107 |
1
| CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
|
108 |
1
| Fqn fqn = Fqn.fromString("/bound");
|
109 |
1
| loader.remove(fqn);
|
110 |
| |
111 |
1
| for (int i = 0; i < 100; i++)
|
112 |
| { |
113 |
100
| cache.put(fqn, "key" + i, "value1");
|
114 |
| } |
115 |
1
| Thread.sleep(1000);
|
116 |
1
| assertEquals(100, loader.get(fqn).size());
|
117 |
1
| loader.remove(fqn);
|
118 |
| } |
119 |
| |
120 |
1
| public void testNoReturnOld() throws Exception
|
121 |
| { |
122 |
1
| configureCache(
|
123 |
| "cache.async.returnOld=false\n" + |
124 |
| "cache.async.pollWait=10\n" + |
125 |
| ""); |
126 |
1
| CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
|
127 |
1
| System.out.println("Loader " + loader);
|
128 |
1
| cache.put(Fqn.ROOT, "key1", "value1");
|
129 |
1
| Thread.sleep(100);
|
130 |
1
| assertEquals(null, loader.put(Fqn.ROOT, "key1", "value1"));
|
131 |
1
| assertEquals(null, loader.remove(Fqn.ROOT, "key1"));
|
132 |
1
| loader.remove(Fqn.ROOT);
|
133 |
| } |
134 |
| |
135 |
1
| public void testStoreState() throws Exception
|
136 |
| { |
137 |
1
| configureCache();
|
138 |
1
| Fqn X = Fqn.fromString("/x");
|
139 |
1
| CacheLoader loader = cache.getCacheLoaderManager().getCacheLoader();
|
140 |
1
| loader.remove(X);
|
141 |
1
| cache.put(X, "key1", "value1");
|
142 |
1
| Thread.sleep(1000);
|
143 |
1
| ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
144 |
1
| MarshalledValueOutputStream os = new MarshalledValueOutputStream(baos);
|
145 |
1
| loader.loadEntireState(os);
|
146 |
1
| cache.getMarshaller().objectToObjectStream(StateTransferManager.STREAMING_DELIMITER_NODE, os);
|
147 |
| |
148 |
1
| assertTrue(baos.size() > 0);
|
149 |
1
| loader.remove(X);
|
150 |
| |
151 |
1
| ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
152 |
1
| MarshalledValueInputStream is = new MarshalledValueInputStream(bais);
|
153 |
1
| loader.storeEntireState(is);
|
154 |
| |
155 |
1
| assertEquals("X found", true, loader.exists(X));
|
156 |
1
| loader.remove(X);
|
157 |
| } |
158 |
| } |