Mercurial > hg > RemoteEditor > REPSessionManager
annotate test/sematest/TestSessionManager.java @ 502:49b689b17d06 default tip
merged TestEditor to REPEditor
author | suika6039 |
---|---|
date | Tue, 21 Dec 2010 18:01:15 +0900 |
parents | 5945266c970d |
children |
rev | line source |
---|---|
189 | 1 package test.sematest; |
2 | |
192 | 3 import java.io.IOException; |
384 | 4 |
189 | 5 import rep.SessionManager; |
192 | 6 import rep.channel.REPLogger; |
189 | 7 import rep.channel.REPServerSocketChannel; |
382
4b87f89b3afd
REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
336
diff
changeset
|
8 import rep.gui.SessionManagerEvent; |
4b87f89b3afd
REP Session Manager (Java version)
one@firefly.cr.ie.u-ryukyu.ac.jp
parents:
336
diff
changeset
|
9 import rep.gui.SessionManagerGUI; |
470 | 10 import test.Text; |
193 | 11 |
189 | 12 |
13 public class TestSessionManager { | |
14 | |
192 | 15 static public REPLogger logger = REPLogger.singleton(); |
335 | 16 public int masterPort = 8766; |
17 public String host = "localhost"; | |
18 public SessionManager sessionManagers[]; | |
19 public SessionManager slaveSessionManagers[]; | |
20 public TestEditor editors[]; | |
430 | 21 public Runnable finishTest; |
189 | 22 |
301 | 23 /* |
24 * All test is performed in localhost, so all session manager | |
438 | 25 * should have different port number each other. |
301 | 26 */ |
27 | |
28 /* | |
29 * Test Pattern List | |
30 * Connect port for each editor | |
31 * Master/client flag for each editor | |
32 * Editor or slave session manager must be started by | |
303 | 33 * master session managers using syncExec. |
301 | 34 */ |
335 | 35 public int editorPort[] = {masterPort,masterPort,masterPort}; |
36 public boolean editorMaster[] = {true,false,false,false}; | |
37 private SessionManagerEvent ev1[] = { | |
289 | 38 new SessionManagerEvent() { |
39 // executed before first select(); | |
334 | 40 public void exec(SessionManager manager) { |
289 | 41 for(TestEditor editor:editors) { |
42 editor.start(); | |
43 } | |
292 | 44 int i = sessionManagers.length; |
45 for(SessionManager slave:slaveSessionManagers) { | |
46 i = startSessionManager(slave,i,masterPort + i); | |
47 } | |
430 | 48 new Thread(finishTest).start(); |
289 | 49 } |
50 }}; | |
51 | |
301 | 52 /* |
53 * Create all editors, master session managers and slave session | |
303 | 54 * managers with specified port. All instances are not started yet. |
301 | 55 */ |
56 | |
289 | 57 public TestSessionManager(int sm, int ss, int e) { |
58 sessionManagers = new SessionManager[sm]; | |
59 slaveSessionManagers = new SessionManager[ss]; | |
60 editors = new TestEditor[e]; | |
61 for(int i=0;i<sm;i++) { | |
62 sessionManagers[i] = new SessionManager(); | |
236 | 63 } |
289 | 64 for(int i=0;i<ss;i++) { |
65 slaveSessionManagers[i] = new SessionManager(); | |
66 } | |
67 for(int i=0;i<e;i++) { | |
68 int port = editorPort[i%editorPort.length]; | |
69 boolean master = editorMaster[i%editorMaster.length]; | |
301 | 70 // TestEditor extends Thread |
431 | 71 editors[i] = new TestEditor("Editor",host,port,master); |
289 | 72 } |
236 | 73 } |
74 | |
301 | 75 /* |
438 | 76 * start session manager. sm.init(port,gui) is a mainloop, so |
301 | 77 * we need Thread here. |
78 */ | |
415 | 79 public int startSessionManager(final SessionManager sm,int i,int port) { |
290 | 80 final SessionManagerGUI gui = new TestGUI(sm); |
289 | 81 final int port1 = port; |
82 logger.writeLog("TestSessionManager.startSessionManager() : start SessionManager"); | |
303 | 83 // syncExec does not wake selector, do this before run(). |
336 | 84 sm.setReceivePort(port1); |
335 | 85 setSMEvent(sm,i); |
289 | 86 Runnable start = new Runnable() { |
313 | 87 public void run() { |
88 try { | |
491 | 89 sm.setDeadlockTime(1000); |
313 | 90 sm.init(port1,gui); |
91 } catch (IOException e) { | |
92 } catch (InterruptedException e) { | |
93 } | |
289 | 94 } |
95 }; | |
336 | 96 new Thread(start).start(); |
289 | 97 return i+1; |
189 | 98 } |
99 | |
310 | 100 |
335 | 101 public void setSMEvent(SessionManager s,int i) { |
102 if (i<ev1.length) { | |
103 s.syncExec(ev1[i]); | |
104 } | |
105 return ; | |
106 } | |
107 | |
334 | 108 protected void startTest() { |
310 | 109 int i = 0; |
110 for(SessionManager master:sessionManagers) { | |
111 i = startSessionManager(master,i, masterPort + i); | |
112 } | |
113 } | |
114 | |
430 | 115 public void finishTest() { |
116 for(TestEditor editor:editors) { | |
117 try { | |
118 editor.join(); | |
119 } catch (InterruptedException e) { | |
120 e.printStackTrace(); | |
121 } | |
122 } | |
123 | |
124 for(SessionManager slave:slaveSessionManagers) { | |
125 slave.serverStop(); | |
126 } | |
127 for(SessionManager master:sessionManagers) { | |
128 master.serverStop(); | |
129 } | |
470 | 130 Text t = editors[0].text; |
131 for(TestEditor editor:editors) { | |
132 assert (editor.text.equals(t)); | |
133 } | |
430 | 134 } |
135 | |
220 | 136 public static void main(String[] args){ |
301 | 137 /* |
138 * set simulation mode | |
139 * isSimulation=true thread base simulation for PathFinder | |
140 * isSimulation=false socket based communication mode | |
141 */ | |
440 | 142 REPServerSocketChannel.isSimulation = true; |
332 | 143 // At least 3 TestEditors are required. |
430 | 144 final TestSessionManager test = new TestSessionManager(1, 0, 3); |
220 | 145 logger.setLogLevel(5); |
430 | 146 |
147 test.finishTest = new Runnable() { | |
148 public void run() { | |
149 test.finishTest(); | |
150 } | |
151 }; | |
291 | 152 test.startTest(); |
220 | 153 } |
154 | |
289 | 155 |
189 | 156 } |