1 package cz.cuni.amis.pogamut.ut2004.bot;
2
3 import java.util.concurrent.TimeUnit;
4
5 import cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot;
6 import cz.cuni.amis.pogamut.ut2004.bot.params.UT2004BotParameters;
7 import cz.cuni.amis.pogamut.ut2004.server.exception.UCCStartException;
8 import cz.cuni.amis.pogamut.ut2004.test.UT2004Test;
9 import cz.cuni.amis.pogamut.ut2004.utils.UCCWrapperConf;
10 import cz.cuni.amis.utils.StopWatch;
11
12
13
14
15
16
17
18
19
20 public abstract class UT2004BotTest extends UT2004Test {
21
22
23
24
25
26
27
28 @Override
29 public void startUCC(UCCWrapperConf uccConf) throws UCCStartException {
30 uccConf.setMapName(getMapName());
31 uccConf.setGameType(getGameType());
32 super.startUCC(uccConf);
33 }
34
35
36
37
38 protected String getGameType() {
39 return "BotDeathMatch";
40 }
41
42
43
44
45 protected String getMapName() {
46 return "DM-Flux2";
47 }
48
49
50
51
52
53
54
55
56
57
58 protected void startTest(Class<? extends UT2004BotTestController> controllerClass) {
59 startTest(controllerClass, 1);
60 }
61
62
63
64
65
66
67
68
69
70
71
72 protected void startTest(Class<? extends UT2004BotTestController> controllerClass, double latchWaitMinutes) {
73 startTest(controllerClass, latchWaitMinutes, null);
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 protected void startTest(Class<? extends UT2004BotTestController> controllerClass, double latchWaitMinutes, int agentsCount) {
90 UT2004BotParameters[] params = new UT2004BotParameters[agentsCount];
91 for (int i = 0; i < agentsCount; ++i) {
92 params[i] = new UT2004BotParameters();
93 }
94 startTest(controllerClass, latchWaitMinutes, params);
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110 protected void startTest(Class<? extends UT2004BotTestController> controllerClass, double latchWaitMinutes, UT2004BotParameters... params) {
111 int numParams = (params == null || params.length == 0 ? 0 : params.length);
112 int numBots = numParams == 0 ? 1 : numParams;
113
114 UT2004Bot bots[];
115
116 if (params == null || params.length == 0) {
117 bots = new UT2004Bot[1];
118 bots[0] = startUTBot(controllerClass);
119 } else {
120 bots = startAllUTBots(controllerClass, params).toArray(new UT2004Bot[0]);
121 }
122
123 long timeoutInMillis = (long)(latchWaitMinutes * 60 * 1000);
124
125 StopWatch watches = new StopWatch();
126
127 for (int i = 0; i < numBots; ++i) {
128 UT2004BotTestController controller = (UT2004BotTestController) bots[i].getController();
129 try {
130 controller.getTestLatch().await((long)(timeoutInMillis - watches.check()), TimeUnit.MILLISECONDS);
131 } catch (Exception ex) {
132 ex.printStackTrace();
133 for (int j = i; j < numBots; ++j) bots[j].kill();
134 Throwable throwable = controller.getLogicModule().getLogicException();
135 throw new RuntimeException("Test failed, bot did not finished.", throwable);
136 }
137 if (timeoutInMillis - watches.check() < 0) {
138 controller.timeout();
139 }
140 bots[i].kill();
141 if (controller.isFailure()) {
142 for (int j = i+1; j < numBots; ++j) bots[j].kill();
143 if (controller.getMessage() != null) {
144 System.out.println("[ERROR] " + controller.getMessage());
145 }
146
147 String exceptionMessage = "Test has failed!";
148 if(controller.getMessage() == null)
149 exceptionMessage += " " + controller.getMessage();
150 throw new RuntimeException(exceptionMessage, controller.getCause());
151
152 } else {
153 if (controller.getMessage() != null) {
154 System.out.println("[OK] " + controller.getMessage());
155 }
156 }
157 }
158
159 System.out.println("---/// TEST OK ///---");
160 }
161
162 }