KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tctest > restart > system > ClientTerminatingTestApp


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tctest.restart.system;
6
7 import org.apache.commons.io.FileUtils;
8
9 import com.tc.objectserver.control.ExtraL1ProcessControl;
10 import com.tc.simulator.app.ApplicationConfig;
11 import com.tc.simulator.listener.ListenerProvider;
12 import com.tctest.ServerCrashingAppBase;
13
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Random JavaDoc;
18
19 public class ClientTerminatingTestApp extends ServerCrashingAppBase {
20
21   public static final String JavaDoc FORCE_KILL = "force-kill";
22
23   private static final int LOOP_COUNT = 2;
24   private static final List JavaDoc queue = new ArrayList JavaDoc();
25
26   private int id = -1;
27   private long count = 0;
28   private ExtraL1ProcessControl client;
29   private final boolean forceKill;
30
31   public ClientTerminatingTestApp(String JavaDoc appId, ApplicationConfig cfg, ListenerProvider listenerProvider) {
32     super(appId, cfg, listenerProvider);
33
34     String JavaDoc forceKillVal = cfg.getAttribute(FORCE_KILL);
35     if (forceKillVal != null && !forceKillVal.equals("")) {
36       forceKill = true;
37     } else {
38       forceKill = false;
39     }
40   }
41
42   public void run() {
43     List JavaDoc myList = new ArrayList JavaDoc();
44     synchronized (queue) {
45       if (id != -1) { throw new AssertionError JavaDoc("Only one controller per Instance allowed. Check the Execution count"); }
46       id = queue.size();
47       queue.add(myList);
48     }
49
50     Random JavaDoc random = new Random JavaDoc();
51     int times = LOOP_COUNT;
52     try {
53       while (times-- > 0) {
54         long toAdd = random.nextInt(10) * 50L + 1;
55         File JavaDoc workingDir = new File JavaDoc(getConfigFileDirectoryPath(), "client-" + id + "-" + times);
56         FileUtils.forceMkdir(workingDir);
57         System.err.println(this + "Creating Client with args " + id + " , " + toAdd);
58         client = new ExtraL1ProcessControl(getHostName(), getPort(), Client.class, getConfigFilePath(), new String JavaDoc[] {
59             "" + id, "" + toAdd, "" + forceKill }, workingDir);
60         client.start(20000);
61         int exitCode = client.waitFor();
62         if (exitCode == 0) {
63           System.err.println(this + "Client existed Normally");
64           verify(myList, toAdd);
65         } else {
66           String JavaDoc errorMsg = this + "Client existed Abnormally. Exit code = " + exitCode;
67           System.err.println(errorMsg);
68           throw new AssertionError JavaDoc(errorMsg);
69         }
70       }
71     } catch (Exception JavaDoc e) {
72       System.err.println(this + " Got - " + e);
73       throw new AssertionError JavaDoc(e);
74     }
75
76   }
77
78   private void verify(List JavaDoc myList, long toAdd) {
79     synchronized (myList) {
80       if (toAdd != myList.size()) {
81         String JavaDoc errorMsg = this + " Expected " + toAdd + " elements in the list. But Found " + myList.size();
82         System.err.println(errorMsg);
83         throw new AssertionError JavaDoc(errorMsg);
84       }
85     }
86     for (int i = 0; i < myList.size(); i++) {
87       synchronized (myList) {
88         if ((++count != ((Long JavaDoc) myList.get(i)).longValue())) {
89           String JavaDoc errorMsg = this + " Expected " + count + " value in the list. But Found " + myList.get(i);
90           System.err.println(errorMsg);
91           throw new AssertionError JavaDoc(errorMsg);
92         }
93       }
94     }
95   }
96
97   public String JavaDoc toString() {
98     return "Controller(" + id + ") :";
99   }
100
101   public static class Client {
102     private int id;
103     private long addCount;
104     private boolean shouldForceKill;
105
106     public Client(int i, long addCount, boolean shouldForceKill) {
107       this.id = i;
108       this.addCount = addCount;
109       this.shouldForceKill = shouldForceKill;
110     }
111
112     public static void main(String JavaDoc args[]) {
113       if (args.length < 2 || args.length > 3) { throw new AssertionError JavaDoc(
114                                                                          "Usage : Client <id> <num of increments> [shouldForceKill]"); }
115
116       boolean shouldForceKill;
117       if (args.length == 3 && args[2] != null && !args[2].equals("")) {
118         shouldForceKill = Boolean.valueOf(args[2]).booleanValue();
119       } else {
120         shouldForceKill = false;
121       }
122
123       Client client = new Client(Integer.parseInt(args[0]), Long.parseLong(args[1]), shouldForceKill);
124       client.execute();
125     }
126
127     // Written so that many transactions are created ...
128
public void execute() {
129       List JavaDoc myList = null;
130       long count = 0;
131       System.err.println(this + " execute : addCount = " + addCount);
132       synchronized (queue) {
133         myList = (List JavaDoc) queue.get(id);
134       }
135       synchronized (myList) {
136         if (myList.size() > 0) {
137           count = ((Long JavaDoc) myList.get(myList.size() - 1)).longValue();
138           myList.clear();
139         }
140       }
141       while (addCount-- > 0) {
142         synchronized (myList) {
143           myList.add(new Long JavaDoc(++count));
144         }
145       }
146
147       if (shouldForceKill) {
148         System.err.println(this + " killed forceably :" + count);
149         Runtime.getRuntime().halt(0);
150       } else {
151         System.err.println(this + " put till :" + count);
152         System.exit(0);
153       }
154     }
155
156     public String JavaDoc toString() {
157       return "Client(" + id + ") :";
158     }
159
160   }
161
162 }
163
Popular Tags