1 4 package com.tc.test.restart; 5 6 import com.tc.test.TCTestCase; 7 import com.tc.util.PortChooser; 8 import com.tc.util.concurrent.NoExceptionLinkedQueue; 9 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(getTempDirectory(), portChooser, RestartTestEnvironment.PROD_MODE); 20 int port = env.chooseAdminPort(); 21 NoExceptionLinkedQueue control = new NoExceptionLinkedQueue(); 22 System.err.println("I chose port " + port); 23 Listener listener = new Listener(port, control); 24 listener.start(); 25 control.take(); 26 Socket s = new Socket ("localhost", port); 27 int value = 10; 28 s.getOutputStream().write(value); 29 assertEquals(new Integer (value), control.take()); 30 } 31 32 private static final class Listener extends Thread { 33 private int port; 34 private final NoExceptionLinkedQueue control; 35 36 public Listener(int port, NoExceptionLinkedQueue control) { 37 this.port = port; 38 this.control = control; 39 } 40 41 public void run() { 42 try { 43 ServerSocket s = new ServerSocket (port); 44 control.put(new Object ()); 45 Socket socket = s.accept(); 46 InputStream in = socket.getInputStream(); 47 control.put(new Integer (in.read())); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 } 53 } 54 | Popular Tags |