annotate src/pathfinder/mergetest/TestMerger2.java @ 153:6326e5ea4595

*** empty log message ***
author pin
date Sat, 23 Aug 2008 11:28:16 +0900
parents 5942e0e3c632
children 6a3c982bd72a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
1 package pathfinder.mergetest;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
2
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
3 import java.util.LinkedList;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
4 import remoteeditor.command.REPCommand;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
5 import remoteeditor.network.REP;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
6
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
7 public class TestMerger2 {
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
8 static public int cmdNO[] = { REP.REPCMD_INSERT, REP.REPCMD_REPLACE, REP.REPCMD_DELETE };
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
9 protected NetworkSimulator<REPCommand> ns=null;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
10 protected LinkedList<EditorSimulatorWithoutMerger> editors;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
11 protected SessionManagerSimulatorWithMerger<REPCommand> sema;
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
12 protected LinkedList<UsersSimulator> users = new LinkedList<UsersSimulator>();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
13
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
14 static private String[] text0 = {
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
15 "aaa", "bbb", "ccc", "ddd", "eee",
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
16 "fff", "ggg", "hhh", "iii", "jjj",
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
17 "kkk", "lll", "mmm", "nnn", "ooo",
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
18 "ppp", "qqq", "rrr", "sss", "ttt",
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
19 "uuu", "vvv", "www", "xxx", "yyy", "zzz"
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
20 };
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
21
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
22 public TestMerger2(){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
23 editors = new LinkedList<EditorSimulatorWithoutMerger>();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
24 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
25
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
26 public static void main(String[] args){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
27 TestMerger2 tm;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
28 /* get the number of Editors. */
149
5942e0e3c632 *** empty log message ***
pin
parents: 148
diff changeset
29 int i = (args.length>0) ? Integer.parseInt(args[0]) : 4;
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
30 System.out.println("number of Editor = "+i);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
31 /* get the number of Command. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
32 int j = (args.length>1) ? Integer.parseInt(args[1]) : 3;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
33 System.out.println("number of Packet = "+j);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
34
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
35 /* create, initialize and start test. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
36 tm = new TestMerger2();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
37 //tm.init(false, i, j, 1); // init( boolean SM?, int max_client, int max_packet, int loglevel);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
38 tm.init(true, i, j, 1);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
39 tm.startTest();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
40
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
41 tm.printAllTexts();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
42 //if (!tm.checkCS())
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
43 // System.out.println("Error!! :some ChannelSimulator still have packet!");
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
44 if (!tm.checkEquality())
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
45 System.out.println("Error!! :all Editor's text is NOT mutch!");
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
46 assert tm.checkEquality();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
47 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
48
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
49 protected void startTest() {
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
50 /* start all Editors. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
51 for (EditorSimulatorWithoutMerger ee: editors){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
52 ee.start();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
53 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
54 /* start SessionManager if it exist. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
55 if (sema!=null) sema.init();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
56 if (sema!=null) sema.start();
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
57
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
58 for(UsersSimulator u: users){
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
59 u.start();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
60 }
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
61 for(UsersSimulator u: users){
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
62 try {
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
63 u.join();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
64 } catch (InterruptedException e) { }
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
65 }
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
66
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
67
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
68 /* wait Editors finish. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
69 for (EditorSimulatorWithoutMerger ee: editors){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
70 try {
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
71 ee.join();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
72 } catch (InterruptedException e) {
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
73 e.printStackTrace();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
74 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
75 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
76 /* inform SessionManager to finish. */
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
77 if (sema!=null) {
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
78 sema.finish();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
79 try {
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
80 sema.join();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
81 } catch (InterruptedException e) {
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
82 // TODO Auto-generated catch block
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
83 e.printStackTrace();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
84 }
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
85 }
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
86 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
87
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
88 LinkedList<REPCommand> userCommand(int eid){
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
89 LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
90 cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 1, 0, 0, "replaced by editor:" + eid + ":1"));
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
91 cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 2, 0, 0, "replaced by editor:" + eid + ":2"));
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
92 cmds .add(new REPCommand(REP.REPCMD_REPLACE, 0, eid, 3, 0, 0, "replaced by editor:" + eid + ":3"));
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
93 if(eid == 0) cmds .add(new REPCommand(REP.SMCMD_QUIT, 0, eid, 4, 0, 0, "replaced by editor:" + eid + ":4"));
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
94 return cmds;
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
95 }
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
96
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
97 protected void init(boolean sm, int ne, int np, int ll){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
98 /* create NetworkSimulator, and SessionManager if it's required. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
99 if (sm){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
100 ns = new NetworkSimulatorwithSeMa<REPCommand>();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
101 sema = new SessionManagerSimulatorWithMerger<REPCommand>(ns, ne, 0);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
102 } else {
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
103 ns = new NetworkSimulatorwithoutSeMa<REPCommand>();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
104 sema = null;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
105 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
106 ns.setLogLevel(ll);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
107
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
108 /* create UsersSimulator. */
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
109 for(int i = 0; i < ne; i++){
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
110 ChannelSimulator<REPCommand> channel = ns.getAcceptedSession(i);
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
111 users.add(new UsersSimulator(channel, userCommand(i)));
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
112 }
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
113
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
114 /* create ne Editors and np commands. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
115 for (int i=0; i<ne; i++){
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
116 //LinkedList<REPCommand> cmds = new LinkedList<REPCommand>();
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
117 // 各エディタが送信するコマンド列を生成
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
118
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
119 /* create command list. */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
120 /*
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
121 for (int j=0; j<np; j++){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
122 String str = "created by Editor"+i+":"+j;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
123 REPCommand cmd = new REPCommand(REP.REPCMD_INSERT,
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
124 0, i, j,
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
125 10, //Verify.random(text.size()-1), //size-1?
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
126 str.length(), str);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
127 cmds.add( cmd);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
128 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
129 */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
130
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
131 /* create a Editor, and pass command list to it. */
153
6326e5ea4595 *** empty log message ***
pin
parents: 149
diff changeset
132 EditorSimulatorWithoutMerger ee = new EditorSimulatorWithoutMerger(i, ns, i==0?text0:null, "Editor"+i);
148
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
133 if(i==0) ee.setOwner(true);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
134 editors.add(ee);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
135 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
136 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
137
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
138 protected void printAllTexts(){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
139 for(EditorSimulatorWithoutMerger ee: editors){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
140 System.out.println("--"+ee.getName()+"------------------------");
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
141 ee.getText().printAllText();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
142 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
143 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
144 /*
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
145 private boolean checkCS(){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
146 return ns.checkAllCS();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
147 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
148 */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
149 protected boolean checkEquality(){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
150 /*
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
151 Text ee0 = editors.remove().getText();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
152 return editors.remove().getText().equals(ee0);
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
153 */
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
154 Text text0 = editors.element().getText();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
155 //System.out.println("------------------- 結果 --------------------");
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
156 for(EditorSimulatorWithoutMerger ee: editors){
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
157 //System.out.println(" ");
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
158 //ee.getText().printAllText();
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
159 if (!text0.equals(ee.getText())) return false;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
160 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
161 return true;
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
162 }
bc162b1a7ebf *** empty log message ***
pin
parents:
diff changeset
163 }