changeset 430:03ab374605a6

Test program termination.
author one
date Sat, 02 Jan 2010 04:16:25 +0900
parents c2e4759eddcb
children 1bb59652d89c
files Todo rep/ServerMainLoop.java rep/channel/SelectorSimulator.java test/sematest/TestInterManagerSession.java test/sematest/TestSessionManager.java
diffstat 5 files changed, 51 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Todo	Sat Jan 02 03:28:26 2010 +0900
+++ b/Todo	Sat Jan 02 04:16:25 2010 +0900
@@ -2,6 +2,9 @@
 
 うーん、まだ、だめですね。
 
+ Session Manager の quit protocol って入れてない気がする...
+ 切れた場合の対処も入れないといけないんだよな。
+
 Sat Jan  2 00:02:41 JST 2010
 
 Todo:
--- a/rep/ServerMainLoop.java	Sat Jan 02 03:28:26 2010 +0900
+++ b/rep/ServerMainLoop.java	Sat Jan 02 04:16:25 2010 +0900
@@ -42,6 +42,7 @@
 	protected int parent_port;
 	protected static final int DEFAULT_PORT = 8766;
 	private SessionManagerEvent execAfterConnect = null;
+	private boolean running = true;
 
 
 	public void setReceivePort(int port) {
@@ -58,7 +59,7 @@
 	}
 	
 	public void mainLoop() throws IOException {
-		while(true){
+		while(running){
 			manager.checkWaitingCommandInMerge();
 			if (checkInputEvent() ||
 			    checkWaitingWrite()) { 
@@ -97,6 +98,11 @@
 		}
 		return false;
 	}
+	
+	public void serverStop() {
+		running = false;
+		selector.wakeup();
+	}
 
 	/**
 	 * To avoid dead locks, we write a command one at a time
--- a/rep/channel/SelectorSimulator.java	Sat Jan 02 03:28:26 2010 +0900
+++ b/rep/channel/SelectorSimulator.java	Sat Jan 02 04:16:25 2010 +0900
@@ -24,18 +24,15 @@
 	}
 
 	public int select() throws IOException {
-		while(true) {
-			getSelectedKeys();
-			if(selectedKeys.isEmpty()) {
-				try {
-					synchronized(this) {
-						wait();
-					}
-				} catch (InterruptedException e) {
-					throw new IOException();
+		getSelectedKeys();
+		if(selectedKeys.isEmpty()) {
+			try {
+				synchronized(this) {
+					wait();
 				}
-			} else
-				break;
+			} catch (InterruptedException e) {
+				throw new IOException();
+			}
 		}
 		return selectedKeys.size();
 	}
--- a/test/sematest/TestInterManagerSession.java	Sat Jan 02 03:28:26 2010 +0900
+++ b/test/sematest/TestInterManagerSession.java	Sat Jan 02 04:16:25 2010 +0900
@@ -61,6 +61,7 @@
 										startEditor(m);
 									}
 									editors[0].setCommand(editorStartCmds);
+									new Thread(finishTest).start();
 								}
 							}
 					);
@@ -152,8 +153,13 @@
 		 */
 		REPServerSocketChannel.isSimulation = true;
 		// At least 3 TestEditors are required.
-		TestInterManagerSession test = new TestInterManagerSession(1, 2, 3);
+		final TestInterManagerSession test = new TestInterManagerSession(1, 2, 3);
 		logger.setLogLevel(5);
+		test.finishTest = new Runnable() {
+			public void run() {		
+				test.finishTest();
+			}
+		};
 		test.startTest();
 	}
 
--- a/test/sematest/TestSessionManager.java	Sat Jan 02 03:28:26 2010 +0900
+++ b/test/sematest/TestSessionManager.java	Sat Jan 02 04:16:25 2010 +0900
@@ -17,6 +17,7 @@
 	public SessionManager sessionManagers[];
 	public SessionManager slaveSessionManagers[];
 	public TestEditor editors[];
+	public Runnable finishTest;
 
 	/*
 	 * All test is performed in localhost, so all session manager
@@ -43,6 +44,7 @@
 					for(SessionManager slave:slaveSessionManagers) {
 						i = startSessionManager(slave,i,masterPort + i);
 					}
+					new Thread(finishTest).start();
 				}
 			}};
 	
@@ -108,6 +110,23 @@
 		}
 	}
 
+	public void finishTest() {
+		for(TestEditor editor:editors) {
+			try {
+				editor.join();
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+		}
+
+		for(SessionManager slave:slaveSessionManagers) {
+			slave.serverStop();
+		}
+		for(SessionManager master:sessionManagers) {
+			master.serverStop();
+		}
+	}
+	
 	public static void main(String[] args){
 		/*
 		 * set simulation mode
@@ -116,8 +135,14 @@
 		 */
 		REPServerSocketChannel.isSimulation = true;
 		// At least 3 TestEditors are required.
-		TestSessionManager test = new TestSessionManager(1, 0, 3);
+		final TestSessionManager test = new TestSessionManager(1, 0, 3);
 		logger.setLogLevel(5);
+
+		test.finishTest = new Runnable() {
+			public void run() {		
+				test.finishTest();
+			}
+		};
 		test.startTest();
 	}