1 |
| package org.jboss.cache.notifications; |
2 |
| |
3 |
| import junit.framework.Assert; |
4 |
| import org.jboss.cache.Cache; |
5 |
| import org.jboss.cache.DefaultCacheFactory; |
6 |
| import org.jboss.cache.Fqn; |
7 |
| import org.jboss.cache.config.Configuration; |
8 |
| import org.jboss.cache.loader.AbstractCacheLoaderTestBase; |
9 |
| import org.jboss.cache.loader.DummyInMemoryCacheLoader; |
10 |
| import org.jboss.cache.misc.TestingUtil; |
11 |
| import org.jboss.cache.notifications.annotation.*; |
12 |
| import org.jboss.cache.notifications.event.Event; |
13 |
| import org.jboss.cache.transaction.DummyTransactionManagerLookup; |
14 |
| |
15 |
| import javax.transaction.TransactionManager; |
16 |
| import java.util.LinkedList; |
17 |
| import java.util.List; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| public class NotificationThreadTest extends AbstractCacheLoaderTestBase |
26 |
| { |
27 |
| private Cache cache1, cache2; |
28 |
| |
29 |
| private TestCacheListener listener; |
30 |
| |
31 |
8
| protected void setUp() throws Exception
|
32 |
| { |
33 |
| |
34 |
| |
35 |
8
| cache1 = DefaultCacheFactory.getInstance().createCache(false);
|
36 |
8
| cache2 = DefaultCacheFactory.getInstance().createCache(false);
|
37 |
8
| cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
38 |
8
| cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
|
39 |
8
| cache1.getConfiguration().setSyncCommitPhase(true);
|
40 |
8
| cache2.getConfiguration().setSyncCommitPhase(true);
|
41 |
8
| cache1.getConfiguration().setSyncRollbackPhase(true);
|
42 |
8
| cache2.getConfiguration().setSyncRollbackPhase(true);
|
43 |
8
| cache1.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
44 |
8
| cache2.getConfiguration().setTransactionManagerLookupClass(DummyTransactionManagerLookup.class.getName());
|
45 |
8
| cache1.getConfiguration().setCacheLoaderConfig(getSingleCacheLoaderConfig("", DummyInMemoryCacheLoader.class.getName(), null, false, false, false));
|
46 |
| |
47 |
8
| listener = new TestCacheListener();
|
48 |
8
| cache1.addCacheListener(listener);
|
49 |
| } |
50 |
| |
51 |
8
| protected void tearDown()
|
52 |
| { |
53 |
8
| cache1.stop();
|
54 |
8
| cache2.stop();
|
55 |
| } |
56 |
| |
57 |
1
| public void testPessimisticWithCacheLoader() throws Throwable
|
58 |
| { |
59 |
1
| doTest(false);
|
60 |
| } |
61 |
| |
62 |
1
| public void testOptimisticWithCacheLoader() throws Throwable
|
63 |
| { |
64 |
1
| cache1.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
65 |
1
| cache2.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
66 |
1
| doTest(false);
|
67 |
| } |
68 |
| |
69 |
1
| public void testPessimisticWithPassivation() throws Throwable
|
70 |
| { |
71 |
1
| cache1.getConfiguration().getCacheLoaderConfig().setPassivation(true);
|
72 |
1
| doTest(false);
|
73 |
| } |
74 |
| |
75 |
1
| public void testOptimisticWithPassivation() throws Throwable
|
76 |
| { |
77 |
1
| cache1.getConfiguration().getCacheLoaderConfig().setPassivation(true);
|
78 |
1
| cache1.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
79 |
1
| cache2.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
80 |
1
| doTest(false);
|
81 |
| } |
82 |
| |
83 |
1
| public void testPessimisticWithCacheLoaderTx() throws Throwable
|
84 |
| { |
85 |
1
| doTest(true);
|
86 |
| } |
87 |
| |
88 |
1
| public void testOptimisticWithCacheLoaderTx() throws Throwable
|
89 |
| { |
90 |
1
| cache1.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
91 |
1
| cache2.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
92 |
1
| doTest(true);
|
93 |
| } |
94 |
| |
95 |
1
| public void testPessimisticWithPassivationTx() throws Throwable
|
96 |
| { |
97 |
1
| cache1.getConfiguration().getCacheLoaderConfig().setPassivation(true);
|
98 |
1
| doTest(true);
|
99 |
| } |
100 |
| |
101 |
1
| public void testOptimisticWithPassivationTx() throws Throwable
|
102 |
| { |
103 |
1
| cache1.getConfiguration().getCacheLoaderConfig().setPassivation(true);
|
104 |
1
| cache1.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
105 |
1
| cache2.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
106 |
1
| doTest(true);
|
107 |
| } |
108 |
| |
109 |
| |
110 |
8
| private void doTest(boolean tx) throws Throwable
|
111 |
| { |
112 |
| |
113 |
8
| cache1.stop();
|
114 |
| |
115 |
8
| cache1.start();
|
116 |
8
| cache2.start();
|
117 |
| |
118 |
8
| TransactionManager tm = cache1.getConfiguration().getRuntimeConfig().getTransactionManager();
|
119 |
| |
120 |
8
| listener.sameThreadExpected = true;
|
121 |
8
| listener.mainThread = Thread.currentThread();
|
122 |
8
| Fqn<String> fqn = Fqn.fromString("/a/b/c");
|
123 |
| |
124 |
| |
125 |
4
| if (tx) tm.begin();
|
126 |
8
| cache1.put(fqn, "k", "v");
|
127 |
4
| if (tx) tm.commit();
|
128 |
4
| if (tx) tm.begin();
|
129 |
8
| cache1.get(fqn, "k");
|
130 |
4
| if (tx) tm.commit();
|
131 |
4
| if (tx) tm.begin();
|
132 |
8
| cache1.put(fqn, "k", "v2");
|
133 |
4
| if (tx) tm.commit();
|
134 |
4
| if (tx) tm.begin();
|
135 |
8
| cache1.removeNode(fqn);
|
136 |
4
| if (tx) tm.commit();
|
137 |
4
| if (tx) tm.begin();
|
138 |
8
| cache1.put(fqn, "k", "v3");
|
139 |
4
| if (tx) tm.commit();
|
140 |
4
| if (tx) tm.begin();
|
141 |
| |
142 |
8
| cache1.evict(fqn, true);
|
143 |
4
| if (tx) tm.commit();
|
144 |
4
| if (tx) tm.begin();
|
145 |
| |
146 |
8
| cache1.get(fqn, "k");
|
147 |
4
| if (tx) tm.commit();
|
148 |
4
| if (tx) tm.begin();
|
149 |
| |
150 |
8
| cache1.move(fqn, Fqn.ROOT);
|
151 |
4
| if (tx) tm.commit();
|
152 |
| |
153 |
| |
154 |
8
| listener.sameThreadExpected = false;
|
155 |
8
| cache2.stop();
|
156 |
| |
157 |
| |
158 |
8
| TestingUtil.sleepThread(500);
|
159 |
| |
160 |
| |
161 |
0
| for (Throwable e : listener.exceptions) throw e;
|
162 |
| } |
163 |
| |
164 |
| |
165 |
| @CacheListener |
166 |
| public class TestCacheListener |
167 |
| { |
168 |
| boolean sameThreadExpected; |
169 |
| Thread mainThread; |
170 |
| List<Throwable> exceptions = new LinkedList<Throwable>(); |
171 |
| |
172 |
432
| @NodeCreated
|
173 |
| @NodeModified |
174 |
| @NodeRemoved |
175 |
| @NodeVisited |
176 |
| @NodeEvicted |
177 |
| @NodeLoaded |
178 |
| @NodeMoved |
179 |
| @NodeActivated |
180 |
| @NodePassivated |
181 |
| @CacheStarted |
182 |
| @CacheStopped |
183 |
| @ViewChanged |
184 |
| @TransactionCompleted |
185 |
| @TransactionRegistered |
186 |
| public void testCallbackThread(Event e) |
187 |
| { |
188 |
432
| try
|
189 |
| { |
190 |
432
| if (sameThreadExpected)
|
191 |
392
| Assert.assertSame(mainThread, Thread.currentThread());
|
192 |
| else |
193 |
40
| Assert.assertNotSame(mainThread, Thread.currentThread());
|
194 |
| } |
195 |
| catch (Throwable t) |
196 |
| { |
197 |
8
| exceptions.add(t);
|
198 |
| } |
199 |
| } |
200 |
| } |
201 |
| |
202 |
| } |