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