KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > runtime > collection > TestThinLock


1 /**
2  * Copyright (C) 2001-2005 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  *
19  *
20  * Authors: S.Chassande-Barrioz.
21  * Created on 17 févr. 2005
22  *
23  */

24 package org.objectweb.speedo.runtime.collection;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Properties JavaDoc;
32
33 import javax.jdo.PersistenceManager;
34 import javax.jdo.PersistenceManagerFactory;
35
36 import org.objectweb.speedo.SpeedoTestHelper;
37 import org.objectweb.speedo.api.SpeedoProperties;
38 import org.objectweb.speedo.pobjects.collection.Group;
39 import org.objectweb.speedo.pobjects.collection.User;
40 import org.objectweb.util.monolog.api.BasicLevel;
41
42 /**
43  * It tests the thin locking on a collection.
44  *
45  * @author S.Chassande-Barrioz
46  */

47 public class TestThinLock extends SpeedoTestHelper {
48
49     /**
50      * @param s
51      */

52     public TestThinLock(String JavaDoc s) {
53         super(s);
54     }
55
56     /**
57      * @see org.objectweb.speedo.SpeedoTestHelper#getLoggerName()
58      */

59     protected String JavaDoc getLoggerName() {
60         return LOG_NAME + ".rt.collection.TestThinLock";
61     }
62
63     /**
64      * Specify the properties activating the thin lock for the Group.users
65      * collection field.
66      * @see org.objectweb.speedo.SpeedoTestHelper#getPMFProperties()
67      */

68     public Properties JavaDoc getPMFProperties() {
69         Properties JavaDoc p = super.getPMFProperties();
70         p.put(SpeedoProperties.TRANSACTION_LOCKING_LEVEL_ENABLETHIN, "true");
71         p.put(SpeedoProperties.TRANSACTION_LOCKING_LEVEL + "("
72                 + Group.class.getName() + "#users)",
73                 SpeedoProperties.TRANSACTION_LOCKING_LEVEL_FIELD);
74         return p;
75     }
76     
77     public void testGroupUser2WithBarrier() {
78         testGroupUser(2,true);
79     }
80     public void testGroupUser20WithBarrier() {
81         testGroupUser(20,true);
82     }
83     public void testGroupUser20() {
84         testGroupUser(20,false);
85     }
86     public void testGroupUser50() {
87         testGroupUser(50,true);
88     }
89     public void testGroupUser100() {
90         testGroupUser(100,false);
91     }
92     
93     /**
94      * Create A group with several users
95      *
96      */

97     public void testGroupUser(final int NB, final boolean withBarrier) {
98         logger.log(BasicLevel.INFO, "testGroupUser" + NB +
99                 (withBarrier ? "WithBarrier" : ""));
100         final Waiter waiter = new Waiter(0, withBarrier);
101         final Group group = new Group("TestThinLock.testGroupUser.group1");
102         final User[] users = new User[NB];
103         for(int i=0; i<NB; i++) {
104             users[i] = new User("TestThinLock.testGroupUser.user" + i);
105             group.getUsers().add(users[i]);
106         }
107         PersistenceManager pm = pmf.getPersistenceManager();
108         pm.currentTransaction().begin();
109         pm.makePersistent(group);
110         pm.currentTransaction().commit();
111         pm.close();
112         Thread JavaDoc[] ths = new Thread JavaDoc[NB];
113         for(int i=0; i<NB; i++) {
114             ths[i] = new Thread JavaDoc(new TestGroupUser(waiter, group, users[i],
115                         new User("TestThinLock.testGroupUser.user" + (NB + i)),
116                         pmf));
117         }
118         for(int i=0; i<NB; i++) {
119             ths[i].start();
120         }
121         Collection JavaDoc threads = Arrays.asList(ths);
122         List JavaDoc res = waiter.nextAction(threads, 1000);
123         assertTrue("Thread blocked on first point: " + res, res.isEmpty());
124         res = waiter.nextAction(threads, 1000);
125         if (!res.isEmpty()) {
126             for(int i=0; i<ths.length; i++) {
127                 if (ths[i].isAlive()) {
128                     ths[i].interrupt();
129                 }
130             }
131         }
132         assertTrue("Thread blocked on second point: " + res, res.isEmpty());
133         res = waiter.nextAction(threads, 1000);
134         for(int i=0; i<ths.length; i++) {
135             if (ths[i].isAlive()) {
136                 try {
137                     ths[i].join(1000);
138                 } catch (InterruptedException JavaDoc e) {
139                     e.printStackTrace();
140                 }
141             }
142         }
143         pm = pmf.getPersistenceManager();
144         int size = group.getUsers().size();
145         pm.currentTransaction().begin();
146         pm.deletePersistentAll(group.getUsers());
147         pm.deletePersistent(group);
148         pm.currentTransaction().commit();
149         pm.close();
150         assertEquals("Bad number of users", NB, size);
151     }
152     
153     
154     public static class Waiter {
155         int actionId;
156         ArrayList JavaDoc waiters = new ArrayList JavaDoc();
157         boolean withBarrier;
158         public Waiter(int actionId, boolean withBarrier) {
159             this.actionId = actionId;
160             this.withBarrier = withBarrier;
161         }
162         public synchronized void waitAction(int aid) {
163             if (!withBarrier) {
164                 return;
165             }
166             waiters.add(Thread.currentThread());
167             notifyAll();
168             while(actionId < aid) {
169                 try {
170                     wait();
171                 } catch (InterruptedException JavaDoc e) {
172                     e.printStackTrace();
173                 }
174             }
175         }
176         public synchronized List JavaDoc nextAction(Collection JavaDoc threads, long time) {
177             if (!withBarrier) {
178                 return Collections.EMPTY_LIST;
179             }
180             while(!waiters.containsAll(threads)) {
181                 try {
182                     wait(time);
183                 } catch (InterruptedException JavaDoc e) {
184                     e.printStackTrace();
185                     break;
186                 }
187             }
188             if (waiters.containsAll(threads)) {
189                 actionId++;
190                 waiters.clear();
191                 notifyAll();
192                 return Collections.EMPTY_LIST;
193             } else {
194                 return Collections.unmodifiableList(
195                         new ArrayList JavaDoc(waiters));
196             }
197         }
198     }
199     
200     public static class TestGroupUser implements Runnable JavaDoc {
201         public Waiter waiter;
202         public Group group;
203         public User userToRemove;
204         public User userToAdd;
205         public PersistenceManagerFactory pmf;
206         
207         public TestGroupUser(Waiter waiter,
208                 Group group,
209                 User userToRemove,
210                 User userToAdd,
211                 PersistenceManagerFactory pmf) {
212             super();
213             this.waiter = waiter;
214             this.group = group;
215             this.userToRemove = userToRemove;
216             this.userToAdd = userToAdd;
217             this.pmf = pmf;
218         }
219         
220         public void run() {
221             PersistenceManager pm = pmf.getPersistenceManager();
222             pm.currentTransaction().begin();
223             Collection JavaDoc users = group.getUsers();
224             waiter.waitAction(1);
225             
226             users.add(userToAdd);
227             users.remove(userToRemove);
228             pm.deletePersistent(userToRemove);
229             
230             waiter.waitAction(2);
231             pm.currentTransaction().commit();
232             pm.close();
233
234             waiter.waitAction(3);
235         }
236     }
237 }
238
Popular Tags