001: /*
002: * $URL: StorageTestThreaded.java $
003: * $Rev: 3582 $
004: * $Date: 2007-11-25 14:29:06 +0300 (Ð’Ñ?, 25 ноÑ? 2007) $
005: *
006: * Copyright 2005 Netup, Inc. All rights reserved.
007: * URL: http://www.netup.biz
008: * e-mail: info@netup.biz
009: */
010:
011: package org.garret.perst;
012:
013: import static org.garret.perst.Storage.INFINITE_PAGE_POOL;
014: import junit.framework.*;
015:
016: /**
017: * These tests verifies an implementation of the <code>Storage</code> interface. <br />
018: * he implementation is created by the following way :
019: * <pre>
020: * storage = org.garret.perst.StorageFactory.getInstance().createStorage()
021: * </pre>
022: * The tested features:
023: * <ul>
024: * <li>Synchronisation in multithreaded applications.</li>
025: * </ul>
026: * <p>
027: * In test are used simple <CODE>Persistent</CODE> class <CODE>Root</CODE>:
028: * <pre>
029: * class Root extends Persistent {
030: * int i;
031: * }
032: * </pre>
033: */
034: public class StorageTestThreaded extends TestCase {
035:
036: Storage storage;
037:
038: public StorageTestThreaded(String testName) {
039: super (testName);
040: }
041:
042: public static junit.framework.Test suite() {
043: junit.framework.TestSuite suite = new junit.framework.TestSuite(
044: StorageTestThreaded.class);
045:
046: return suite;
047: }
048:
049: protected void setUp() throws java.lang.Exception {
050: storage = StorageFactory.getInstance().createStorage();
051: }
052:
053: protected void tearDown() throws java.lang.Exception {
054: if (storage.isOpened())
055: storage.close();
056: }
057:
058: /**
059: * <B>Goal:</B> To prove correct synchronisation in <code>EXCLUSIVE_TRANSACTION</code>.
060: * <P>
061: * <B>Conditions:</B>
062: * <ul>
063: * <li>Two threads started: <br />
064: * Each thread accessed storage in <code>EXCLUSIVE_TRANSACTION</code>.
065: * </li>
066: * </ul>
067: * <P>
068: * <B>Result:</B>
069: * <ul>
070: * <li>Storage provided correct synchronisation.</li>
071: * </ul>
072: */
073: public void testThreadExclusive() {
074: storage.open(new NullFile(), INFINITE_PAGE_POOL);
075: storage.setRoot(new Root());
076: storage.commit();
077: new TestThreadExclusive(storage);
078: new TestThreadExclusive(storage);
079:
080: while (TestThread.cnt > 0) {
081: try {
082: Thread.sleep(100);
083: } catch (InterruptedException ex) {
084: }
085: }
086: if (TestThread.failed != "") {
087: fail(TestThread.failed);
088: }
089: }
090:
091: /**
092: * <B>Goal:</B> To prove correct synchronisation in <code>SERIALIZABLE_TRANSACTION</code>.
093: * <P>
094: * <B>Conditions:</B>
095: * <ul>
096: * <li>Two threads started: <br />
097: * Each thread accessed storage in <code>SERIALIZABLE_TRANSACTION</code>.
098: * </li>
099: * </ul>
100: * <P>
101: * <B>Result:</B>
102: * <ul>
103: * <li>Storage provided correct synchronisation.</li>
104: * </ul>
105: */
106: public void testThreadSerializable() {
107: storage.open(new NullFile(), INFINITE_PAGE_POOL);
108: storage.setRoot(new Root());
109: storage.commit();
110: new TestThreadSerializable(storage);
111: new TestThreadSerializable(storage);
112:
113: while (TestThread.cnt > 0) {
114: try {
115: Thread.sleep(100);
116: } catch (InterruptedException ex) {
117: }
118: }
119: if (TestThread.failed != "") {
120: fail(TestThread.failed);
121: }
122: }
123:
124: /**
125: * Internal class.
126: */
127: private static class Root extends PersistentResource {
128: int i;
129:
130: public Root(int i) {
131: this .i = i;
132: }
133:
134: public Root() {
135: }
136:
137: public String toString() {
138: return "Root{" + i + "}";
139: }
140: }
141:
142: private static class TestThread extends Thread {
143: static Integer lock = new Integer(5);
144: static int max_id = 0;
145: static int cnt = 0;
146: Storage storage;
147: static String failed = "";
148: int myID = ++max_id;
149:
150: TestThread(Storage storage) {
151: cnt++;
152: this .storage = storage;
153: this .start();
154: }
155:
156: protected void print(String s) {
157: //System.out.println("Thread "+myID+": "+s+".");
158: }
159:
160: }
161:
162: private static class TestThreadExclusive extends TestThread {
163: TestThreadExclusive(Storage storage) {
164: super (storage);
165: }
166:
167: public void run() {
168: print("ready");
169: storage
170: .beginThreadTransaction(Storage.EXCLUSIVE_TRANSACTION);
171: print("start");
172: Root root = (Root) storage.getRoot();
173: if (0 != root.i)
174: failed = "Test failed.";
175: root.i = 10;
176: try {
177: sleep(100 + myID * 50);
178: } catch (InterruptedException e) {
179: }
180: if (10 != root.i)
181: failed = "Test failed.";
182: root.i = 0;
183: print("end");
184: storage.rollbackThreadTransaction();
185: cnt--;
186: }
187: }
188:
189: private static class TestThreadSerializable extends TestThread {
190: TestThreadSerializable(Storage storage) {
191: super (storage);
192: }
193:
194: public void run() {
195: storage
196: .beginThreadTransaction(Storage.SERIALIZABLE_TRANSACTION);
197: Root root = (Root) storage.getRoot();
198: print("ready");
199: root.exclusiveLock();
200: print("start");
201: if (0 != root.i)
202: failed = "Test failed.";
203: root.i = 10;
204: try {
205: sleep(100);
206: } catch (InterruptedException e) {
207: }
208: if (10 != root.i)
209: failed = "Test failed.";
210: root.i = 0;
211: print("end");
212: root.unlock();
213: storage.rollbackThreadTransaction();
214: cnt--;
215: }
216: }
217: }
|