01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.test.restart;
05:
06: import com.tc.test.TCTestCase;
07: import com.tc.util.PortChooser;
08: import com.tc.util.concurrent.NoExceptionLinkedQueue;
09:
10: import java.io.IOException;
11: import java.io.InputStream;
12: import java.net.ServerSocket;
13: import java.net.Socket;
14:
15: public class RestartTestEnvironmentTest extends TCTestCase {
16:
17: public void testChoosePort() throws Exception {
18: PortChooser portChooser = new PortChooser();
19: RestartTestEnvironment env = new RestartTestEnvironment(
20: getTempDirectory(), portChooser,
21: RestartTestEnvironment.PROD_MODE);
22: int port = env.chooseAdminPort();
23: NoExceptionLinkedQueue control = new NoExceptionLinkedQueue();
24: System.err.println("I chose port " + port);
25: Listener listener = new Listener(port, control);
26: listener.start();
27: control.take();
28: Socket s = new Socket("localhost", port);
29: int value = 10;
30: s.getOutputStream().write(value);
31: assertEquals(new Integer(value), control.take());
32: }
33:
34: private static final class Listener extends Thread {
35: private int port;
36: private final NoExceptionLinkedQueue control;
37:
38: public Listener(int port, NoExceptionLinkedQueue control) {
39: this .port = port;
40: this .control = control;
41: }
42:
43: public void run() {
44: try {
45: ServerSocket s = new ServerSocket(port);
46: control.put(new Object());
47: Socket socket = s.accept();
48: InputStream in = socket.getInputStream();
49: control.put(new Integer(in.read()));
50: } catch (IOException e) {
51: e.printStackTrace();
52: }
53: }
54: }
55: }
|