KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > concurrency > TestOptimistic


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

25
26 package org.objectweb.perseus.concurrency;
27
28 import junit.framework.TestCase;
29 import org.objectweb.perseus.persistence.PInteger;
30 import org.objectweb.perseus.persistence.PIntegerState;
31 import org.objectweb.perseus.persistence.TPMTest;
32 import org.objectweb.perseus.cache.replacement.lib.LRUReplacementManager;
33 import org.objectweb.perseus.concurrency.api.ConcurrencyException;
34 import org.objectweb.perseus.persistence.api.PersistenceException;
35 import org.objectweb.perseus.persistence.api.TransactionalWorkingSet;
36 import org.objectweb.perseus.persistence.concurrency.POptimisticConcurrencyManager;
37
38 public class TestOptimistic extends TestCase {
39
40     PInteger o1, o2;
41     Object JavaDoc oid1, oid2;
42
43     TransactionalWorkingSet ctxt1, ctxt2;
44     TPMTest tpm;
45
46     public TestOptimistic(String JavaDoc s) throws Exception JavaDoc {
47         super(s);
48     }
49
50     protected void setUp() throws Exception JavaDoc {
51
52         tpm = new TPMTest("TPM", new POptimisticConcurrencyManager(), new LRUReplacementManager());
53         ctxt1 = (TransactionalWorkingSet) tpm.createWS("1");
54         ctxt2 = (TransactionalWorkingSet) tpm.createWS("2");
55         o1 = new PInteger(1);
56         o2 = new PInteger(2);
57
58         tpm.begin(ctxt1);
59         oid1 = tpm.export(ctxt1, o1);
60         oid2 = tpm.export(ctxt1, o2);
61         tpm.prepare(ctxt1);
62         tpm.commit(ctxt1);
63
64     }
65
66     // single context, single object, single read access
67
public void test1 () throws ConcurrencyException, PersistenceException {
68         tpm.begin(ctxt1);
69         tpm.readIntention(ctxt1, oid1);
70         assertTrue(tpm.prepare(ctxt1));
71         tpm.commit(ctxt1);
72     }
73
74     // single context, single object, single write access
75
public void test2 () throws ConcurrencyException, PersistenceException {
76         tpm.begin(ctxt1);
77         tpm.readIntention(ctxt1, oid1);
78         tpm.writeIntention(ctxt1, oid1);
79         assertTrue(tpm.prepare(ctxt1));
80         tpm.commit(ctxt1);
81     }
82
83     // two contexts, single object, simultaneous read accesses
84
public void test3 () throws ConcurrencyException, PersistenceException {
85         tpm.begin(ctxt1);
86         tpm.begin(ctxt2);
87         tpm.readIntention(ctxt1, oid1);
88         tpm.readIntention(ctxt2, oid1);
89         assertTrue(tpm.prepare(ctxt1));
90         tpm.commit(ctxt1);
91         assertTrue(tpm.prepare(ctxt2));
92         tpm.commit(ctxt2);
93     }
94
95     // two contexts, single object, one read access, one write access
96
public void test4 () throws ConcurrencyException, PersistenceException {
97         PIntegerState s1, s2;
98         tpm.begin(ctxt1);
99         tpm.begin(ctxt2);
100         s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1);
101         s2 = (PIntegerState) tpm.readIntention(ctxt2, oid1);
102         s2 = (PIntegerState) tpm.writeIntention(ctxt2, oid1);
103         s2.i = 2;
104
105         // check repeatable reads (tx2 not committed)
106
s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1);
107         assertTrue(s1.i == 1);
108         assertTrue(tpm.prepare(ctxt2));
109         tpm.commit(ctxt2);
110
111         // check repeatable reads (tx2 committed)
112
s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1);
113         assertTrue(s1.i == 1);
114         // cannot commit read-only transaction with current implementation
115
tpm.rollback(ctxt1);
116 // tpm.commit(ctxt1);
117

118         // check read committed
119
tpm.begin(ctxt1);
120         s1 = (PIntegerState) tpm.readIntention(ctxt1, oid1);
121         assertTrue(s1.i == 2);
122         assertTrue(tpm.prepare(ctxt1));
123         tpm.commit(ctxt1);
124     }
125
126     // two contexts, single object, one write access, one read access
127
public void test5 () throws ConcurrencyException, PersistenceException {
128         tpm.begin(ctxt1);
129         tpm.begin(ctxt2);
130         tpm.readIntention(ctxt1, oid1);
131         tpm.writeIntention(ctxt1, oid1);
132         tpm.readIntention(ctxt2, oid1);
133         assertTrue(tpm.prepare(ctxt1));
134         tpm.commit(ctxt1);
135         tpm.rollback(ctxt2);
136     }
137
138     // two contexts, single object, simultaneous write access
139
public void test6 () throws ConcurrencyException, PersistenceException {
140         tpm.begin(ctxt1);
141         tpm.begin(ctxt2);
142         tpm.readIntention(ctxt1, oid1);
143         tpm.writeIntention(ctxt1, oid1);
144         tpm.readIntention(ctxt2, oid1);
145         tpm.writeIntention(ctxt2, oid1);
146         assertTrue(tpm.prepare(ctxt1));
147         tpm.commit(ctxt1);
148         tpm.rollback(ctxt2);
149     }
150
151     // two contexts, two objects
152
// T1 read O1, T2 read O2, T1 write O1, T2 write O2
153
public void test7 () throws ConcurrencyException, PersistenceException {
154         tpm.begin(ctxt1);
155         tpm.begin(ctxt2);
156         tpm.readIntention(ctxt1, oid1);
157         tpm.readIntention(ctxt2, oid2);
158         tpm.readIntention(ctxt1, oid1);
159         tpm.writeIntention(ctxt1, oid1);
160         tpm.readIntention(ctxt2, oid2);
161         tpm.writeIntention(ctxt2, oid2);
162         assertTrue(tpm.prepare(ctxt1));
163         tpm.commit(ctxt1);
164         assertTrue(tpm.prepare(ctxt2));
165         tpm.commit(ctxt2);
166     }
167
168     // two contexts, two objects, no deadlock with optimist
169
// T1 read O1, T2 read O2, T1 write O2, T2 write O1
170
public void test9 () throws ConcurrencyException, PersistenceException {
171         tpm.begin(ctxt1);
172         tpm.begin(ctxt2);
173         tpm.readIntention(ctxt1, oid1);
174         tpm.readIntention(ctxt2, oid2);
175         tpm.readIntention(ctxt1, oid2);
176         tpm.writeIntention(ctxt1, oid2);
177         tpm.readIntention(ctxt2, oid1);
178         tpm.writeIntention(ctxt2, oid1);
179         assertTrue(tpm.prepare(ctxt1));
180         tpm.commit(ctxt1);
181         tpm.rollback(ctxt2);
182     }
183 }
184
Popular Tags