KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > lock > BreakDeadMemberLocksTest


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22
23 package org.jboss.cache.lock;
24
25 import junit.framework.TestCase;
26 import org.jboss.cache.CacheImpl;
27 import org.jboss.cache.factories.XmlConfigurationParser;
28 import org.jboss.cache.misc.TestingUtil;
29
30 import javax.transaction.Synchronization JavaDoc;
31 import javax.transaction.Transaction JavaDoc;
32 import javax.transaction.TransactionManager JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Set JavaDoc;
36
37 /**
38  * Tests the breaking of locks held by dead members.
39  *
40  * @author <a HREF="mailto://brian.stansberry@jboss.com">Brian Stansberry</a>
41  * @version $Revision$
42  */

43 public class BreakDeadMemberLocksTest extends TestCase
44 {
45
46    private Map JavaDoc caches;
47
48    protected void setUp() throws Exception JavaDoc
49    {
50       super.setUp();
51
52       caches = new HashMap JavaDoc();
53    }
54
55    public void testBreakDeadMemberLocks() throws Exception JavaDoc
56    {
57       CacheImpl cacheA = createCache("A");
58
59       cacheA.put("/1/A", "1", "A");
60       cacheA.put("/1/A", "2", "A");
61       cacheA.put("/2/A", "1", "A");
62       cacheA.put("/2/A", "2", "A");
63       cacheA.put("/1/A/I", "1", "A");
64       cacheA.put("/1/A/I", "2", "A");
65
66       CacheImpl cacheB = createCache("B");
67
68       // Pause to give caches time to see each other
69
TestingUtil.blockUntilViewsReceived(new CacheImpl[]{cacheA, cacheB}, 60000);
70
71       final TransactionManager JavaDoc tm = cacheB.getTransactionManager();
72       tm.begin();
73       final Transaction JavaDoc tx = tm.getTransaction();
74
75       cacheB.put("/1/A", "1", "B");
76       cacheB.put("/1/A", "2", "B");
77       cacheB.put("/2/A", "1", "B");
78       cacheB.put("/2/A", "2", "B");
79       cacheB.put("/1/A/I", "1", "B");
80       cacheB.put("/1/A/I", "2", "B");
81       cacheB.put("/EXISTS", "KEY", "B");
82
83       Object JavaDoc monitor = new Object JavaDoc();
84       HangSync sync = new HangSync(monitor);
85       tx.registerSynchronization(sync);
86
87       Thread JavaDoc t = new Thread JavaDoc()
88       {
89          public void run()
90          {
91             try
92             {
93                tm.resume(tx);
94                tx.commit();
95             }
96             catch (Exception JavaDoc e) {}
97          }
98       };
99
100       synchronized (monitor)
101       {
102          t.start();
103
104          while (!sync.hung)
105             monitor.wait(500);
106       }
107
108       tm.suspend();
109
110       // Confirm that B's tx replicated
111
assertTrue(cacheA.exists("/EXISTS"));
112
113       cacheB.stop();
114       cacheB.destroy();
115
116       while (cacheA.getMembers().size() > 1)
117       {
118          try
119          {
120             Thread.sleep(100);
121          }
122          catch (InterruptedException JavaDoc e) {}
123       }
124
125       assertEquals("A", cacheA.get("/1/A", "1"));
126       assertEquals("A", cacheA.get("/1/A", "2"));
127       assertEquals("A", cacheA.get("/2/A", "1"));
128       assertEquals("A", cacheA.get("/2/A", "2"));
129       assertEquals("A", cacheA.get("/1/A/I", "1"));
130       assertEquals("A", cacheA.get("/1/A/I", "2"));
131
132       if (t.isAlive())
133       {
134          t.interrupt();
135       }
136    }
137
138    protected CacheImpl createCache(String JavaDoc cacheID) throws Exception JavaDoc
139    {
140       if (caches.get(cacheID) != null)
141          throw new IllegalStateException JavaDoc(cacheID + " already created");
142
143       CacheImpl cache = new CacheImpl();
144       cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml"));
145
146       cache.create();
147       cache.start();
148
149       caches.put(cacheID, cache);
150
151       return cache;
152    }
153
154    protected void tearDown() throws Exception JavaDoc
155    {
156       super.tearDown();
157
158       Set JavaDoc keys = caches.keySet();
159       String JavaDoc[] cacheIDs = new String JavaDoc[keys.size()];
160       cacheIDs = (String JavaDoc[]) keys.toArray(cacheIDs);
161       for (int i = 0; i < cacheIDs.length; i++)
162       {
163          stopCache(cacheIDs[i]);
164       }
165    }
166
167    protected void stopCache(String JavaDoc id)
168    {
169       CacheImpl cache = (CacheImpl) caches.get(id);
170       if (cache != null)
171       {
172          try
173          {
174             cache.stop();
175             cache.destroy();
176             caches.remove(id);
177          }
178          catch (Exception JavaDoc e)
179          {
180             System.out.println("Exception stopping cache " + e.getMessage());
181             e.printStackTrace(System.out);
182          }
183       }
184    }
185
186    class HangSync implements Synchronization JavaDoc
187    {
188       private boolean hung = false;
189       private Object JavaDoc monitor;
190
191       HangSync(Object JavaDoc monitor)
192       {
193          this.monitor = monitor;
194       }
195
196       public void afterCompletion(int arg0)
197       {
198       }
199
200       public void beforeCompletion()
201       {
202          hung = true;
203          synchronized (monitor)
204          {
205             monitor.notifyAll();
206          }
207          try
208          {
209             Thread.sleep(30000);
210          }
211          catch (InterruptedException JavaDoc e) {}
212       }
213
214
215    }
216 }
217
Popular Tags