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