1 |
| package org.jboss.cache.loader; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestSuite; |
5 |
| import org.jboss.cache.CacheImpl; |
6 |
| import org.jboss.cache.DefaultCacheFactory; |
7 |
| import org.jboss.cache.Fqn; |
8 |
| import org.jboss.cache.misc.TestingUtil; |
9 |
| import org.jboss.cache.transaction.DummyTransactionManager; |
10 |
| |
11 |
| import javax.transaction.NotSupportedException; |
12 |
| import javax.transaction.RollbackException; |
13 |
| import javax.transaction.SystemException; |
14 |
| import javax.transaction.Transaction; |
15 |
| import java.io.File; |
16 |
| import java.util.Set; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| public class TxCacheLoaderTest extends AbstractCacheLoaderTestBase |
23 |
| { |
24 |
| CacheImpl cache1, cache2; |
25 |
| private Fqn fqn = Fqn.fromString("/one/two/three"); |
26 |
| |
27 |
4
| protected void setUp() throws Exception
|
28 |
| { |
29 |
4
| super.setUp();
|
30 |
| |
31 |
4
| String tmpLoc = System.getProperty("java.io.tmpdir", "/tmp");
|
32 |
4
| String location = tmpLoc + File.separator + "TxCacheLoaderTest1";
|
33 |
| |
34 |
4
| cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
35 |
4
| cache1.getConfiguration().setCacheMode("repl_sync");
|
36 |
4
| cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
37 |
| |
38 |
4
| cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + location, false, false, false));
|
39 |
| |
40 |
4
| cache1.create();
|
41 |
4
| cache1.start();
|
42 |
| |
43 |
4
| location = tmpLoc + File.separator + "TxCacheLoaderTest2";
|
44 |
| |
45 |
4
| cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
46 |
4
| cache2.getConfiguration().setCacheMode("repl_sync");
|
47 |
4
| cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
|
48 |
4
| cache2.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", "org.jboss.cache.loader.FileCacheLoader", "location=" + location, false, false, false));
|
49 |
4
| cache2.getConfiguration().setLockAcquisitionTimeout(2000);
|
50 |
| |
51 |
4
| cache2.create();
|
52 |
4
| cache2.start();
|
53 |
| } |
54 |
| |
55 |
4
| protected void tearDown() throws Exception
|
56 |
| { |
57 |
4
| super.tearDown();
|
58 |
| |
59 |
| |
60 |
4
| cache1.remove(Fqn.ROOT);
|
61 |
| |
62 |
4
| cache1.stop();
|
63 |
4
| cache1.destroy();
|
64 |
4
| cache2.stop();
|
65 |
4
| cache2.destroy();
|
66 |
| } |
67 |
| |
68 |
| |
69 |
1
| public void testTxPutCommit() throws Exception, NotSupportedException
|
70 |
| { |
71 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
72 |
1
| mgr.begin();
|
73 |
1
| Transaction tx = mgr.getTransaction();
|
74 |
| |
75 |
| |
76 |
1
| cache1.put(fqn, "key1", "val1");
|
77 |
1
| cache1.put("/one/two/three/four", "key2", "val2");
|
78 |
1
| assertNull(cache2.get(fqn, "key1"));
|
79 |
1
| assertNull(cache2.get("/one/two/three/four", "key2"));
|
80 |
1
| tx.commit();
|
81 |
1
| assertNotNull(cache1.getKeys(fqn));
|
82 |
1
| Set children = cache1.getChildrenNames("/one");
|
83 |
1
| assertEquals(1, children.size());
|
84 |
1
| TestingUtil.sleepThread(2000);
|
85 |
1
| assertEquals("val1", cache2.get(fqn, "key1"));
|
86 |
1
| assertEquals("val2", cache2.get("/one/two/three/four", "key2"));
|
87 |
| } |
88 |
| |
89 |
| |
90 |
1
| public void testTxPrepareAndRollback() throws Exception, NotSupportedException
|
91 |
| { |
92 |
1
| final DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
93 |
1
| mgr.begin();
|
94 |
1
| Transaction tx1 = mgr.getTransaction();
|
95 |
| |
96 |
1
| cache1.getConfiguration().setLockAcquisitionTimeout(1500);
|
97 |
1
| cache2.getConfiguration().setLockAcquisitionTimeout(1500);
|
98 |
| |
99 |
| |
100 |
1
| Thread locker = new Thread()
|
101 |
| { |
102 |
| Transaction tx2 = null; |
103 |
| |
104 |
1
| public void run()
|
105 |
| { |
106 |
1
| try
|
107 |
| { |
108 |
1
| mgr.begin();
|
109 |
1
| tx2 = mgr.getTransaction();
|
110 |
1
| cache2.put(fqn, "block-key1", "block-val1");
|
111 |
1
| TestingUtil.sleepThread(5000);
|
112 |
| } |
113 |
| catch (Exception e) |
114 |
| { |
115 |
0
| e.printStackTrace();
|
116 |
| } |
117 |
| finally |
118 |
| { |
119 |
1
| if (tx2 != null)
|
120 |
| { |
121 |
1
| try
|
122 |
| { |
123 |
1
| tx2.rollback();
|
124 |
| } |
125 |
| catch (SystemException e) |
126 |
| { |
127 |
0
| e.printStackTrace();
|
128 |
| } |
129 |
| } |
130 |
| } |
131 |
| } |
132 |
| }; |
133 |
| |
134 |
1
| locker.start();
|
135 |
1
| TestingUtil.sleepThread(1000);
|
136 |
| |
137 |
1
| cache1.put(fqn, "key1", "val1");
|
138 |
1
| cache1.put("/one/two/three/four", "key2", "val2");
|
139 |
| |
140 |
1
| try
|
141 |
| { |
142 |
1
| tx1.commit();
|
143 |
0
| fail("commit() should fail because we cannot acquire the lock on cache2");
|
144 |
| } |
145 |
| catch (RollbackException rollback) |
146 |
| { |
147 |
1
| System.out.println("--- TX was rolled back (as expected)");
|
148 |
1
| assertTrue(true);
|
149 |
| } |
150 |
| |
151 |
1
| assertNull(cache1.get(fqn, "key1"));
|
152 |
1
| assertNull(cache1.get("/one/two/three/four", "key1"));
|
153 |
| |
154 |
| } |
155 |
| |
156 |
| |
157 |
1
| public void testPutAfterTxCommit() throws Exception, NotSupportedException
|
158 |
| { |
159 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
160 |
1
| mgr.begin();
|
161 |
1
| Transaction tx = mgr.getTransaction();
|
162 |
| |
163 |
1
| cache1.put(fqn, "key1", "val1");
|
164 |
1
| assertTrue(cache1.exists(fqn));
|
165 |
1
| tx.commit();
|
166 |
1
| assertTrue(cache1.exists(fqn));
|
167 |
1
| cache1.put("/a/b/c", null);
|
168 |
1
| assertTrue(cache1.exists("/a/b/c"));
|
169 |
| } |
170 |
| |
171 |
1
| public void testPutAfterTxRollback() throws Exception, NotSupportedException
|
172 |
| { |
173 |
1
| DummyTransactionManager mgr = DummyTransactionManager.getInstance();
|
174 |
1
| mgr.begin();
|
175 |
1
| Transaction tx = mgr.getTransaction();
|
176 |
| |
177 |
1
| cache1.put(fqn, "key1", "val1");
|
178 |
1
| assertTrue(cache1.exists(fqn));
|
179 |
1
| tx.rollback();
|
180 |
1
| assertFalse(cache1.getCacheLoader().exists(fqn));
|
181 |
1
| assertFalse(cache1.exists(fqn));
|
182 |
1
| cache1.put("/a/b/c", null);
|
183 |
1
| assertTrue(cache1.exists("/a/b/c"));
|
184 |
| } |
185 |
| |
186 |
1
| public static Test suite()
|
187 |
| { |
188 |
1
| return new TestSuite(TxCacheLoaderTest.class);
|
189 |
| } |
190 |
| |
191 |
0
| public static void main(String[] args)
|
192 |
| { |
193 |
0
| junit.textui.TestRunner.run(suite());
|
194 |
| } |
195 |
| |
196 |
| } |