KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.tctest.spring.integrationtests.tests;
5
6 import com.tctest.spring.bean.ISharedLock;
7 import com.tctest.spring.integrationtests.framework.AbstractTwoServerDeploymentTest;
8 import com.tctest.spring.integrationtests.framework.DeploymentBuilder;
9
10 import java.util.List JavaDoc;
11
12 import junit.extensions.TestSetup;
13 import junit.framework.Test;
14
15 /**
16  * This class is testing shared lock behavior
17  * 1. Locking on shared object applies to the whole cluster
18  * 2. Trying to modify a shared objec outside a shared lock should raise exception
19  * 3. Shared value will not be propagated until execution thread exits the monitor
20  */

21 public class SharedLockTest extends AbstractTwoServerDeploymentTest {
22
23   private static final String JavaDoc REMOTE_SERVICE_NAME = "SharedLock";
24   private static final String JavaDoc BEAN_DEFINITION_FILE_FOR_TEST = "classpath:/com/tctest/spring/beanfactory-sharedlock.xml";
25   private static final String JavaDoc CONFIG_FILE_FOR_TEST = "/tc-config-files/sharedlock-tc-config.xml";
26
27   private static ISharedLock sharedLock1;
28   private static ISharedLock sharedLock2;
29
30   public void testSharedLock() throws Exception JavaDoc {
31
32     logger.debug("testing ShareLock");
33     sharedLock1.start();
34     sharedLock2.start();
35
36     long id1 = sharedLock1.getLocalID();
37     long id2 = sharedLock2.getLocalID();
38     
39     List JavaDoc sharedVar1 = sharedLock1.getSharedVar();
40     List JavaDoc sharedVar2 = sharedLock2.getSharedVar();
41     List JavaDoc unSharedVar1 = sharedLock1.gethUnSharedVar();
42     List JavaDoc unSharedVar2 = sharedLock2.gethUnSharedVar();
43     
44     
45     assertTrue("Pre-condition is not satisfied - id1 - " + id1 + " - id2 - " + id2,
46                id1 != id2);
47     assertEquals("Pre-condition is not satisfied", 0, unSharedVar1.size());
48     assertEquals("Pre-condition is not satisfied", 0, unSharedVar2.size());
49     assertEquals("Pre-condition is not satisfied", 0, sharedVar2.size());
50     assertEquals("Pre-condition is not satisfied", 0, sharedVar2.size());
51     
52     sharedLock1.moveToStep(10);
53     sharedLock2.moveToStep(10);
54     
55     sharedVar1 = sharedLock1.getSharedVar();
56     sharedVar2 = sharedLock2.getSharedVar();
57     unSharedVar1 = sharedLock1.gethUnSharedVar();
58     unSharedVar2 = sharedLock2.gethUnSharedVar();
59
60     assertEquals("Expected exception not happen: " + sharedVar1, 0, sharedVar1.size());
61     assertEquals("Expected exception not happen: " + sharedVar2, 0, sharedVar2.size());
62     assertEquals("Expected exception not happen: " + sharedVar1, 1, unSharedVar1.size());
63     assertEquals("Expected exception not happen: " + sharedVar2, 1, unSharedVar2.size());
64     assertEquals("Expected exception not happen: " + sharedVar1, unSharedVar1.get(0), "ckpoint1-"+id1);
65     assertEquals("Expected exception not happen: " + sharedVar2, unSharedVar2.get(0), "ckpoint1-" + id2);
66
67     sharedLock1.moveToStep(20); // sharedLock1 get the lock and modify the sharedVar
68
sharedLock2.moveToStep(20); // sharedLock1 should block
69

70     sharedVar1 = sharedLock1.getSharedVar();
71     sharedVar2 = sharedLock2.getSharedVar();
72   
73     assertEquals("Failed to aquire the lock and modify the shared variable: " + sharedVar1, 1, sharedVar1.size());
74     assertEquals("Failed to aquire the lock and modify the shared variable: " + sharedVar1, sharedVar1.get(0), "ckpoint2-"+id1);
75     assertEquals("Failed to be blocked: " + sharedVar2, 0, sharedVar2.size());
76     
77     sharedLock1.moveToStep(30); // sharedLock1 release the lock and propagate the value
78

79     sharedVar1 = sharedLock1.getSharedVar();
80     sharedVar2 = sharedLock2.getSharedVar();
81   
82     assertEquals("Unexpected modification: " + sharedVar1, 1, sharedVar1.size());
83     assertEquals("Unexpected modification: " + sharedVar1, sharedVar1.get(0), "ckpoint2-"+id1);
84     assertEquals("Failed to aquired lock and receive the propagation: " + sharedVar2, 2, sharedVar2.size());
85     assertTrue("Failed to aquired lock and receive the propagation: " + sharedVar2, sharedVar2.contains("ckpoint2-"+id1));
86     assertTrue("Failed to aquired lock and receive the propagation: " + sharedVar2, sharedVar2.contains("ckpoint2-"+id2));
87     
88     sharedLock2.moveToStep(30); // sharedLock1 release the lock and propagate the value
89

90     sharedVar1 = sharedLock1.getSharedVar();
91     sharedVar2 = sharedLock2.getSharedVar();
92   
93     assertEquals("Failed receive the propagation: " + sharedVar1, 2, sharedVar1.size());
94     assertTrue("Failed receive the propagation: " + sharedVar1, sharedVar1.contains("ckpoint2-"+id1));
95     assertTrue("Failed receive the propagation: " + sharedVar1, sharedVar1.contains("ckpoint2-"+id2));
96         
97     logger.debug("!!!! Asserts passed !!!");
98   }
99
100   
101   private static class SharedLockTestSetup extends TwoSvrSetup {
102     private SharedLockTestSetup() {
103       super(SharedLockTest.class, CONFIG_FILE_FOR_TEST, "test-sharedlock");
104     }
105
106     protected void setUp() throws Exception JavaDoc {
107       super.setUp();
108
109       sharedLock1 = (ISharedLock) server1.getProxy(ISharedLock.class, REMOTE_SERVICE_NAME);
110       sharedLock2 = (ISharedLock) server2.getProxy(ISharedLock.class, REMOTE_SERVICE_NAME);
111     }
112
113     protected void configureWar(DeploymentBuilder builder) {
114       builder.addBeanDefinitionFile(BEAN_DEFINITION_FILE_FOR_TEST);
115       builder.addRemoteService(REMOTE_SERVICE_NAME, "sharedlock", ISharedLock.class);
116     }
117   }
118
119   /**
120    * JUnit test loader entry point
121    */

122   public static Test suite() {
123     TestSetup setup = new SharedLockTestSetup();
124     return setup;
125   }
126
127 }
128
Popular Tags