KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > DeadClientCrashedServerReconnectTest


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;
5
6 import com.tc.cluster.Cluster;
7 import com.tc.config.schema.setup.L1TVSConfigurationSetupManager;
8 import com.tc.lang.TCThreadGroup;
9 import com.tc.lang.ThrowableHandler;
10 import com.tc.logging.TCLogging;
11 import com.tc.net.proxy.TCPProxy;
12 import com.tc.object.BaseDSOTestCase;
13 import com.tc.object.DistributedObjectClient;
14 import com.tc.object.PauseListener;
15 import com.tc.object.bytecode.MockClassProvider;
16 import com.tc.object.bytecode.NullManager;
17 import com.tc.object.bytecode.hook.impl.PreparedComponentsFromL2Connection;
18 import com.tc.object.config.DSOClientConfigHelper;
19 import com.tc.object.config.StandardDSOClientConfigHelper;
20 import com.tc.objectserver.control.ServerControl;
21 import com.tc.test.restart.RestartTestEnvironment;
22 import com.tc.test.restart.RestartTestHelper;
23 import com.tc.util.PortChooser;
24
25 import java.net.InetAddress JavaDoc;
26
27 public class DeadClientCrashedServerReconnectTest extends BaseDSOTestCase {
28
29   private TestPauseListener pauseListener;
30
31   public DeadClientCrashedServerReconnectTest() {
32     this.disableAllUntil("3001-01-01");
33   }
34
35   public void setUp() {
36     pauseListener = new TestPauseListener();
37   }
38
39   public void testClientsStayDeadAcrossRestarts() throws Exception JavaDoc {
40     final boolean isCrashy = true;
41     PortChooser portChooser = new PortChooser();
42     RestartTestHelper helper = new RestartTestHelper(isCrashy,
43                                                      new RestartTestEnvironment(this.getTempDirectory(), portChooser,
44                                                                                 RestartTestEnvironment.DEV_MODE));
45     ServerControl server = helper.getServerControl();
46     long startTimeout = 20 * 60 * 1000;
47     server.start(startTimeout);
48
49     int proxyListenPort = portChooser.chooseRandomPort();
50     InetAddress JavaDoc destHost = InetAddress.getByName("localhost");
51     int destPort = server.getDsoPort();
52     long delay = 0;
53     boolean logData = true;
54     TCPProxy proxy = new TCPProxy(proxyListenPort, destHost, destPort, delay, logData, getTempDirectory());
55     proxy.toggleDebug();
56     proxy.start();
57
58     makeClientUsePort(proxyListenPort);
59
60     L1TVSConfigurationSetupManager manager = super.createL1ConfigManager();
61     DSOClientConfigHelper configHelper = new StandardDSOClientConfigHelper(manager);
62
63     PreparedComponentsFromL2Connection components = new PreparedComponentsFromL2Connection(manager);
64
65     DistributedObjectClient client = new DistributedObjectClient(configHelper,
66                                                                  new TCThreadGroup(new ThrowableHandler(TCLogging
67                                                                      .getLogger(DistributedObjectClient.class))),
68                                                                  new MockClassProvider(), components, NullManager
69                                                                      .getInstance(), new Cluster());
70     client.setPauseListener(pauseListener);
71     client.start();
72
73     // wait until client handshake is complete...
74
pauseListener.waitUntilUnpaused();
75
76     // disconnect the client from the server... this should make the server kill the client from the server
77
// perspective...
78
proxy.stop();
79
80     // Now crash the server
81
server.crash();
82
83     // start the server back up
84
proxy.start();
85     server.start(startTimeout);
86
87     // XXX: This is kind of dumb... there should be a way to find out from the server if the client has actually
88
// tried to connect but was not allowed to.
89
Thread.sleep(10 * 1000);
90     assertTrue(pauseListener.isPaused());
91   }
92
93   private static final class TestPauseListener implements PauseListener {
94
95     private boolean paused = true;
96
97     public void waitUntilPaused() throws InterruptedException JavaDoc {
98       waitUntilCondition(true);
99     }
100
101     public void waitUntilUnpaused() throws InterruptedException JavaDoc {
102       waitUntilCondition(false);
103     }
104
105     public boolean isPaused() {
106       synchronized (this) {
107         return paused;
108       }
109     }
110
111     private void waitUntilCondition(boolean b) throws InterruptedException JavaDoc {
112       synchronized (this) {
113         while (b != paused) {
114           wait();
115         }
116       }
117     }
118
119     public void notifyPause() {
120       synchronized (this) {
121         paused = true;
122         notifyAll();
123       }
124     }
125
126     public void notifyUnpause() {
127       synchronized (this) {
128         paused = false;
129         notifyAll();
130       }
131     }
132
133   }
134
135 }
136
Popular Tags