1
2 package net.sf.voruta;
3
4 import junit.framework.*;
5 import java.util.*;
6
7 /***
8 *
9 * @author baliuka
10 */
11 public class SnapshotCacheTest extends TestCase {
12
13 public SnapshotCacheTest(java.lang.String testName) {
14 super(testName);
15 }
16
17 public static Test suite() {
18
19 TestSuite suite = new TestSuite(SnapshotCacheTest.class);
20 return suite;
21
22 }
23
24 public void testTrivial() {
25
26 SnapshotCache cache = new SnapshotCache();
27 cache.beforeBegin();
28 Object key = "testTrivial";
29 cache.put(key,"");
30 assertTrue(cache.get(key) != null);
31 cache.clear();
32 assertTrue(cache.get(key) == null);
33 cache.afterCommit();
34 }
35
36 public void testIsolation()throws Exception{
37
38 final SnapshotCache cache = new SnapshotCache();
39 cache.beforeBegin();
40 final Set state = new HashSet();
41 final Object key = "testIsolation";
42
43 cache.put(key,"");
44
45 assertTrue(cache.get(key) != null);
46
47 Thread thread = new Thread(){
48 public void run(){
49 cache.beforeBegin();
50 if(cache.get(key) != null ){
51 state.add(key);
52 }
53 }
54 };
55 thread.start();
56 assertTrue(cache.get(key) != null);
57 thread.join();
58 assertTrue(cache.get(key) != null);
59 assertTrue("Dirty read", !state.contains(key) );
60
61 thread = new Thread(){
62 public void run(){
63 cache.beforeBegin();
64 try{ sleep(100); }catch(Exception e){}
65 if(cache.get(key) != null ){
66 state.add(key);
67 }
68 }
69 };
70
71 thread.start();
72 try{ Thread.currentThread().sleep(1); }catch(Exception e){}
73 cache.afterCommit();
74 thread.join();
75
76
77 assertTrue(cache.get(key) != null);
78
79 assertTrue("non-repeatable read", !state.contains(key) );
80
81
82
83 }
84
85
86 public static void main(java.lang.String[] args) throws Exception{
87
88 junit.textui.TestRunner.run(suite());
89
90 }
91
92
93 }
This page was automatically generated by Maven