KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > spring > integrationtests > tests > ThreadCoordinationTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4
5 package com.tctest.spring.integrationtests.tests;
6
7 import com.tctest.spring.bean.IActiveBean;
8 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
9 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
10 import com.tctest.spring.integrationtests.framework.TestCallback;
11
12 import junit.framework.Test;
13
14 /**
15  * LKC-1760: Test: thread coordination for spring beans
16  * https://jira.terracotta.lan/jira//browse/LKC-1760
17  *
18  * Test that thread coordination is working across two nodes
19  *
20  * Auto locks
21  * Named locks
22  *
23  * Locking on bean
24  * Locking on reference in bean (?????)
25  * wait/notify etc.
26  */

27 public class ThreadCoordinationTest extends AbstractTwoServerDeploymentTest {
28
29   private static final int MAX_NOTIFY_DELAY = 1;
30   private static final int MAX_REPLICATION_DELAY = 10;
31
32   public void testAutolock() throws Throwable JavaDoc {
33     logger.info("Start testAutolock()");
34     checkThreadCoordination("beanWithAutolock");
35     logger.info("End testAutolock()");
36   }
37
38   public void testNamedLock() throws Throwable JavaDoc {
39     logger.info("Start testNamedLock()");
40     checkThreadCoordination("beanWithNamedLock");
41     logger.info("End testNamedLock()");
42   }
43   
44   public void testWaitNotify() throws Throwable JavaDoc {
45     logger.info("Start testWaitNotify()");
46     
47     final IActiveBean bean1 = (IActiveBean) server1.getProxy(IActiveBean.class, "beanWithWaitNotify");
48     final IActiveBean bean2 = (IActiveBean) server2.getProxy(IActiveBean.class, "beanWithWaitNotify");
49     
50     bean1.start();
51     bean2.start();
52
53     assertEquals("Preconditions " + getInfo(bean1, bean2), "0", bean1.getValue());
54     assertEquals("Preconditions " + getInfo(bean1, bean2), "0", bean2.getValue());
55     
56     // added this sleep to make sure the 2 running thread made to the wait().
57
Thread.sleep(1000L);
58
59     bean1.setValue("1");
60     waitForSuccess(1, new TestCallback() {
61         public void check() throws Exception JavaDoc {
62           assertEquals("Expected update1: "+ getInfo(bean1, bean2), "2", bean1.getValue());
63           assertEquals("Expected update1: "+ getInfo(bean1, bean2), "2", bean2.getValue());
64           
65         }
66       });
67     
68     // added this sleep to make sure the 2 running thread made to the wait().
69
Thread.sleep(1000L);
70
71     bean2.setValue("1");
72     waitForSuccess(MAX_NOTIFY_DELAY, new TestCallback() {
73         public void check() throws Exception JavaDoc {
74           assertEquals("Expected update2: "+ getInfo(bean1, bean2), "4", bean1.getValue());
75           assertEquals("Expected update2: "+ getInfo(bean1, bean2), "4", bean2.getValue());
76         }
77       });
78     
79     bean1.stop();
80     bean2.stop();
81
82     logger.info("End testWaitNotify()");
83   }
84   
85   private void checkThreadCoordination(String JavaDoc beanName) throws Throwable JavaDoc {
86     final IActiveBean bean1 = (IActiveBean) server1.getProxy(IActiveBean.class, beanName);
87     final IActiveBean bean2 = (IActiveBean) server2.getProxy(IActiveBean.class, beanName);
88     
89     bean1.setValue("1");
90
91     bean1.start();
92     waitForSuccess(MAX_REPLICATION_DELAY, new TestCallback() {
93         public void check() throws Exception JavaDoc {
94           assertEquals("Expected update1 on bean1: "+ getInfo(bean1, bean2), "1", bean1.getValue());
95         }
96       });
97
98     bean2.start();
99     Thread.sleep(1000L);
100
101     waitForSuccess(5, new TestCallback() {
102         public void check() throws Exception JavaDoc {
103           assertEquals("Expected update1 on bean1: "+ getInfo(bean1, bean2), "1", bean1.getValue());
104           assertEquals("Expected update1 only on bean1: "+ getInfo(bean1, bean2), "0", bean2.getValue());
105         }
106       });
107
108     bean1.stop();
109     waitForSuccess(5, new TestCallback() {
110         public void check() throws Exception JavaDoc {
111           assertEquals("Expected update1 on bean2: "+ getInfo(bean1, bean2), "1", bean2.getValue());
112         }
113       });
114     
115     bean1.start();
116     Thread.sleep(1000L);
117     
118     bean2.setValue("2");
119     waitForSuccess(5, new TestCallback() {
120         public void check() throws Exception JavaDoc {
121           assertEquals("Expected update2 on bean2: "+ getInfo(bean1, bean2), "2", bean2.getValue());
122           assertEquals("Expected update2 only on bean2: "+ getInfo(bean1, bean2), "1", bean1.getValue());
123         }
124       });
125
126     bean2.stop();
127     waitForSuccess(5, new TestCallback() {
128         public void check() throws Exception JavaDoc {
129           assertEquals("Expected update2 on bean1: "+ getInfo(bean1, bean2), "2", bean1.getValue());
130         }
131       });
132     
133     bean1.stop();
134   }
135
136   private String JavaDoc getInfo(IActiveBean bean1, IActiveBean bean2) {
137     return (bean1.isActive() ? "started" : "waiting") + ":" + bean1.getValue() + "/"
138            + (bean2.isActive() ? "started" : "waiting") + ":" + bean2.getValue();
139   }
140   
141   
142   private static class ThreadCoordinationTestSetup extends TwoSvrSetup {
143     private ThreadCoordinationTestSetup() {
144       super(ThreadCoordinationTest.class, "/tc-config-files/thread-coordination-tc-config.xml", "thread-coordination-test");
145     }
146
147     protected void configureWar(DeploymentBuilder builder) {
148       builder.addBeanDefinitionFile("classpath:/com/tctest/spring/beanfactory-thread-coordination.xml");
149       
150       builder.addRemoteService("beanWithAutolock", "beanWithAutolock", IActiveBean.class);
151       builder.addRemoteService("beanWithNamedLock", "beanWithNamedLock", IActiveBean.class);
152       builder.addRemoteService("beanWithWaitNotify", "beanWithWaitNotify", IActiveBean.class);
153     }
154
155   }
156
157   /**
158    * JUnit test loader entry point
159    */

160   public static Test suite() {
161     return new ThreadCoordinationTestSetup();
162   }
163
164 }
165
166
Popular Tags