Skip to content

Commit 1970dd5

Browse files
committed
Removed console noise caused by bot thread dying (as expected) from exception test
1 parent c30289b commit 1970dd5

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed

src/main/java/bwapi/BotWrapper.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -151,28 +151,41 @@ Throwable getLastBotThrow() {
151151

152152
private Thread createBotThread() {
153153
return new Thread(() -> {
154-
configuration.log("Bot: Thread started");
155-
while ( ! gameOver) {
156-
configuration.log("Bot: Attempting to handle next frame");
157-
frameBuffer.lockSize.lock();
158-
try {
159-
while (frameBuffer.empty()) {
160-
configuration.log("Bot: Waiting for next frame");
161-
performanceMetrics.botIdle.startTiming();
162-
frameBuffer.conditionSize.awaitUninterruptibly();
154+
try {
155+
configuration.log("Bot: Thread started");
156+
while (!gameOver) {
157+
158+
configuration.log("Bot: Attempting to handle next frame");
159+
frameBuffer.lockSize.lock();
160+
try {
161+
while (frameBuffer.empty()) {
162+
configuration.log("Bot: Waiting for next frame");
163+
performanceMetrics.botIdle.startTiming();
164+
frameBuffer.conditionSize.awaitUninterruptibly();
165+
}
166+
performanceMetrics.botIdle.stopTiming();
167+
} finally {
168+
frameBuffer.lockSize.unlock();
163169
}
164-
performanceMetrics.botIdle.stopTiming();
165-
} finally {
166-
frameBuffer.lockSize.unlock();
167-
}
168-
configuration.log("Bot: Peeking next frame");
169-
game.clientData().setBuffer(frameBuffer.peek());
170-
performanceMetrics.frameBufferSize.record(frameBuffer.framesBuffered() - 1);
171-
try {
170+
171+
configuration.log("Bot: Peeking next frame");
172+
game.clientData().setBuffer(frameBuffer.peek());
173+
performanceMetrics.frameBufferSize.record(frameBuffer.framesBuffered() - 1);
174+
172175
configuration.log("Bot: Handling frame #" + game.getFrameCount());
173176
handleEvents();
174-
} finally {
175-
// In the case where t
177+
frameBuffer.dequeue();
178+
}
179+
} catch (Throwable throwable) {
180+
// Record the throw,
181+
// Then allow the thread to terminate silently.
182+
// The main thread will look for the stored throw.
183+
lastBotThrowLock.lock();
184+
lastBotThrow = throwable;
185+
lastBotThrowLock.unlock();
186+
187+
// Awaken any threads waiting on bot progress
188+
while (!frameBuffer.empty()) {
176189
frameBuffer.dequeue();
177190
}
178191
}
@@ -184,21 +197,14 @@ private void handleEvents() {
184197
if (gameData.getFrameCount() > 0 || ! configuration.unlimitedFrameZero) {
185198
performanceMetrics.botResponse.startTiming();
186199
}
200+
187201
// Populate gameOver before invoking event handlers (in case the bot throws)
188202
for (int i = 0; i < gameData.getEventCount(); i++) {
189203
gameOver = gameOver || gameData.getEvents(i).getType() == EventType.MatchEnd;
190204
}
191-
try {
192-
for (int i = 0; i < gameData.getEventCount(); i++) {
193-
EventHandler.operation(eventListener, game, gameData.getEvents(i));
194-
}
195-
} catch (Throwable throwable) {
196-
lastBotThrowLock.lock();
197-
lastBotThrow = throwable;
198-
lastBotThrowLock.unlock();
199-
throw throwable;
200-
} finally {
201-
performanceMetrics.botResponse.stopTiming();
205+
for (int i = 0; i < gameData.getEventCount(); i++) {
206+
EventHandler.operation(eventListener, game, gameData.getEvents(i));
202207
}
208+
performanceMetrics.botResponse.stopTiming();
203209
}
204210
}

src/main/java/bwapi/ClientData.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ class ClientData {
44
private WrappedBuffer buffer;
55
private GameData gameData;
66
ClientData() {
7-
System.out.println("new ClientData()"); // TODO: REMOVE!
87
gameData = new GameData(0);
98
}
109
GameData gameData() {

src/main/java/bwapi/Game.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ private static boolean hasPower(final int x, final int y, final UnitType unitTyp
142142
Call this method in EventHander::OnMatchStart
143143
*/
144144
void init() {
145-
System.out.println("Game.init()"); // TODO: REMOVE!
146145
visibleUnits.clear();
147146

148147
final int forceCount = gameData().getForceCount();

src/test/java/bwapi/SynchronizationEnvironment.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ public class SynchronizationEnvironment {
4343
return null;
4444
}).when(client).update();
4545
doAnswer(answer -> {
46-
System.out.println("Test: onStart()");
46+
configuration.log("Test: onStart()");
4747
return null;
4848
}).when(listener).onStart();
4949
doAnswer(answer -> {
50-
System.out.println("Test: onEnd()");
50+
configuration.log("Test: onEnd()");
5151
return null;
5252
}).when(listener).onEnd(anyBoolean());
5353
doAnswer(answer -> {
54-
System.out.println("Test: onFrame()");
54+
configuration.log("Test: onFrame()");
5555
if (onFrames.containsKey(liveFrame())) {
5656
onFrames.get(liveFrame()).run();
5757
}
@@ -92,7 +92,7 @@ private int liveFrame() {
9292

9393
private void clientUpdate() {
9494
client.clientData().gameData().setFrameCount(liveFrame() + 1);
95-
System.out.println("Test: clientUpdate() to liveFrame #" + liveFrame());
95+
configuration.log("Test: clientUpdate() to liveFrame #" + liveFrame());
9696
if (liveFrame() == 0) {
9797
client.clientData().gameData().setIsInGame(true);
9898
client.clientData().gameData().setEventCount(2);

src/test/java/bwapi/SynchronizationTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,12 @@ public void async_IfDelay_ThenBuffer() throws InterruptedException {
6565
SynchronizationEnvironment environment = new SynchronizationEnvironment();
6666
environment.configuration.async = true;
6767
environment.configuration.asyncFrameDurationMs = 1;
68-
environment.configuration.asyncFrameBufferSize = 3;
69-
environment.configuration.logVerbosely = true;
68+
environment.configuration.asyncFrameBufferSize = 4;
7069
environment.onFrame(1, () -> {
71-
sleepUnchecked(50);
70+
sleepUnchecked(5);
7271
assertEquals("Bot should be observing an old frame", 1, environment.bwClient.getGame().getFrameCount());
73-
assertEquals("Client should be as far ahead as the frame buffer allows", 3, environment.liveGameData().getFrameCount());
74-
assertEquals("Bot should be behind the live game", 2, environment.bwClient.framesBehind());
72+
assertEquals("Client should be as far ahead as the frame buffer allows", 4, environment.liveGameData().getFrameCount());
73+
assertEquals("Bot should be behind the live game", 3, environment.bwClient.framesBehind());
7574
});
7675
environment.runGame();
7776
}

0 commit comments

Comments
 (0)