KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > statetransfer > StateTransferUnderLoadTest


1 package org.jboss.cache.statetransfer;
2
3 import junit.framework.Test;
4 import junit.framework.TestCase;
5 import junit.framework.TestSuite;
6 import org.jboss.cache.Cache;
7 import org.jboss.cache.CacheSPI;
8 import org.jboss.cache.Fqn;
9 import org.jboss.cache.config.Configuration;
10 import org.jboss.cache.factories.DefaultCacheFactory;
11 import org.jboss.cache.misc.TestingUtil;
12 import org.jboss.cache.transaction.DummyTransactionManager;
13
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.transaction.SystemException JavaDoc;
17 import javax.transaction.UserTransaction JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 /**
21  * Tests state transfer while the other node keeps sending transactional, synchronous method calls
22  *
23  * @author Bela Ban
24  * @version $Id: StateTransferUnderLoadTest.java,v 1.7 2007/01/02 18:26:07 msurtani Exp $
25  */

26 public class StateTransferUnderLoadTest extends TestCase
27 {
28    Cache cache1, cache2;
29    Properties JavaDoc p = null;
30    String JavaDoc old_factory = null;
31    final String JavaDoc FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
32
33
34    public StateTransferUnderLoadTest(String JavaDoc name)
35    {
36       super(name);
37    }
38
39    public void setUp() throws Exception JavaDoc
40    {
41       super.setUp();
42       old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
43       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
44       DummyTransactionManager.getInstance();
45       if (p == null)
46       {
47          p = new Properties JavaDoc();
48          p.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.cache.transaction.DummyContextFactory");
49       }
50    }
51
52
53    public void tearDown() throws Exception JavaDoc
54    {
55       super.tearDown();
56       if (cache2 != null)
57       {
58          cache2.stop();
59          cache2 = null;
60       }
61       if (cache1 != null)
62       {
63          cache1.stop();
64          cache1 = null;
65       }
66
67       // BW. kind of a hack to destroy jndi binding and thread local tx before next run.
68
DummyTransactionManager.destroy();
69       if (old_factory != null)
70       {
71          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
72          old_factory = null;
73       }
74    }
75
76    public void testStateTransferDeadlocksPessimistic() throws Exception JavaDoc
77    {
78       runTest(false);
79    }
80
81    public void testStateTransferDeadlocksOptimistic() throws Exception JavaDoc
82    {
83       runTest(true);
84    }
85
86
87    private void runTest(boolean optimistic) throws Exception JavaDoc
88    {
89       Writer writer;
90       Configuration cfg1, cfg2;
91       cfg1 = new Configuration();
92       cfg2 = new Configuration();
93       cfg1.setCacheMode(Configuration.CacheMode.REPL_SYNC);
94       cfg2.setCacheMode(Configuration.CacheMode.REPL_SYNC);
95
96       if (optimistic)
97       {
98          cfg1.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
99          cfg2.setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
100       }
101
102       cfg1.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
103       cfg2.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
104
105       cache1 = DefaultCacheFactory.getInstance().createCache(cfg1, true);
106       cache2 = DefaultCacheFactory.getInstance().createCache(cfg2, false);
107       UserTransaction JavaDoc tx1 = (UserTransaction JavaDoc) new InitialContext JavaDoc(p).lookup("UserTransaction");
108       writer = new Writer(cache1, tx1);
109       try
110       {
111          writer.start();
112
113
114          cache2.create();
115          for (int i = 0; i < 100; i++)
116          {
117
118             cache2.start();
119             // gets state
120

121             // check if state was retrieved successfully
122
int num_nodes = ((CacheSPI) cache2).getNumberOfNodes();
123             assertTrue(num_nodes >= 1);
124
125             TestingUtil.sleepThread(100);
126             cache2.stop();
127          }
128
129
130       }
131       finally
132       {
133          if (writer != null)
134             writer.stop();
135       }
136    }
137
138
139    static class Writer implements Runnable JavaDoc
140    {
141       Thread JavaDoc thread;
142       Cache cache;
143       boolean running = false;
144       UserTransaction JavaDoc tx;
145
146
147       public Writer(Cache cache, UserTransaction JavaDoc tx)
148       {
149          this.cache = cache;
150          this.tx = tx;
151       }
152
153       public void start()
154       {
155          thread = new Thread JavaDoc(this, "cache writer");
156          running = true;
157          thread.start();
158       }
159
160       public void stop()
161       {
162          running = false;
163       }
164
165       public void run()
166       {
167          Fqn fqn = Fqn.fromString("/a/b/c");
168          while (running)
169          {
170             try
171             {
172                tx.begin();
173                cache.put(fqn, "key", "value");
174                tx.commit();
175             }
176             catch (Exception JavaDoc e)
177             {
178                e.printStackTrace();
179                try {tx.rollback();} catch (SystemException JavaDoc e1) {}
180             }
181             finally
182             {
183                TestingUtil.sleepRandom(100);
184             }
185          }
186       }
187
188    }
189
190
191    private static void log(String JavaDoc msg)
192    {
193       System.out.println(Thread.currentThread().getName() + ": " + msg);
194    }
195
196
197    public static Test suite() throws Exception JavaDoc
198    {
199       return new TestSuite(StateTransferUnderLoadTest.class);
200    }
201
202    public static void main(String JavaDoc[] args) throws Exception JavaDoc
203    {
204       junit.textui.TestRunner.run(StateTransferUnderLoadTest.suite());
205    }
206
207
208 }
209
Popular Tags